first commit
This commit is contained in:
71
handler/event.go
Normal file
71
handler/event.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"astra-events/model"
|
||||
"astra-events/services"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (h *Handler) CreateEvent(c echo.Context) (err error) {
|
||||
newEvent := new(model.Event)
|
||||
event := new(model.CreateEvent)
|
||||
authorId, err := strconv.Atoi(c.Request().Header.Get("UserId"))
|
||||
|
||||
if err != nil {
|
||||
return c.NoContent(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
if err = c.Bind(event); err != nil {
|
||||
return c.NoContent(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
// TODO: Add gorutine
|
||||
employee, err := services.GetUser(event.EmployeeId, c.Request().Header)
|
||||
if err != nil {
|
||||
return c.HTML(http.StatusBadGateway, err.Error())
|
||||
}
|
||||
author, err := services.GetUser(uint(authorId), c.Request().Header)
|
||||
if err != nil {
|
||||
return c.HTML(http.StatusBadGateway, err.Error())
|
||||
}
|
||||
|
||||
newEvent.Start = event.Start
|
||||
newEvent.End = event.End
|
||||
newEvent.AuthorId = uint(authorId)
|
||||
newEvent.EmployeeId = event.EmployeeId
|
||||
newEvent.MemberId = event.MemberId
|
||||
newEvent.MedicalCardId = event.MedicalCardId
|
||||
newEvent.Status = event.Status
|
||||
|
||||
h.DB.Begin()
|
||||
|
||||
tx := h.DB.Create(&newEvent)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
h.DB.Rollback()
|
||||
return c.NoContent(http.StatusBadGateway)
|
||||
}
|
||||
|
||||
eventsServices := []model.EventsServices{}
|
||||
|
||||
for _, service := range event.Services {
|
||||
eventsServices = append(eventsServices, model.EventsServices{EventId: newEvent.ID, ServiceId: service})
|
||||
}
|
||||
|
||||
tx = h.DB.Create(eventsServices)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
h.DB.Rollback()
|
||||
return c.NoContent(http.StatusBadGateway)
|
||||
}
|
||||
|
||||
h.DB.Commit()
|
||||
|
||||
newEvent.Services = event.Services
|
||||
newEvent.Author = *author
|
||||
newEvent.Employee = *employee
|
||||
|
||||
return c.JSON(http.StatusCreated, newEvent)
|
||||
}
|
||||
Reference in New Issue
Block a user