Created
July 12, 2020 12:03
-
-
Save alfianlosari/2464da846fab04bc340d8a5983e1405f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| struct TodoLamdaHandler: EventLoopLambdaHandler { | |
| //... | |
| // Main handler | |
| func handle(context: Lambda.Context, event: APIGateway.Request) -> EventLoopFuture<APIGateway.Response> { | |
| guard let handler = Handler.current else { | |
| return context.eventLoop.makeSucceededFuture(APIGateway.Response(with: APIError.requestError, statusCode: .badRequest)) | |
| } | |
| switch handler { | |
| case .create: | |
| return handleCreate(context: context, event: event) | |
| case .read: | |
| return handleRead(context: context, event: event) | |
| case .update: | |
| return handleUpdate(context: context, event: event) | |
| case .delete: | |
| return handleDelete(context: context, event: event) | |
| case .list: | |
| return handleList(context: context, event: event) | |
| } | |
| } | |
| // Handling Create Todo | |
| func handleCreate(context: Lambda.Context, event: APIGateway.Request) -> EventLoopFuture<APIGateway.Response> { | |
| guard let newTodo: Todo = try? event.bodyObject() else { | |
| return context.eventLoop.makeSucceededFuture(APIGateway.Response(with: APIError.requestError, statusCode: .badRequest)) | |
| } | |
| return todoService.createTodo(todo: newTodo) | |
| .map { todo in | |
| APIGateway.Response(with: todo, statusCode: .ok) | |
| }.flatMapError { | |
| self.catchError(context: context, error: $0) | |
| } | |
| } | |
| // Handling Read Todo | |
| func handleRead(context: Lambda.Context, event: APIGateway.Request) -> EventLoopFuture<APIGateway.Response> { | |
| guard let id = event.pathParameters?[Todo.DynamoDBField.id] else { | |
| return context.eventLoop.makeSucceededFuture(APIGateway.Response(with: APIError.requestError, statusCode: .notFound)) | |
| } | |
| return todoService.getTodo(id: id) | |
| .map { todo in | |
| APIGateway.Response(with: todo, statusCode: .ok) | |
| }.flatMapError { | |
| self.catchError(context: context, error: $0) | |
| } | |
| } | |
| // Handling Update Todo | |
| func handleUpdate(context: Lambda.Context, event: APIGateway.Request) -> EventLoopFuture<APIGateway.Response> { | |
| guard let updatedTodo: Todo = try? event.bodyObject() else { | |
| return context.eventLoop.makeSucceededFuture(APIGateway.Response(with: APIError.requestError, statusCode: .badRequest)) | |
| } | |
| return todoService.updateTodo(todo: updatedTodo) | |
| .map { todo in | |
| APIGateway.Response(with: todo, statusCode: .ok) | |
| }.flatMapError { | |
| self.catchError(context: context, error: $0) | |
| } | |
| } | |
| // Handling Delete Todo | |
| func handleDelete(context: Lambda.Context, event: APIGateway.Request) -> EventLoopFuture<APIGateway.Response> { | |
| guard let id = event.pathParameters?[Todo.DynamoDBField.id] else { | |
| return context.eventLoop.makeSucceededFuture(APIGateway.Response(with: APIError.requestError, statusCode: .badRequest)) | |
| } | |
| return todoService.deleteTodo(id: id) | |
| .map { | |
| APIGateway.Response(with: EmptyResponse(), statusCode: .ok) | |
| }.flatMapError { | |
| self.catchError(context: context, error: $0) | |
| } | |
| } | |
| // Handling Todos List | |
| func handleList(context: Lambda.Context, event: APIGateway.Request) -> EventLoopFuture<APIGateway.Response> { | |
| return todoService.getAllTodos() | |
| .map { todos in | |
| APIGateway.Response(with: todos, statusCode: .ok) | |
| }.flatMapError { | |
| self.catchError(context: context, error: $0) | |
| } | |
| } | |
| // Catch Error | |
| func catchError(context: Lambda.Context, error: Error) -> EventLoopFuture<APIGateway.Response> { | |
| let response = APIGateway.Response(with: error, statusCode: .notFound) | |
| return context.eventLoop.makeSucceededFuture(response) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment