Skip to content

Commit

Permalink
it doesnt work yet
Browse files Browse the repository at this point in the history
  • Loading branch information
BoogieMonster1O1 committed Jul 6, 2024
1 parent 67f2ea8 commit 19001bb
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 5 deletions.
1 change: 0 additions & 1 deletion Sources/App/GraphQL/Query/Resolver+GetConfessions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ extension Resolver {
.first()
}
}

63 changes: 63 additions & 0 deletions Sources/App/GraphQL/Query/Search/Resolver+Search.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Resolver+Search.swift
//
//
// Created by Shrish Deshpande on 06/07/24.
//

import Vapor
import Fluent
import FluentPostgresDriver

struct SearchQueryArgs: Codable {
let query: String
}

fileprivate struct MetaRegisteredUser: Codable, PostgresDecodable {
let id: Int
let collegeid: String
let name: String
let phone: String
let email: String
let personal_email: String
let branch: String
let gender: String
let pronouns: String?
let date_registered: Date?
let bio: String?
let intake_year: Int
let avatar_hash: String?

func convert() -> RegisteredUser {
let user: RegisteredUser = .init(collegeId: self.collegeid, name: self.name, phone: self.phone, email: self.email, personalEmail: self.personal_email, branch: self.branch, gender: self.gender, pronouns: self.pronouns, bio: self.bio, intakeYear: self.intake_year , id: self.id)
user.dateRegistered = self.date_registered
user.avatarHash = self.avatar_hash
return user
}
}

extension Resolver {
func search(request: Request, arguments: SearchQueryArgs) async throws -> [any SearchResult] {
do {
let pgdb = request.db as! PostgresDatabase
let search = arguments.query

let userResult = try await pgdb.query("SELECT * FROM \"registeredUsers\" WHERE tsv @@ to_tsquery($1)", [.init(string: search)]).get()
let users = try userResult.rows.map { try $0.decode(MetaRegisteredUser.self) }.map { $0.convert() }


//let postResult = try await pgdb.query("SELECT * FROM posts WHERE tsv @@ to_tsquery($1)", [.init(string: search)]).get()
//let posts = try postResult.rows.map { try $0.decode(Post.self) }

//let confessionResult = try await pgdb.query("SELECT * FROM confessions WHERE tsv @@ to_tsquery($1)", [.init(string: search)]).get()
//let confessions = try confessionResult.rows.map { try $0.decode(Confession.self) }

let searchResults: [any SearchResult] = users.map { $0 }// + posts.map { $0 } + confessions.map { $0 }
return searchResults.sorted { $0.createdAt! > $1.createdAt! }
} catch {
request.logger.error("\(String(reflecting: error))")
request.logger.error("Error while searching: \(error)")
throw Abort(.internalServerError)
}
}
}
26 changes: 26 additions & 0 deletions Sources/App/GraphQL/Query/Search/SearchResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// SearchResult.swift
//
//
// Created by Shrish Deshpande on 06/07/24.
//

import Foundation

protocol SearchResult {
var createdAt: Date? {
get
}
}

extension RegisteredUser: SearchResult {
var createdAt: Date? {
self.dateRegistered
}
}

extension Post: SearchResult {
}

extension Confession: SearchResult {
}
7 changes: 6 additions & 1 deletion Sources/App/GraphQL/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ let schema = try! Graphiti.Schema<Resolver, Request> {
Field("createdAt", at: \.createdAt?.timeIntervalSince1970)
Field("type", at: \.type)
}

Union(SearchResult.self, members: RegisteredUser.self, Post.self, Confession.self)

Query {
Field("users", at: Resolver.getAllRegisteredUsers)
Expand Down Expand Up @@ -158,8 +160,11 @@ let schema = try! Graphiti.Schema<Resolver, Request> {
Argument("id", at: \.id)
}
Field("latestConfession", at: Resolver.getLatestConfession)
Field<Resolver, Request, [SearchResult], SearchQueryArgs>("search", at: Resolver.search) {
Argument("query", at: \.query)
}
}

Mutation {
Field("editProfile", at: Resolver.editProfile) {
Argument("bio", at: \.bio)
Expand Down
3 changes: 2 additions & 1 deletion Sources/App/Models/Confession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import Vapor
import Fluent
import Foundation
import FluentPostgresDriver

final class Confession: Model, Content {
final class Confession: Model, Content, PostgresDecodable {
static var schema: String = "confessions"

@ID(custom: "id", generatedBy: .user)
Expand Down
3 changes: 2 additions & 1 deletion Sources/App/Models/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import Vapor
import Fluent
import Foundation
import FluentPostgresDriver

final class Post: Model, Content {
final class Post: Model, Content, PostgresDecodable {
public static let schema = "posts"

@ID(custom: "id", generatedBy: .user)
Expand Down
3 changes: 2 additions & 1 deletion Sources/App/Models/RegisteredUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import Vapor
import Fluent
import FluentPostgresDriver

public final class RegisteredUser: Model, Content {
public final class RegisteredUser: Model, Content, PostgresDecodable {
public static let schema = "registeredUsers"

/// The user's ID
Expand Down

0 comments on commit 19001bb

Please sign in to comment.