add new methods and update current

This commit is contained in:
andrusyakka
2023-08-17 17:08:34 +03:00
parent 8bfbee4fe2
commit e007d66f51
4 changed files with 140 additions and 17 deletions

View File

@@ -6,15 +6,10 @@ import (
"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)
}
id := c.Param("id")
position, err := h.GetPosition(id)
@@ -25,7 +20,24 @@ func (h *Handler) GetPositionHandler(c echo.Context) error {
return c.JSON(http.StatusOK, position)
}
func (h *Handler) GetPosition(id int) (*model.Position, error) {
func (h *Handler) GetPositions(c echo.Context) error {
var positions []model.Position
h.DB.Find(&positions)
return c.JSON(http.StatusOK, positions)
}
func (h *Handler) GetPositionsByOrg(c echo.Context) error {
id := c.Param("id")
var positions []model.Position
h.DB.Where("organization_id = ?", id).Find(&positions)
return c.JSON(http.StatusOK, positions)
}
func (h *Handler) GetPosition(id string) (*model.Position, error) {
var position model.Position
r := h.DB.Find(&position, id)
@@ -54,3 +66,52 @@ func (h *Handler) CreatePosition(c echo.Context) (err error) {
return c.JSON(http.StatusCreated, newPosition)
}
func (h *Handler) DeletePosition(c echo.Context) error {
id := c.Param("id")
tx := h.DB.Model(model.Position{}).Delete(id)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusNotFound)
}
return c.NoContent(http.StatusOK)
}
func (h *Handler) UpdatePosition(c echo.Context) (err error) {
position := new(model.PositionApi)
var newPosition model.Position
id := c.Param("id")
if err = c.Bind(position); err != nil {
return c.NoContent(http.StatusBadRequest)
}
tx := h.DB.Find(&newPosition, id)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusNotFound)
}
if position.RightMask != 0 {
newPosition.RightMask = position.RightMask
}
if position.Name != "" {
newPosition.Name = position.Name
}
if position.OrganizationID.ID() != 0 {
newPosition.OrganizationID = position.OrganizationID
}
tx = tx.Save(&newPosition)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusBadGateway)
}
return c.JSON(http.StatusCreated, newPosition)
}