forked from mirrors/homebox
move to nuxt
This commit is contained in:
parent
890eb55d27
commit
26ecb5a9d4
93 changed files with 5273 additions and 4749 deletions
|
@ -2,44 +2,75 @@ package services
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/content/backend/ent"
|
||||
"github.com/hay-kot/content/backend/internal/repo"
|
||||
"github.com/hay-kot/content/backend/internal/services/mappers"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotOwner = errors.New("not owner")
|
||||
)
|
||||
|
||||
type LocationService struct {
|
||||
repos *repo.AllRepos
|
||||
}
|
||||
|
||||
func ToLocationOut(location *ent.Location, err error) (*types.LocationOut, error) {
|
||||
return &types.LocationOut{
|
||||
ID: location.ID,
|
||||
GroupID: location.Edges.Group.ID,
|
||||
Name: location.Name,
|
||||
Description: location.Description,
|
||||
CreatedAt: location.CreatedAt,
|
||||
UpdatedAt: location.UpdatedAt,
|
||||
}, err
|
||||
func (svc *LocationService) GetOne(ctx context.Context, groupId uuid.UUID, id uuid.UUID) (*types.LocationOut, error) {
|
||||
location, err := svc.repos.Locations.Get(ctx, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if location.Edges.Group.ID != groupId {
|
||||
return nil, ErrNotOwner
|
||||
}
|
||||
|
||||
return mappers.ToLocationOut(location), nil
|
||||
}
|
||||
|
||||
func (svc *LocationService) Create(ctx context.Context, groupId uuid.UUID, data types.LocationCreate) (*types.LocationOut, error) {
|
||||
location, err := svc.repos.Locations.Create(ctx, groupId, data)
|
||||
return ToLocationOut(location, err)
|
||||
}
|
||||
|
||||
func (svc *LocationService) GetAll(ctx context.Context, groupId uuid.UUID) ([]*types.LocationOut, error) {
|
||||
func (svc *LocationService) GetAll(ctx context.Context, groupId uuid.UUID) ([]*types.LocationSummary, error) {
|
||||
locations, err := svc.repos.Locations.GetAll(ctx, groupId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
locationsOut := make([]*types.LocationOut, len(locations))
|
||||
locationsOut := make([]*types.LocationSummary, len(locations))
|
||||
for i, location := range locations {
|
||||
locationOut, _ := ToLocationOut(location, nil)
|
||||
locationsOut[i] = locationOut
|
||||
locationsOut[i] = mappers.ToLocationSummary(location)
|
||||
}
|
||||
|
||||
return locationsOut, nil
|
||||
}
|
||||
|
||||
func (svc *LocationService) Create(ctx context.Context, groupId uuid.UUID, data types.LocationCreate) (*types.LocationSummary, error) {
|
||||
location, err := svc.repos.Locations.Create(ctx, groupId, data)
|
||||
return mappers.ToLocationSummaryErr(location, err)
|
||||
}
|
||||
|
||||
func (svc *LocationService) Delete(ctx context.Context, groupId uuid.UUID, id uuid.UUID) error {
|
||||
location, err := svc.repos.Locations.Get(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if location.Edges.Group.ID != groupId {
|
||||
return ErrNotOwner
|
||||
}
|
||||
|
||||
return svc.repos.Locations.Delete(ctx, id)
|
||||
}
|
||||
|
||||
func (svc *LocationService) Update(ctx context.Context, groupId uuid.UUID, data types.LocationUpdate) (*types.LocationOut, error) {
|
||||
location, err := svc.repos.Locations.Get(ctx, data.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if location.Edges.Group.ID != groupId {
|
||||
return nil, ErrNotOwner
|
||||
}
|
||||
|
||||
return mappers.ToLocationOutErr(svc.repos.Locations.Update(ctx, data))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue