add new methods and update current
This commit is contained in:
@@ -32,3 +32,47 @@ func (h *Handler) GetOrganizations(c echo.Context) error {
|
|||||||
|
|
||||||
return c.JSON(http.StatusOK, organizations)
|
return c.JSON(http.StatusOK, organizations)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) DeleteOrganization(c echo.Context) error {
|
||||||
|
id := c.Param("id")
|
||||||
|
|
||||||
|
tx := h.DB.Model(model.Organization{}).Delete(id)
|
||||||
|
|
||||||
|
if tx.RowsAffected == 0 {
|
||||||
|
return c.NoContent(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.NoContent(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) UpdateOrganization(c echo.Context) (err error) {
|
||||||
|
id := c.Param("id")
|
||||||
|
organization := new(model.OrganizationApi)
|
||||||
|
var newOrganization model.Organization
|
||||||
|
|
||||||
|
if err = c.Bind(organization); err != nil {
|
||||||
|
return c.NoContent(http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
tx := h.DB.Find(&newOrganization, id)
|
||||||
|
|
||||||
|
if tx.RowsAffected == 0 {
|
||||||
|
return c.NoContent(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
if organization.Name != "" {
|
||||||
|
newOrganization.Name = organization.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
if organization.ParentID.ID() != 0 {
|
||||||
|
newOrganization.ParentID = organization.ParentID
|
||||||
|
}
|
||||||
|
|
||||||
|
tx = tx.Save(&newOrganization)
|
||||||
|
|
||||||
|
if tx.RowsAffected == 0 {
|
||||||
|
return c.NoContent(http.StatusBadGateway)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.NoContent(http.StatusOK)
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,15 +6,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handler) GetPositionHandler(c echo.Context) error {
|
func (h *Handler) GetPositionHandler(c echo.Context) error {
|
||||||
id, err := strconv.Atoi(c.Param("id"))
|
id := c.Param("id")
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return c.NoContent(http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
|
|
||||||
position, err := h.GetPosition(id)
|
position, err := h.GetPosition(id)
|
||||||
|
|
||||||
@@ -25,7 +20,24 @@ func (h *Handler) GetPositionHandler(c echo.Context) error {
|
|||||||
return c.JSON(http.StatusOK, position)
|
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
|
var position model.Position
|
||||||
|
|
||||||
r := h.DB.Find(&position, id)
|
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)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -92,21 +92,33 @@ func (h *Handler) GetUsers(c echo.Context) error {
|
|||||||
func (h *Handler) UpdateUser(c echo.Context) (err error) {
|
func (h *Handler) UpdateUser(c echo.Context) (err error) {
|
||||||
var user = new(model.User)
|
var user = new(model.User)
|
||||||
var apiUser = new(model.APIUser)
|
var apiUser = new(model.APIUser)
|
||||||
tx := h.DB
|
|
||||||
|
|
||||||
id := c.Param("id")
|
|
||||||
tx = tx.Find(&user, id)
|
|
||||||
|
|
||||||
if tx.RowsAffected == 0 {
|
|
||||||
return c.NoContent(http.StatusNotFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = c.Bind(apiUser); err != nil {
|
if err = c.Bind(apiUser); err != nil {
|
||||||
return c.NoContent(http.StatusBadRequest)
|
return c.NoContent(http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
user.FirstName = apiUser.FirstName
|
id := c.Param("id")
|
||||||
user.LastName = apiUser.LastName
|
tx := h.DB.Find(&user, id)
|
||||||
|
|
||||||
|
if tx.RowsAffected == 0 {
|
||||||
|
return c.NoContent(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
if apiUser.FirstName != "" {
|
||||||
|
user.FirstName = apiUser.FirstName
|
||||||
|
}
|
||||||
|
if apiUser.LastName != "" {
|
||||||
|
user.LastName = apiUser.LastName
|
||||||
|
}
|
||||||
|
if apiUser.PositionID.ID() != 0 {
|
||||||
|
user.PositionID = apiUser.PositionID
|
||||||
|
}
|
||||||
|
if apiUser.Role != "" {
|
||||||
|
user.Role = apiUser.Role
|
||||||
|
}
|
||||||
|
if apiUser.Password != "" {
|
||||||
|
user.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(apiUser.Password)))
|
||||||
|
}
|
||||||
|
|
||||||
tx = h.DB.Save(&user)
|
tx = h.DB.Save(&user)
|
||||||
|
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -53,9 +53,15 @@ func main() {
|
|||||||
e.GET("/health", h.Health)
|
e.GET("/health", h.Health)
|
||||||
|
|
||||||
e.GET("/positions/:id", h.GetPositionHandler, mw.CheckAdminRole)
|
e.GET("/positions/:id", h.GetPositionHandler, mw.CheckAdminRole)
|
||||||
|
e.DELETE("/positions/:id", h.DeletePosition, mw.CheckAdminRole)
|
||||||
|
e.PATCH("/positions/:id", h.UpdatePosition, mw.CheckAdminRole)
|
||||||
|
e.GET("/positions", h.GetPositions, mw.CheckAdminRole)
|
||||||
|
e.GET("/organizations/:id/positions", h.GetPositionsByOrg, mw.CheckAdminRole)
|
||||||
e.POST("/positions", h.CreatePosition, mw.CheckAdminRole)
|
e.POST("/positions", h.CreatePosition, mw.CheckAdminRole)
|
||||||
|
|
||||||
e.GET("/organizations", h.GetOrganizations, mw.CheckAdminRole)
|
e.GET("/organizations", h.GetOrganizations, mw.CheckAdminRole)
|
||||||
|
e.DELETE("/organizations/:id", h.GetOrganizations, mw.CheckAdminRole)
|
||||||
|
e.PATCH("/organizations/:id", h.UpdateOrganization, mw.CheckAdminRole)
|
||||||
e.POST("/organizations", h.CreateOrganization, mw.CheckAdminRole)
|
e.POST("/organizations", h.CreateOrganization, mw.CheckAdminRole)
|
||||||
|
|
||||||
e.Logger.Fatal(e.Start(":" + config.Env.Server.Port))
|
e.Logger.Fatal(e.Start(":" + config.Env.Server.Port))
|
||||||
|
|||||||
Reference in New Issue
Block a user