136 lines
3.1 KiB
Go
136 lines
3.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"astra-events/model"
|
|
"astra-events/pkg/utils"
|
|
"astra-events/services"
|
|
"github.com/hashicorp/go-set"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func (h *Handler) CreateEvent(c echo.Context) (err error) {
|
|
event := new(model.CreateEvent)
|
|
authorId, err := strconv.Atoi(c.Request().Header.Get("UserId"))
|
|
orgId, err := strconv.Atoi(c.Request().Header.Get("UserOrganizationId"))
|
|
|
|
if err != nil {
|
|
return c.NoContent(http.StatusUnauthorized)
|
|
}
|
|
|
|
if err = c.Bind(event); err != nil {
|
|
return c.NoContent(http.StatusBadRequest)
|
|
}
|
|
|
|
usersMap, err := services.GetUsers([]uint{event.EmployeeId, uint(authorId)}, c.Request().Header)
|
|
|
|
if err != nil {
|
|
return c.HTML(http.StatusBadGateway, err.Error())
|
|
}
|
|
|
|
employee := usersMap[event.EmployeeId]
|
|
author := usersMap[uint(authorId)]
|
|
|
|
newEvent, err := utils.TypeConverter[model.Event](event)
|
|
|
|
newEvent.OrgId = uint(orgId)
|
|
newEvent.AuthorId = uint(authorId)
|
|
|
|
tx := h.DB.Create(&newEvent)
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return c.NoContent(http.StatusBadGateway)
|
|
}
|
|
|
|
newEvent.Author = author
|
|
newEvent.Employee = employee
|
|
|
|
return c.JSON(http.StatusCreated, newEvent)
|
|
}
|
|
|
|
func (h *Handler) GetEvents(c echo.Context) (err error) {
|
|
var events []model.Event
|
|
query := c.Request().URL.Query()
|
|
_, _, filterMap := utils.GetFilterParams(query)
|
|
orgId, err := strconv.Atoi(c.Request().Header.Get("UserOrganizationId"))
|
|
|
|
if err != nil {
|
|
return c.NoContent(http.StatusUnauthorized)
|
|
}
|
|
|
|
tx := h.DB.Where("org_id = ?", orgId)
|
|
|
|
if len(filterMap) > 0 {
|
|
tx = tx.Where("start >= ?", filterMap["start"]).Where("\"end\" <= ?", filterMap["end"])
|
|
}
|
|
|
|
tx.Find(&events)
|
|
|
|
ids := set.New[uint](len(events) * 2)
|
|
|
|
for _, event := range events {
|
|
ids.Insert(event.EmployeeId)
|
|
ids.Insert(event.AuthorId)
|
|
}
|
|
|
|
usersMap, err := services.GetUsers(ids.Slice(), c.Request().Header)
|
|
if err != nil {
|
|
return c.HTML(http.StatusBadGateway, err.Error())
|
|
}
|
|
|
|
for i, event := range events {
|
|
events[i].Author = usersMap[event.AuthorId]
|
|
events[i].Employee = usersMap[event.EmployeeId]
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, events)
|
|
}
|
|
|
|
func (h *Handler) GetEvent(c echo.Context) error {
|
|
var event model.Event
|
|
id := c.Param("id")
|
|
orgId, err := strconv.Atoi(c.Request().Header.Get("UserOrganizationId"))
|
|
|
|
if err != nil {
|
|
return c.NoContent(http.StatusUnauthorized)
|
|
}
|
|
|
|
tx := h.DB.Where("org_id = ?", orgId).First(&event, id)
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return c.NoContent(http.StatusNotFound)
|
|
}
|
|
|
|
usersMap, err := services.GetUsers([]uint{event.EmployeeId, event.AuthorId}, c.Request().Header)
|
|
|
|
if err != nil {
|
|
return c.HTML(http.StatusBadGateway, err.Error())
|
|
}
|
|
|
|
employee := usersMap[event.EmployeeId]
|
|
author := usersMap[event.AuthorId]
|
|
|
|
event.Employee = employee
|
|
event.Author = author
|
|
|
|
return c.JSON(http.StatusOK, event)
|
|
}
|
|
|
|
func (h *Handler) DeleteEvent(c echo.Context) error {
|
|
id := c.Param("id")
|
|
orgId, err := strconv.Atoi(c.Request().Header.Get("UserOrganizationId"))
|
|
|
|
if err != nil {
|
|
return c.NoContent(http.StatusUnauthorized)
|
|
}
|
|
|
|
tx := h.DB.Where("org_id = ?", orgId).Delete(&model.Event{}, id)
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return c.NoContent(http.StatusNotFound)
|
|
}
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
}
|