Skip to content

Commit

Permalink
Get recent posts
Browse files Browse the repository at this point in the history
  • Loading branch information
BoogieMonster1O1 committed Jan 9, 2024
1 parent a91f6fe commit 831cccf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
37 changes: 31 additions & 6 deletions Sources/App/GraphQL/Query/Resolver+GetPosts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,28 @@ import Vapor

struct RecentPostsArgs: Codable {
let count: Int
let before: Date?
let before: Int? // Unix milliseconds
let likes: Bool?

var beforeDate: Date? {
guard let before = self.before else {
return nil
}
return Date(timeIntervalSince1970: TimeInterval(before / 1000))
}

var hasLikes: Bool {
self.likes ?? false
}
}

struct PostByIdArgs: Codable {
let id: String
let likes: Bool?

var hasLikes: Bool {
self.likes ?? false
}
}

extension Resolver {
Expand All @@ -22,21 +43,25 @@ extension Resolver {
.all()
}

func getPostById(request: Request, arguments: StringIdArgs) async throws -> [Post] {
func getPostById(request: Request, arguments: PostByIdArgs) async throws -> [Post] {
try await assertPermission(request: request, .read)
return try await Post.query(on: request.db)
let query: QueryBuilder<Post> = arguments.hasLikes ? Post.query(on: request.db).with(\.$likes) : Post.query(on: request.db)
return try await query
.filter(\.$id == arguments.id)
.all()
}

func getRecentPosts(request: Request, arguments: RecentPostsArgs) async throws -> [Post] {
try await assertPermission(request: request, .read)

return try await Post.query(on: request.db)
let query: QueryBuilder<Post> = arguments.hasLikes ? Post.query(on: request.db).with(\.$likes) : Post.query(on: request.db)
let before = arguments.beforeDate ?? Date.now

return try await query
.filter(\.$deleted == false)
.filter(\.$createdAt < (arguments.before ?? Date.now))
.filter(\.$createdAt < before)
.sort(\.$createdAt, .descending)
.limit(arguments.count)
.limit(min(arguments.count, 10))
.all()
}
}
8 changes: 7 additions & 1 deletion Sources/App/GraphQL/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ let schema = try! Schema<Resolver, Request> {
Field("content", at: \.content)
Field("createdAt", at: \.createdAt?.timeIntervalSince1970)
Field("deleted", at: \.deleted)
Field("likes", at: \.likes.count)
}

Type(LikedPost.self) {
Expand All @@ -63,8 +64,13 @@ let schema = try! Schema<Resolver, Request> {
Field("posts", at: Resolver.getPostsByUser) {
Argument("creator", at: \.id)
}
Field("post", at: Resolver.getPostsByUser) {
Field("post", at: Resolver.getPostById) {
Argument("id", at: \.id)
Argument("likes", at: \.likes)
}
Field("recentPosts", at: Resolver.getRecentPosts) {
Argument("count", at: \.count)
Argument("before", at: \.before)
}
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/App/Models/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ final class Post: Model, Content {
@Field(key: "deleted")
var deleted: Bool

@Children(for: \.$post)
var likes: [LikedPost]

init() {
}

Expand Down

0 comments on commit 831cccf

Please sign in to comment.