update events

This commit is contained in:
andrusyakka
2023-08-01 16:48:54 +03:00
parent 46afe2e794
commit c55df47ada
5 changed files with 127 additions and 38 deletions

View File

@@ -10,7 +10,8 @@ type ServerConfig struct {
} }
type MSHosts struct { type MSHosts struct {
UsersHost string UsersHost string
PersonsHost string
} }
type Config struct { type Config struct {
@@ -25,7 +26,8 @@ func New() *Config {
ConnectionString: getEnv("CONNECTION_STRING", "host=localhost user=astra_events password=password dbname=astra_events_db port=5432 sslmode=disable TimeZone=Europe/Moscow"), ConnectionString: getEnv("CONNECTION_STRING", "host=localhost user=astra_events password=password dbname=astra_events_db port=5432 sslmode=disable TimeZone=Europe/Moscow"),
}, },
MSHosts: MSHosts{ MSHosts: MSHosts{
UsersHost: getEnv("ASTRA-USERS", "http://localhost:8081"), UsersHost: getEnv("ASTRA-USERS", "http://localhost:8083"),
PersonsHost: getEnv("ASTRA-PESONAL-INFORMATION", "http://localhost:8082"),
}, },
} }
} }

View File

@@ -11,10 +11,10 @@ import (
) )
func (h *Handler) CreateEvent(c echo.Context) (err error) { func (h *Handler) CreateEvent(c echo.Context) (err error) {
var person *model.PersonalInformation
event := new(model.CreateEvent) event := new(model.CreateEvent)
authorId, err := uuid.Parse(c.Request().Header.Get("UserId")) authorId, err := uuid.Parse(c.Request().Header.Get("UserId"))
orgId, err := uuid.Parse(c.Request().Header.Get("UserOrganizationId")) orgId, err := uuid.Parse(c.Request().Header.Get("UserOrganizationId"))
if err != nil { if err != nil {
return c.NoContent(http.StatusUnauthorized) return c.NoContent(http.StatusUnauthorized)
} }
@@ -23,19 +23,46 @@ func (h *Handler) CreateEvent(c echo.Context) (err error) {
return c.NoContent(http.StatusBadRequest) return c.NoContent(http.StatusBadRequest)
} }
usersMap, err := services.GetUsers([]uuid.UUID{event.EmployeeId, authorId}, c.Request().Header) if event.Person.ID.ID() != 0 {
person, err = services.GetPerson(event.Person.ID, c.Request().Header)
if err != nil || person.ID != event.Person.ID {
return c.NoContent(http.StatusBadRequest)
}
} else {
newPerson := model.PersonalInformationCreate{
FirstName: event.Person.FirstName,
LastName: event.Person.LastName,
Patronymic: event.Person.Patronymic,
Contacts: []model.Contact{
{
Category: "PHONE",
Value: event.Person.Phone,
},
},
}
if err != nil { person, err = services.CreatePerson(newPerson, c.Request().Header)
return c.HTML(http.StatusBadGateway, err.Error()) if err != nil || person.ID.ID() == 0 {
return c.NoContent(http.StatusBadRequest)
}
} }
employee := usersMap[event.EmployeeId] usersMap, err := services.GetUsers([]uuid.UUID{event.MedicId, authorId}, c.Request().Header)
if err != nil || len(usersMap) == 0 {
return c.HTML(http.StatusBadGateway, "Error get users")
}
medic := usersMap[event.MedicId]
author := usersMap[authorId] author := usersMap[authorId]
newEvent, err := utils.TypeConverter[model.Event](event) newEvent, err := utils.TypeConverter[model.Event](event)
newEvent.OrgId = orgId newEvent.OrgId = orgId
newEvent.MedicId = event.MedicId
newEvent.AuthorId = authorId newEvent.AuthorId = authorId
newEvent.PersonId = person.ID
newEvent.Person = *person
tx := h.DB.Create(&newEvent) tx := h.DB.Create(&newEvent)
@@ -44,7 +71,7 @@ func (h *Handler) CreateEvent(c echo.Context) (err error) {
} }
newEvent.Author = author newEvent.Author = author
newEvent.Employee = employee newEvent.Medic = medic
return c.JSON(http.StatusCreated, newEvent) return c.JSON(http.StatusCreated, newEvent)
} }
@@ -70,7 +97,7 @@ func (h *Handler) GetEvents(c echo.Context) (err error) {
ids := set.New[uuid.UUID](len(events) * 2) ids := set.New[uuid.UUID](len(events) * 2)
for _, event := range events { for _, event := range events {
ids.Insert(event.EmployeeId) ids.Insert(event.MedicId)
ids.Insert(event.AuthorId) ids.Insert(event.AuthorId)
} }
@@ -81,7 +108,7 @@ func (h *Handler) GetEvents(c echo.Context) (err error) {
for i, event := range events { for i, event := range events {
events[i].Author = usersMap[event.AuthorId] events[i].Author = usersMap[event.AuthorId]
events[i].Employee = usersMap[event.EmployeeId] events[i].Medic = usersMap[event.MedicId]
} }
return c.JSON(http.StatusOK, events) return c.JSON(http.StatusOK, events)
@@ -102,16 +129,16 @@ func (h *Handler) GetEvent(c echo.Context) error {
return c.NoContent(http.StatusNotFound) return c.NoContent(http.StatusNotFound)
} }
usersMap, err := services.GetUsers([]uuid.UUID{event.EmployeeId, event.AuthorId}, c.Request().Header) usersMap, err := services.GetUsers([]uuid.UUID{event.MedicId, event.AuthorId}, c.Request().Header)
if err != nil { if err != nil {
return c.HTML(http.StatusBadGateway, err.Error()) return c.HTML(http.StatusBadGateway, err.Error())
} }
employee := usersMap[event.EmployeeId] medic := usersMap[event.MedicId]
author := usersMap[event.AuthorId] author := usersMap[event.AuthorId]
event.Employee = employee event.Medic = medic
event.Author = author event.Author = author
return c.JSON(http.StatusOK, event) return c.JSON(http.StatusOK, event)

View File

@@ -2,35 +2,27 @@ package model
import ( import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/lib/pq"
"gorm.io/gorm" "gorm.io/gorm"
"time" "time"
) )
type Event struct { type Event struct {
gorm.Model gorm.Model
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primarykey"` ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primarykey"`
Start time.Time `json:"start"` Start time.Time `json:"start"`
End time.Time `json:"end"` End time.Time `json:"end"`
Author User `json:"author" gorm:"-"` Author User `json:"author" gorm:"-"`
AuthorId uuid.UUID `json:"-"` AuthorId uuid.UUID `json:"-"`
Employee User `json:"employee" gorm:"-"` Medic User `json:"medic" gorm:"-"`
EmployeeId uuid.UUID `json:"-"` MedicId uuid.UUID `json:"-"`
Member PD `json:"member" gorm:"-"` Person PersonalInformation `json:"person" gorm:"-"`
MemberId uuid.UUID `json:"-"` PersonId uuid.UUID `json:"-"`
Status string `json:"status"` OrgId uuid.UUID `json:"-"`
MedicalCard Card `json:"medicalCard" gorm:"-"`
MedicalCardId uuid.UUID `json:"-"`
Services pq.Int32Array `json:"services" gorm:"type:integer[]"`
OrgId uuid.UUID `json:"-"`
} }
type CreateEvent struct { type CreateEvent struct {
Start time.Time `json:"start"` Start time.Time `json:"start"`
End time.Time `json:"end"` End time.Time `json:"end"`
EmployeeId uuid.UUID `json:"employee_id"` Person PersonalInformation `json:"person"`
MemberId uuid.UUID `json:"member_id"` MedicId uuid.UUID `json:"medic_id"`
Status string `json:"status"`
MedicalCardId uuid.UUID `json:"medical_card_id"`
Services []int32 `json:"services"`
} }

View File

@@ -1,7 +1,23 @@
package model package model
import "gorm.io/gorm" import "github.com/google/uuid"
type PD struct { type PersonalInformation struct {
gorm.Model ID uuid.UUID `json:"id,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Patronymic string `json:"patronymic,omitempty"`
Phone string `json:"phone,omitempty"`
}
type PersonalInformationCreate struct {
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Patronymic string `json:"patronymic,omitempty"`
Contacts []Contact `json:"contacts"`
}
type Contact struct {
Category string `json:"category"`
Value string `json:"value"`
} }

52
services/pd.go Normal file
View File

@@ -0,0 +1,52 @@
package services
import (
"astra-events/config"
"astra-events/model"
"astra-events/pkg/api"
"encoding/json"
"errors"
"github.com/google/uuid"
"net/http"
)
func GetPerson(id uuid.UUID, header http.Header) (*model.PersonalInformation, error) {
data, err, _ := api.GetRequest(
config.Env.MSHosts.PersonsHost+"/persons/"+id.String(),
header)
if err != nil {
return nil, errors.New("error Request to Personal Information Service")
}
pi := new(model.PersonalInformation)
err = json.Unmarshal(data, &pi)
if err != nil {
return nil, errors.New("bad Response from Personal Information Service")
}
return pi, nil
}
func CreatePerson(person model.PersonalInformationCreate, header http.Header) (*model.PersonalInformation, error) {
data, err := api.PostRequest(
config.Env.MSHosts.PersonsHost+"/persons/",
person,
header,
)
if err != nil {
return nil, errors.New("error Request to Personal Information Service")
}
pi := new(model.PersonalInformation)
err = json.Unmarshal(data, &pi)
if err != nil {
return nil, errors.New("bad Response from Personal Information Service")
}
return pi, nil
}