add org structure
This commit is contained in:
53
handler/position.go
Normal file
53
handler/position.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"astra-users/model"
|
||||
"errors"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (h *Handler) GetPositionHandler(c echo.Context) error {
|
||||
id, err := strconv.Atoi(c.Param("id"))
|
||||
|
||||
if err != nil {
|
||||
return c.NoContent(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
position, err := h.GetPosition(id)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, position)
|
||||
}
|
||||
|
||||
func (h *Handler) GetPosition(id int) (*model.Position, error) {
|
||||
var position model.Position
|
||||
|
||||
r := h.DB.Find(&position, id)
|
||||
|
||||
if r.RowsAffected == 0 {
|
||||
return nil, errors.New("position not found")
|
||||
}
|
||||
|
||||
return &position, nil
|
||||
}
|
||||
|
||||
func (h *Handler) CreatePosition(c echo.Context) (err error) {
|
||||
position := new(model.Position)
|
||||
|
||||
if err = c.Bind(position); err != nil {
|
||||
return c.NoContent(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
tx := h.DB.Create(position)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusBadGateway)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, position)
|
||||
}
|
||||
Reference in New Issue
Block a user