Create adds a record to the database. Method: POST.
(ctx iris.Context)
| 72 | // Create adds a record to the database. |
| 73 | // Method: POST. |
| 74 | func (h *ProductHandler) Create(ctx iris.Context) { |
| 75 | var product entity.Product |
| 76 | if err := ctx.ReadJSON(&product); err != nil { |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | id, err := h.service.Insert(ctx.Request().Context(), product) |
| 81 | if err != nil { |
| 82 | if err == sql.ErrUnprocessable { |
| 83 | ctx.StopWithJSON(iris.StatusUnprocessableEntity, newError(iris.StatusUnprocessableEntity, ctx.Request().Method, ctx.Path(), "required fields are missing")) |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | debugf("ProductHandler.Create(DB): %v", err) |
| 88 | writeInternalServerError(ctx) |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | // Send 201 with body of {"id":$last_inserted_id"}. |
| 93 | ctx.StatusCode(iris.StatusCreated) |
| 94 | ctx.JSON(iris.Map{product.PrimaryKey(): id}) |
| 95 | } |
| 96 | |
| 97 | // Update performs a full-update of a record in the database. |
| 98 | // Method: PUT. |
nothing calls this directly
no test coverage detected