Skip to content

Instantly share code, notes, and snippets.

@chebykinn
Created February 13, 2023 02:32
Show Gist options
  • Save chebykinn/98d2d051671dfbefb464b1f59b159d15 to your computer and use it in GitHub Desktop.
Save chebykinn/98d2d051671dfbefb464b1f59b159d15 to your computer and use it in GitHub Desktop.
PUT, DELETE, GET handlers implementation for todo service
// TodosIdDelete - Delete todo note by id
func (s *TodosIdApiService) TodosIdDelete(ctx *core.MifyRequestContext, id int64) (openapi.ServiceResponse, error) {
todoSvc := apputil.GetServiceExtra(ctx.ServiceContext()).TodoService
if err := todoSvc.DeleteTodo(ctx, id); err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.Error{
Code: strconv.Itoa(http.StatusInternalServerError),
Message: "Failed to delete todo note",
}), err
}
// Return empty response
return openapi.Response(200, map[string]interface{}{}), nil
}
// TodosPut - Update todo note
func (s *TodosIdApiService) TodosIdPut(
ctx *core.MifyRequestContext, id int64, todoNoteUpdateRequest openapi.TodoNoteUpdateRequest) (openapi.ServiceResponse, error) {
todoSvc := apputil.GetServiceExtra(ctx.ServiceContext()).TodoService
note, err := todoSvc.UpdateTodo(ctx, domain.TodoNote{
ID: id,
Title: todoNoteUpdateRequest.Title,
Description: todoNoteUpdateRequest.Description,
IsCompleted: todoNoteUpdateRequest.IsCompleted,
})
if err != nil && errors.Is(err, storage.ErrTodoNoteNotFound) {
// Return not found error
return openapi.Response(http.StatusNotFound, openapi.Error{
Code: strconv.Itoa(http.StatusNotFound),
Message: fmt.Sprintf("Unable to find todo note by id: %d", id),
}), err
}
if err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.Error{
Code: strconv.Itoa(http.StatusInternalServerError),
Message: "Failed to update todo note",
}), err
}
return openapi.Response(http.StatusOK, handlers.MakeAPITodoNote(note)), nil
}
// TodosIdGet - Get todo note by id
func (s *TodosIdApiService) TodosIdGet(ctx *core.MifyRequestContext, id int64) (openapi.ServiceResponse, error) {
todoSvc := apputil.GetServiceExtra(ctx.ServiceContext()).TodoService
note, err := todoSvc.GetTodo(ctx, id)
if err != nil && errors.Is(err, storage.ErrTodoNoteNotFound) {
// Return not found error
return openapi.Response(http.StatusNotFound, openapi.Error{
Code: strconv.Itoa(http.StatusNotFound),
Message: fmt.Sprintf("Unable to find todo note by id: %d", id),
}), err
}
if err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.Error{
Code: strconv.Itoa(http.StatusInternalServerError),
Message: "Failed to get todo note",
}), err
}
return openapi.Response(http.StatusOK, handlers.MakeAPITodoNote(note)), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment