add request to personal information

This commit is contained in:
kandrusyak
2023-08-03 00:53:18 +03:00
parent 15f51516af
commit 0364ad2d7d
3 changed files with 48 additions and 10 deletions

View File

@@ -0,0 +1,86 @@
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
}
func GetPersons(ids []uuid.UUID, header http.Header) (map[uuid.UUID]model.PersonalInformation, error) {
type BatchBody struct {
ids []uuid.UUID
}
var persons []model.PersonalInformation
personsMap := make(map[uuid.UUID]model.PersonalInformation)
jsonIds, err := json.Marshal(&BatchBody{
ids: ids,
})
data, err, req := api.PostRequest(
config.Env.MSHosts.PersonsHost+"/persons/batch/",
jsonIds,
header)
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
}