Skip to content

Instantly share code, notes, and snippets.

@chebykinn
Created February 13, 2023 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chebykinn/6f88c03680167c4dc286f1b6226341f9 to your computer and use it in GitHub Desktop.
Save chebykinn/6f88c03680167c4dc286f1b6226341f9 to your computer and use it in GitHub Desktop.
Implementation of POST todo handler
// TodosPost - Add new todo note
func (s *TodosApiService) TodosPost(
ctx *core.MifyRequestContext, todoNoteCreateRequest openapi.TodoNoteCreateRequest) (openapi.ServiceResponse, error) {
todoSvc := apputil.GetServiceExtra(ctx.ServiceContext()).TodoService
note, err := todoSvc.CreateTodo(ctx, domain.TodoNote{
Title: todoNoteCreateRequest.Title,
Description: todoNoteCreateRequest.Description,
})
if err != nil {
// Pass some user friendly error to user instead of internal one,
// which will be available in service logs. Since we don't expect
// any errors here, we return Internal Server Error, which is catch-all
// for all unknown bad things. These kind of errors will be reported in
// metrics and it's easy to set alerts for them.
return openapi.Response(http.StatusInternalServerError, openapi.Error{
Code: strconv.Itoa(http.StatusInternalServerError),
Message: "Failed to create 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