83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package services
|
|
|
|
import (
|
|
"astra-events/config"
|
|
"astra-events/model"
|
|
"astra-events/pkg/api"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
)
|
|
|
|
func GetPerson(id uuid.UUID, header http.Header, c echo.Context) (*model.PersonalInformationApi, error) {
|
|
data, err, _ := api.GetRequest(
|
|
config.Env.MSHosts.PersonsHost+"/persons/"+id.String(),
|
|
header, c)
|
|
|
|
if err != nil {
|
|
return nil, errors.New("error Request to Personal Information Service")
|
|
}
|
|
|
|
pi := new(model.PersonalInformationApi)
|
|
|
|
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.PersonalInformationApi, header http.Header, c echo.Context) (*model.PersonalInformationApi, error) {
|
|
data, err, _ := api.PostRequest(
|
|
config.Env.MSHosts.PersonsHost+"/persons/",
|
|
person,
|
|
header,
|
|
c,
|
|
)
|
|
if err != nil {
|
|
return nil, errors.New("error Request to Personal Information Service")
|
|
}
|
|
|
|
pi := new(model.PersonalInformationApi)
|
|
|
|
err = json.Unmarshal(data, &pi)
|
|
|
|
if err != nil {
|
|
return nil, errors.New("bad Response from Personal Information Service")
|
|
}
|
|
|
|
return pi, nil
|
|
}
|
|
|
|
func GetPersons(ids []uuid.UUID, header http.Header, c echo.Context) (map[uuid.UUID]*model.PersonalInformationApi, error) {
|
|
var persons []model.PersonalInformationApi
|
|
personsMap := make(map[uuid.UUID]*model.PersonalInformationApi)
|
|
|
|
data, err, req := api.PostRequest(
|
|
config.Env.MSHosts.PersonsHost+"/persons/batch/",
|
|
&map[string][]uuid.UUID{
|
|
"ids": ids,
|
|
},
|
|
header, c)
|
|
|
|
if err != nil || req.StatusCode != 200 {
|
|
return nil, err
|
|
}
|
|
|
|
err = json.Unmarshal(data, &persons)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, person := range persons {
|
|
personsMap[person.ID] = &person
|
|
}
|
|
|
|
return personsMap, nil
|
|
}
|