forked from mirrors/homebox
31b34241e0
* change /content/ -> /homebox/ * add cache to code generators * update env variables to set data storage * update env variables * set env variables in prod container * implement attachment post route (WIP) * get attachment endpoint * attachment download * implement string utilities lib * implement generic drop zone * use explicit truncate * remove clean dir * drop strings composable for lib * update item types and add attachments * add attachment API * implement service context * consolidate API code * implement editing attachments * implement upload limit configuration * improve error handling * add docs for max upload size * fix test cases
76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/hay-kot/homebox/backend/internal/repo"
|
|
"github.com/hay-kot/homebox/backend/internal/services/mappers"
|
|
"github.com/hay-kot/homebox/backend/internal/types"
|
|
)
|
|
|
|
var (
|
|
ErrNotOwner = errors.New("not owner")
|
|
)
|
|
|
|
type LocationService struct {
|
|
repos *repo.AllRepos
|
|
}
|
|
|
|
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) GetAll(ctx context.Context, groupId uuid.UUID) ([]*types.LocationCount, error) {
|
|
locations, err := svc.repos.Locations.GetAll(ctx, groupId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
locationsOut := make([]*types.LocationCount, len(locations))
|
|
for i, location := range locations {
|
|
locationsOut[i] = mappers.ToLocationCount(&location)
|
|
}
|
|
|
|
return locationsOut, 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 mappers.ToLocationOutErr(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))
|
|
}
|