Skip to content

Commit

Permalink
Liking, somewhat
Browse files Browse the repository at this point in the history
  • Loading branch information
BoogieMonster1O1 committed Jan 6, 2024
1 parent 4200bd2 commit 1039401
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Sources/App/GraphQL/Resolver+Mutation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ struct CreatePostArgs: Codable {
var content: String
}

struct LikePostArgs: Codable {
var user: Int?
var post: String
}

extension Resolver {
func editUserInfo(request: Request, arguments: EditUserInfoArgs) async throws -> RegisteredUser {
let token = try await getAndVerifyAccessToken(req: request)
Expand Down Expand Up @@ -111,6 +116,28 @@ extension Resolver {
try await post.update(on: request.db)
return post
}

func likePost(request: Request, arguments: LikePostArgs) async throws -> LikedPost {
try await assertPermission(request: request, .createPosts)
let token = try await getAndVerifyAccessToken(req: request)
if arguments.user != token.id {
try await assertPermission(request: request, .admin)
}
let lp: LikedPost = .init(postId: arguments.post, userId: arguments.user ?? token.id)
try await lp.create(on: request.db)
return lp
}

// func unlikePost(request: Request, arguments: LikePostArgs) async throws -> LikedPost {
// try await assertPermission(request: request, .createPosts)
// let token = try await getAndVerifyAccessToken(req: request)
// if arguments.user != token.id {
// try await assertPermission(request: request, .admin)
// }
// let lp = LikedPost.
// try await lp.delete(on: request.db)
// return lp
// }
}

extension RegisteredUser {
Expand Down
23 changes: 23 additions & 0 deletions Sources/App/Migrations/005_CreateLikedPosts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// 005_CreateLikedPosts.swift
//
//
// Created by Shrish Deshpande on 06/01/24.
//

import Fluent

struct CreateLikedPosts: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.schema("likedPosts")
.field("id", .uuid, .required)
.field("postId", .string, .required, .references("posts", "id"))
.field("userId", .int, .required, .references("registeredUsers", "id"))
.unique(on: "id")
.create()
}

func revert(on database: Database) -> EventLoopFuture<Void> {
return database.schema("likedPosts").delete()
}
}
32 changes: 32 additions & 0 deletions Sources/App/Models/LikedPost.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// LikedPost.swift
//
//
// Created by Shrish Deshpande on 06/01/24.
//

import Foundation

import Vapor
import Fluent

final class LikedPost: Model, Content {
public static let schema = "likedPosts"

@ID(key: .id)
var id: UUID?

@Parent(key: "postId")
var post: Post

@Parent(key: "userId")
var user: RegisteredUser

init() {
}

init(postId: String, userId: Int) {
self.$post.id = postId
self.$user.id = userId
}
}

0 comments on commit 1039401

Please sign in to comment.