homebox/backend/internal/services/service_locations.go

48 lines
1.3 KiB
Go
Raw Normal View History

2022-08-31 03:21:18 +00:00
package services
import (
"context"
2022-09-01 22:32:03 +00:00
"errors"
2022-08-31 03:21:18 +00:00
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/repo"
2022-08-31 03:21:18 +00:00
)
2022-09-01 22:32:03 +00:00
var (
ErrNotOwner = errors.New("not owner")
)
2022-08-31 03:21:18 +00:00
type LocationService struct {
repos *repo.AllRepos
}
func (svc *LocationService) GetOne(ctx context.Context, groupId uuid.UUID, id uuid.UUID) (repo.LocationOut, error) {
return svc.repos.Locations.GetOneByGroup(ctx, groupId, id)
2022-08-31 05:22:01 +00:00
}
func (svc *LocationService) GetAll(ctx context.Context, groupId uuid.UUID) ([]repo.LocationOutCount, error) {
return svc.repos.Locations.GetAll(ctx, groupId)
2022-08-31 03:21:18 +00:00
}
2022-09-01 22:32:03 +00:00
func (svc *LocationService) Create(ctx context.Context, groupId uuid.UUID, data repo.LocationCreate) (repo.LocationOut, error) {
return svc.repos.Locations.Create(ctx, groupId, data)
2022-09-01 22:32:03 +00:00
}
func (svc *LocationService) Delete(ctx context.Context, groupId uuid.UUID, id uuid.UUID) error {
_, err := svc.repos.Locations.GetOneByGroup(ctx, groupId, id)
2022-09-01 22:32:03 +00:00
if err != nil {
return err
}
return svc.repos.Locations.Delete(ctx, id)
}
func (svc *LocationService) Update(ctx context.Context, groupId uuid.UUID, data repo.LocationUpdate) (repo.LocationOut, error) {
location, err := svc.repos.Locations.GetOneByGroup(ctx, groupId, data.ID)
2022-09-01 22:32:03 +00:00
if err != nil {
return repo.LocationOut{}, err
2022-09-01 22:32:03 +00:00
}
data.ID = location.ID
return svc.repos.Locations.Update(ctx, data)
2022-09-01 22:32:03 +00:00
}