Skip to content

Commit

Permalink
require not get
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Jan 17, 2024
1 parent a18e372 commit 7df90ae
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sessions/Sources/App/Controllers/UserController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct UserController {
/// Login user and create session
@Sendable func login(_ request: HBRequest, context: SessionsContext) async throws -> HBResponse {
// get authenticated user and return
guard let user = context.auth.get(LoggedInUser.self) else { throw HBHTTPError(.unauthorized) }
let user = try context.auth.require(LoggedInUser.self)
// create session lasting 1 hour
let cookie = try await self.sessionStorage.save(session: user.id, expiresIn: .seconds(60))
return .init(status: .ok, headers: [.setCookie: cookie.description])
Expand All @@ -68,7 +68,7 @@ struct UserController {
/// Get current logged in user
@Sendable func current(_ request: HBRequest, context: SessionsContext) throws -> UserResponse {
// get authenticated user and return
guard let user = context.auth.get(LoggedInUser.self) else { throw HBHTTPError(.unauthorized) }
let user = try context.auth.require(LoggedInUser.self)
return UserResponse(id: user.id, name: user.name)
}
}
7 changes: 3 additions & 4 deletions todos-dynamodb/Sources/App/Controllers/TodoController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct TodoController: Sendable {
}

@Sendable func get(_ request: HBRequest, context: some HBRequestContext) async throws -> Todo? {
guard let id = context.parameters.get("id", as: String.self) else { throw HBHTTPError(.badRequest) }
let id = try context.parameters.require("id")
let input = DynamoDB.QueryInput(
consistentRead: true,
expressionAttributeValues: [":id": .s(id)],
Expand All @@ -72,8 +72,7 @@ struct TodoController: Sendable {

@Sendable func updateId(_ request: HBRequest, context: some HBRequestContext) async throws -> Todo {
var todo = try await request.decode(as: EditTodo.self, context: context)
guard let id = context.parameters.get("id", as: UUID.self) else { throw HBHTTPError(.badRequest) }
todo.id = id
todo.id = try context.parameters.require("id", as: UUID.self)
let input = DynamoDB.UpdateItemCodableInput(
conditionExpression: "attribute_exists(id)",
key: ["id"],
Expand Down Expand Up @@ -103,7 +102,7 @@ struct TodoController: Sendable {
}

@Sendable func deleteId(_ request: HBRequest, context: some HBRequestContext) async throws -> HTTPResponse.Status {
guard let id = context.parameters.get("id", as: String.self) else { throw HBHTTPError(.badRequest) }
let id = try context.parameters.require("id")

let input = DynamoDB.DeleteItemInput(
conditionExpression: "attribute_exists(id)",
Expand Down

0 comments on commit 7df90ae

Please sign in to comment.