-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67f2ea8
commit 19001bb
Showing
7 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,3 @@ extension Resolver { | |
.first() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters