-
Notifications
You must be signed in to change notification settings - Fork 5
/
put.go
47 lines (33 loc) · 937 Bytes
/
put.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Copyright © 2023 Codoworks
Author: Dexter Codo
Contact: dexter.codo@gmail.com
*/
package cats
import (
"net/http"
"github.com/codoworks/go-boilerplate/pkg/api/handlers"
"github.com/codoworks/go-boilerplate/pkg/api/helpers"
"github.com/codoworks/go-boilerplate/pkg/db/models"
"github.com/codoworks/go-boilerplate/pkg/utils/constants"
"github.com/labstack/echo/v4"
)
func Put(c echo.Context) error {
id := c.Param("id")
if id == "" {
return helpers.Error(c, constants.ERROR_ID_NOT_FOUND, nil)
}
f := &models.CatForm{}
if err := c.Bind(f); err != nil {
return helpers.Error(c, constants.ERROR_BINDING_BODY, err)
}
if err := helpers.Validate(f); err != nil {
return c.JSON(http.StatusBadRequest, handlers.ValidationErrors(err))
}
m := f.MapToModel()
m.ID = id
if err := m.Update(); err != nil {
return helpers.Error(c, err, nil)
}
return c.JSON(http.StatusOK, handlers.Success(m.MapToForm()))
}