MCPcopy Create free account
hub / github.com/kataras/iris / Create

Method Create

_examples/database/mysql/api/product_handler.go:74–95  ·  view source on GitHub ↗

Create adds a record to the database. Method: POST.

(ctx iris.Context)

Source from the content-addressed store, hash-verified

72// Create adds a record to the database.
73// Method: POST.
74func (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.

Callers

nothing calls this directly

Calls 12

PrimaryKeyMethod · 0.95
debugfFunction · 0.85
writeInternalServerErrorFunction · 0.85
ContextMethod · 0.80
RequestMethod · 0.80
StopWithJSONMethod · 0.80
newErrorFunction · 0.70
PathMethod · 0.65
StatusCodeMethod · 0.65
ReadJSONMethod · 0.45
InsertMethod · 0.45
JSONMethod · 0.45

Tested by

no test coverage detected