feat: Better styling on web pages, bad-words check now has less edge cases.
This commit is contained in:
@@ -8,64 +8,59 @@ func routes(_ app: Application) throws {
|
||||
return output
|
||||
}
|
||||
|
||||
app.get("loggedIn") { req in
|
||||
guard let userIdString = req.session.data["userId"],
|
||||
let displayName = req.session.data["displayName"],
|
||||
let userId = UUID(uuidString: userIdString)
|
||||
else {
|
||||
return try await req.view.render("/")
|
||||
app.get("loggedIn") { req -> View in
|
||||
guard let user = try await req.currentUser(withProsAndCons: true) else {
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
guard let user = try await User.query(on: req.db)
|
||||
.filter(\.$id == userId)
|
||||
.with(\.$prosAndCons)
|
||||
.first()
|
||||
else {
|
||||
throw Abort(.unauthorized)
|
||||
}
|
||||
// let prosAndCons = try await user.$prosAndCons.get(on: req.db)
|
||||
return try await req.view.render(
|
||||
"loggedIn",
|
||||
LoggedInContext(name: displayName, prosAndCons: user.prosAndCons)
|
||||
LoggedInContext(name: user.displayName, prosAndCons: user.prosAndCons)
|
||||
)
|
||||
}
|
||||
|
||||
app.get("submitProOrCon") { req in
|
||||
let params = try req.query.decode(SubmitProOrCon.self)
|
||||
guard let userIdString = req.session.data["userId"],
|
||||
let userId = UUID(uuidString: userIdString)
|
||||
else {
|
||||
guard let userId = req.userId else {
|
||||
throw Abort(.unauthorized)
|
||||
}
|
||||
try checkForBadWords(in: params.description)
|
||||
let proOrCon = ProCon(type: params.type, description: params.description, userId: userId)
|
||||
_ = try await req.db.transaction {
|
||||
proOrCon.save(on: $0)
|
||||
}
|
||||
.get()
|
||||
|
||||
try await proOrCon.save(on: req.db)
|
||||
return req.redirect(to: "loggedIn")
|
||||
}
|
||||
|
||||
app.get("login") { req in
|
||||
let params = try req.query.decode(LoginParams.self)
|
||||
req.logger.info("params: \(params)")
|
||||
try checkForBadWords(in: params.displayName)
|
||||
let user = User(displayName: params.displayName)
|
||||
try await user.save(on: req.db)
|
||||
req.session.data["userId"] = user.id?.uuidString
|
||||
return req.redirect(to: "loggedIn")
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
try checkForBadWords(in: params.displayName)
|
||||
} catch {
|
||||
throw Abort(.unauthorized, reason: "Stop using such naughty language.")
|
||||
private extension Request {
|
||||
|
||||
var userId: UUID? {
|
||||
guard let userIdString = session.data["userId"],
|
||||
let userId = UUID(uuidString: userIdString)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
return userId
|
||||
}
|
||||
|
||||
func currentUser(withProsAndCons: Bool) async throws -> User? {
|
||||
guard let userId = userId else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let user = User(displayName: params.displayName)
|
||||
_ = try await req.db.transaction {
|
||||
user.save(on: $0)
|
||||
}.get()
|
||||
|
||||
let userId = user.id?.uuidString ?? "nil"
|
||||
req.session.data["userId"] = userId
|
||||
req.session.data["displayName"] = user.displayName
|
||||
|
||||
// return try await req.view.render("loggedIn", ["name": user.displayName])
|
||||
return req.redirect(to: "loggedIn")
|
||||
var query = User.query(on: db).filter(\.$id == userId)
|
||||
if withProsAndCons {
|
||||
query = query.with(\.$prosAndCons)
|
||||
}
|
||||
return try await query.first()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user