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

Method Create

_examples/database/mysql/api/category_handler.go:83–104  ·  view source on GitHub ↗

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

(ctx iris.Context)

Source from the content-addressed store, hash-verified

81// Create adds a record to the database.
82// Method: POST.
83func (h *CategoryHandler) Create(ctx iris.Context) {
84 var cat entity.Category
85 if err := ctx.ReadJSON(&cat); err != nil {
86 return
87 }
88
89 id, err := h.service.Insert(ctx.Request().Context(), cat)
90 if err != nil {
91 if err == sql.ErrUnprocessable {
92 ctx.StopWithJSON(iris.StatusUnprocessableEntity, newError(iris.StatusUnprocessableEntity, ctx.Request().Method, ctx.Path(), "required fields are missing"))
93 return
94 }
95
96 debugf("CategoryHandler.Create(DB): %v", err)
97 writeInternalServerError(ctx)
98 return
99 }
100
101 // Send 201 with body of {"id":$last_inserted_id"}.
102 ctx.StatusCode(iris.StatusCreated)
103 ctx.JSON(iris.Map{cat.PrimaryKey(): id})
104}
105
106// Update performs a full-update of a record in the database.
107// 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