-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseManager.swift
93 lines (80 loc) · 2.84 KB
/
DatabaseManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// DatabaseManager.swift
// GrappaMovie
//
// Created by Kristian Emil on 27/11/2024.
//
import Foundation
import GRDB
// This class will handle all our database operations
class DatabaseManager {
// This gives us a single, shared instance we can use throughout the app
static let shared = DatabaseManager()
// This is our database connection
private var dbQueue: DatabaseQueue!
// This represents a favorite movie in our database
struct FavoriteMovie: Codable, FetchableRecord, PersistableRecord {
let id: Int
let title: String
let overview: String
let posterPath: String?
let releaseDate: String
let voteAverage: Double
// This tells GRDB how to create the database table
static let databaseTableName = "favoriteMovie"
}
private init() {
// Set up the database in the app's documents directory
do {
let databaseURL = try FileManager.default
.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("movies.sqlite")
dbQueue = try DatabaseQueue(path: databaseURL.path)
// Create our table if it doesn't exist
try dbQueue.write { db in
try db.create(table: "favoriteMovie", ifNotExists: true) { table in
table.column("id", .integer).primaryKey()
table.column("title", .text)
table.column("overview", .text)
table.column("posterPath", .text)
table.column("releaseDate", .text)
table.column("voteAverage", .double)
}
}
} catch {
print("Database setup failed: \(error.localizedDescription)")
}
}
// Add a movie to favorites
func addFavorite(_ movie: Movie) throws {
let favorite = FavoriteMovie(
id: movie.id,
title: movie.title,
overview: movie.overview,
posterPath: movie.posterPath,
releaseDate: movie.releaseDate,
voteAverage: movie.voteAverage
)
try dbQueue.write { db in
try favorite.insert(db)
}
}
// Remove a movie from favorites
func removeFavorite(id: Int) throws {
try dbQueue.write { db in
_ = try FavoriteMovie.deleteOne(db, key: id)
}
}
// Check if a movie is favorited
func isFavorite(id: Int) throws -> Bool {
try dbQueue.read { db in
try FavoriteMovie.exists(db, key: id)
}
}
// Get all favorite movies
func getAllFavorites() throws -> [FavoriteMovie] {
try dbQueue.read { db in
try FavoriteMovie.fetchAll(db)
}
}
}