add today endpoint
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.idea
|
||||
build
|
||||
.env
|
||||
test
|
||||
@@ -8,7 +8,9 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/hashicorp/go-set"
|
||||
"github.com/labstack/echo/v4"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// swagger:route POST /events/ Events CreateEvent
|
||||
@@ -91,7 +93,6 @@ func (h *Handler) CreateEvent(c echo.Context) (err error) {
|
||||
// 200: []Event
|
||||
|
||||
func (h *Handler) GetEvents(c echo.Context) (err error) {
|
||||
var events []model.Event
|
||||
query := c.Request().URL.Query()
|
||||
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
|
||||
|
||||
@@ -105,30 +106,29 @@ func (h *Handler) GetEvents(c echo.Context) (err error) {
|
||||
tx = tx.Where("start >= ?", query.Get("start")).Where("\"end\" <= ?", query.Get("end"))
|
||||
}
|
||||
|
||||
tx.Find(&events)
|
||||
return h.queryEvents(tx, c)
|
||||
}
|
||||
|
||||
userIds := set.New[uuid.UUID](len(events) * 2)
|
||||
personIds := set.New[uuid.UUID](len(events))
|
||||
// swagger:route GET /events/today Events GetEventsToday
|
||||
//
|
||||
// responses:
|
||||
// 200: []Event
|
||||
|
||||
for _, event := range events {
|
||||
userIds.Insert(event.MedicId)
|
||||
userIds.Insert(event.AuthorId)
|
||||
personIds.Insert(event.PersonId)
|
||||
}
|
||||
func (h *Handler) GetEventsToday(c echo.Context) (err error) {
|
||||
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
|
||||
|
||||
usersMap, err := services.GetUsers(userIds.Slice(), c.Request().Header)
|
||||
personsMap, err := services.GetPersons(personIds.Slice(), c.Request().Header)
|
||||
if err != nil {
|
||||
return c.HTML(http.StatusBadGateway, err.Error())
|
||||
return c.NoContent(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
for i, event := range events {
|
||||
events[i].Author = usersMap[event.AuthorId]
|
||||
events[i].Medic = usersMap[event.MedicId]
|
||||
events[i].Person = personsMap[event.PersonId]
|
||||
}
|
||||
tx := h.DB.Where("org_id = ?", orgId)
|
||||
|
||||
return c.JSON(http.StatusOK, events)
|
||||
currentDate := time.Now().Format(time.DateOnly)
|
||||
nextDate := time.Now().Add(24 * time.Hour).Format(time.DateOnly)
|
||||
|
||||
tx = tx.Where("start >= ?", currentDate).Where("\"end\" < ?", nextDate)
|
||||
|
||||
return h.queryEvents(tx, c)
|
||||
}
|
||||
|
||||
// swagger:route GET /events/{uuid} Events GetEvent
|
||||
@@ -187,3 +187,31 @@ func (h *Handler) DeleteEvent(c echo.Context) error {
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *Handler) queryEvents(tx *gorm.DB, c echo.Context) error {
|
||||
var events []model.Event
|
||||
tx.Find(&events)
|
||||
|
||||
userIds := set.New[uuid.UUID](len(events) * 2)
|
||||
personIds := set.New[uuid.UUID](len(events))
|
||||
|
||||
for _, event := range events {
|
||||
userIds.Insert(event.MedicId)
|
||||
userIds.Insert(event.AuthorId)
|
||||
personIds.Insert(event.PersonId)
|
||||
}
|
||||
|
||||
usersMap, err := services.GetUsers(userIds.Slice(), c.Request().Header)
|
||||
personsMap, err := services.GetPersons(personIds.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].Medic = usersMap[event.MedicId]
|
||||
events[i].Person = personsMap[event.PersonId]
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, events)
|
||||
}
|
||||
|
||||
@@ -43,6 +43,25 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/events/today": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"Events"
|
||||
],
|
||||
"operationId": "GetEvents",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Event",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/Event"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/events/{uuid}": {
|
||||
"get": {
|
||||
"tags": [
|
||||
@@ -68,9 +87,26 @@
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"Contact": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"category": {
|
||||
"type": "string",
|
||||
"x-go-name": "Category"
|
||||
},
|
||||
"value": {
|
||||
"type": "string",
|
||||
"x-go-name": "Value"
|
||||
}
|
||||
},
|
||||
"x-go-package": "astra-events/model"
|
||||
},
|
||||
"CreateEvent": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"start",
|
||||
"end",
|
||||
"person",
|
||||
"medic_id"
|
||||
],
|
||||
"properties": {
|
||||
@@ -115,8 +151,8 @@
|
||||
"$ref": "#/definitions/DeletedAt"
|
||||
},
|
||||
"ID": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
"type": "integer",
|
||||
"format": "uint64"
|
||||
},
|
||||
"UpdatedAt": {
|
||||
"type": "string",
|
||||
@@ -130,11 +166,16 @@
|
||||
"format": "date-time",
|
||||
"x-go-name": "End"
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"format": "uuid",
|
||||
"x-go-name": "ID"
|
||||
},
|
||||
"medic": {
|
||||
"$ref": "#/definitions/User"
|
||||
},
|
||||
"person": {
|
||||
"$ref": "#/definitions/PersonalInformation"
|
||||
"$ref": "#/definitions/PersonalInformationApi"
|
||||
},
|
||||
"start": {
|
||||
"type": "string",
|
||||
@@ -219,6 +260,40 @@
|
||||
},
|
||||
"x-go-package": "astra-events/model"
|
||||
},
|
||||
"PersonalInformationApi": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"birth_date": {
|
||||
"type": "string",
|
||||
"x-go-name": "BirthDate"
|
||||
},
|
||||
"contacts": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/Contact"
|
||||
},
|
||||
"x-go-name": "Contacts"
|
||||
},
|
||||
"first_name": {
|
||||
"type": "string",
|
||||
"x-go-name": "FirstName"
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"format": "uuid",
|
||||
"x-go-name": "ID"
|
||||
},
|
||||
"last_name": {
|
||||
"type": "string",
|
||||
"x-go-name": "LastName"
|
||||
},
|
||||
"patronymic": {
|
||||
"type": "string",
|
||||
"x-go-name": "Patronymic"
|
||||
}
|
||||
},
|
||||
"x-go-package": "astra-events/model"
|
||||
},
|
||||
"User": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
Reference in New Issue
Block a user