Skip to content

Commit

Permalink
broken: Continue watching
Browse files Browse the repository at this point in the history
  • Loading branch information
Eltik committed Sep 7, 2024
1 parent 47a9d45 commit da0929c
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 58 deletions.
9 changes: 8 additions & 1 deletion Targets/Clients/DatabaseClient/Sources/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ import SharedModels

public struct DatabaseClient: Sendable {
public let initDB: @Sendable () async -> Void
public let createCollection: @Sendable (_ name: String) async -> String

public let fetchCollection: @Sendable (_ id: String) async -> CollectionData?
public let fetchCollections: @Sendable () async -> [HomeSection]

public let isInCollection: @Sendable(_ collectionId: String, _ moduleId: String, _ infoData: CollectionItem) async -> Bool

public let createCollection: @Sendable (_ name: String) async -> String
public let addToCollection: @Sendable (_ collectionId: String, _ moduleId: String, _ infoData: CollectionItem) async -> Void
public let updateItemInCollection: @Sendable (_ collectionId: String, _ moduleId: String, _ infoData: CollectionItem) async -> Void
public let removeFromCollection: @Sendable (_ collectionId: String, _ moduleId: String, _ infoData: CollectionItem) async -> Void
public let removeCollection: @Sendable (_ collectionId: String, _ moduleId: String) async -> Void


public let fetchContinueWatching: @Sendable () async -> HomeSection
public let addToContinueWatching: @Sendable (_ moduleId: String, _ infoData: CollectionItem) async -> Void
}

extension DependencyValues {
Expand Down
186 changes: 137 additions & 49 deletions Targets/Clients/DatabaseClient/Sources/Live.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,72 +63,28 @@ extension DatabaseClient: DependencyKey {

return Self(
initDB: {
do {
let dbQueue: DatabaseQueue = {
let dbPath = try! fetchDatabasePath()
return try! DatabaseQueue(path: dbPath)
}()

// ??

try dbQueue.close()
} catch {
print("Error initializing database file!")
print("\(error)")
}
},
createCollection: { name in
let randomId = UUID().uuidString;
do {
let dbQueue: DatabaseQueue = {
let dbPath = try! fetchDatabasePath()
return try! DatabaseQueue(path: dbPath)
}()

try dbQueue.write { db in
let collectionTableName = "collection-\(randomId)"
let itemsTableName = "items-\(randomId)"

try db.create(table: collectionTableName) { t in
t.column("uuid", .text).primaryKey()
t.column("name", .text).notNull()
}

try db.create(table: itemsTableName) { t in
try db.create(table: "continuewatching", options: .ifNotExists) { t in
t.column("id", .integer).primaryKey()
t.column("collection_uuid", .text).notNull().references(collectionTableName, onDelete: .cascade)
t.column("moduleId", .text).notNull()
t.column("infoData", .jsonText).notNull()
t.column("flag", .text).notNull()
t.column("episodeData", .jsonText).notNull()
}
}

try dbQueue.close()
} catch {
print("Error creating collection tables for \(name)!")
print("\(error)")
}

do {
let dbQueue: DatabaseQueue = {
let dbPath = try! fetchDatabasePath()
return try! DatabaseQueue(path: dbPath)
}()

try dbQueue.write { db in
let collectionTableName = "collection-\(randomId)"
try db.execute(sql: "INSERT INTO '\(collectionTableName)' (uuid, name) VALUES (?, ?)", arguments: [randomId, name])
print("Successfully created collection for \(name). ID: \(randomId)")
}


try dbQueue.close()
} catch {
print("Error creating base collections for \(name)!")
print("Error initializing database file!")
print("\(error)")
}

return randomId;
},

fetchCollection: { id in
do {
let dbQueue: DatabaseQueue = {
Expand Down Expand Up @@ -245,6 +201,60 @@ extension DatabaseClient: DependencyKey {
return false
}
},

createCollection: { name in
let randomId = UUID().uuidString;
do {
let dbQueue: DatabaseQueue = {
let dbPath = try! fetchDatabasePath()
return try! DatabaseQueue(path: dbPath)
}()

try dbQueue.write { db in
let collectionTableName = "collection-\(randomId)"
let itemsTableName = "items-\(randomId)"

try db.create(table: collectionTableName) { t in
t.column("uuid", .text).primaryKey()
t.column("name", .text).notNull()
}

try db.create(table: itemsTableName) { t in
t.column("id", .integer).primaryKey()
t.column("collection_uuid", .text).notNull().references(collectionTableName, onDelete: .cascade)
t.column("moduleId", .text).notNull()
t.column("infoData", .jsonText).notNull()
t.column("flag", .text).notNull()
}
}

try dbQueue.close()
} catch {
print("Error creating collection tables for \(name)!")
print("\(error)")
}

do {
let dbQueue: DatabaseQueue = {
let dbPath = try! fetchDatabasePath()
return try! DatabaseQueue(path: dbPath)
}()

try dbQueue.write { db in
let collectionTableName = "collection-\(randomId)"
try db.execute(sql: "INSERT INTO '\(collectionTableName)' (uuid, name) VALUES (?, ?)", arguments: [randomId, name])
print("Successfully created collection for \(name). ID: \(randomId)")
}

try dbQueue.close()
} catch {
print("Error creating base collections for \(name)!")
print("\(error)")
}

return randomId;
},

addToCollection: { collectionId, moduleId, infoData in
do {
let dbQueue: DatabaseQueue = {
Expand Down Expand Up @@ -332,6 +342,84 @@ extension DatabaseClient: DependencyKey {
print("Error deleting collection!")
print("\(error)")
}
},

fetchContinueWatching: {
do {
let dbQueue: DatabaseQueue = {
let dbPath = try! fetchDatabasePath()
return try! DatabaseQueue(path: dbPath)
}()

let continueWatchingItems = try dbQueue.read { db in
// Fetch all rows from the "continuewatching" table
let rows = try Row.fetchAll(db, sql: "SELECT * FROM continuewatching")

let randomId = UUID().uuidString;
var result = HomeSection(id: randomId, title: "Continue Watching", type: 3, list: [])

for row in rows {
// Parse each row to create a ContinueWatchingItem object
let moduleId: String = row["moduleId"]
let infoDataString: String = row["infoData"]
let episodeDataString: String = row["episodeData"]

do {
let item = try JSONDecoder().decode(CollectionItem.self, from: infoDataString.data(using: .utf8)!)
let homeData = HomeData(
url: item.url,
titles: Titles(primary: item.infoData.titles.primary, secondary: item.infoData.titles.secondary ?? ""),
description: item.infoData.description,
poster: item.infoData.poster,
label: Label(text: "Test", color: ""),
indicator: "\(item.flag.rawValue)",
status: item.flag,
current: nil,
total: nil
)
result.list.append(homeData)
} catch {
continue
}
}

return result
}


try dbQueue.close()

return continueWatchingItems
} catch {
print("Error fetching continue watching!")
print("\(error)")

let randomId = UUID().uuidString;

return HomeSection(id: randomId, title: "Continue Watching", type: 0, list: [])
}
},

addToContinueWatching: { moduleId, infoData in
do {
let dbQueue: DatabaseQueue = {
let dbPath = try! fetchDatabasePath()
return try! DatabaseQueue(path: dbPath)
}()

// temp need episode data
try dbQueue.write { db in
try db.execute(sql: """
INSERT INTO 'continuewatching' (moduleId, infoData, episodeData) VALUES (?, ?, ?);
""", arguments: [moduleId, try? JSONEncoder().encode(infoData), try? JSONEncoder().encode(HomeSection(id: "", title: "", type: 0, list: []))])
print("Successfully added item to continue watching for \(infoData.infoData.titles.primary).")
}

try dbQueue.close()
} catch {
print("Error adding to continue watching!")
print("\(error)")
}
}
)
}()
Expand Down
Loading

0 comments on commit da0929c

Please sign in to comment.