Skip to content

Commit

Permalink
Fix notification migration
Browse files Browse the repository at this point in the history
  • Loading branch information
BoogieMonster1O1 committed Jun 29, 2024
1 parent 0d13645 commit 764652e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
35 changes: 26 additions & 9 deletions Sources/App/GraphQL/Mutation/Resolver+MutateUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ extension Resolver {
user.setValue(\.bio, arguments.bio, orElse: nil)
user.setValue(\.pronouns, arguments.pronouns, orElse: nil)

try await user.update(on: request.db)
do {
try await user.update(on: request.db)
} catch {
request.logger.error("Error updating user profile: \(String(reflecting: error))")
throw Abort(.internalServerError, reason: "Error updating user profile")
}

return user
}
Expand All @@ -40,9 +45,16 @@ extension Resolver {
}

let notif: Notification = .follow(targetUser: try target.requireID(), referenceUser: try user.requireID())
try await notif.create(on: request.db)

try await user.$following.attach(target, on: request.db)

do {
try await request.db.transaction { db in
try await notif.create(on: db)
try await user.$following.attach(target, on: db)
}
} catch {
request.logger.error("Error following user: \(String(reflecting: error))")
throw Abort(.internalServerError, reason: "Error following user")
}

return try await target.$following.query(on: request.db).count()
}
Expand All @@ -53,12 +65,17 @@ extension Resolver {
let user = try await getContextUser(request)

let target = try await RegisteredUser.query(on: request.db)
.filter(\.$id == arguments.id)
.first()
.unwrap(or: Abort(.notFound, reason: "User \(arguments.id) not found"))
.get()
.filter(\.$id == arguments.id)
.first()
.unwrap(or: Abort(.notFound, reason: "User \(arguments.id) not found"))
.get()

try await user.$following.detach(target, on: request.db)
do {
try await user.$following.detach(target, on: request.db)
} catch {
request.logger.error("Error unfollowing user: \(String(reflecting: error))")
throw Abort(.internalServerError, reason: "Error unfollowing user")
}

return try await target.$following.query(on: request.db).count()
}
Expand Down
8 changes: 5 additions & 3 deletions Sources/App/Migrations/010_CreateNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ struct CreateNotifications: AsyncMigration {
.create()
try await db.schema("notifications")
.field("id", .string, .required)
.field("target_user_id", .int, .references("registeredUsers", "id"))
.field("target_post_id", .string, .references("posts", "id"))
.field("target_user_id", .int, .references("registeredUsers", "id"), .required)
.field("reference_user_id", .int, .references("registeredUsers", "id"))
.field("reference_post_id", .string, .references("posts", "id"))
.field("created_at", .datetime, .required)
.field("deleted_at", .datetime)
.field("type", type, .required)
Expand All @@ -29,6 +30,7 @@ struct CreateNotifications: AsyncMigration {
}

func revert(on database: Database) async throws {
return try await database.schema("notifications").delete()
try await database.schema("notifications").delete()
try await database.enum("notificaion_type").delete()
}
}

0 comments on commit 764652e

Please sign in to comment.