homebox/backend/internal/repo/repo_locations.go

59 lines
1.5 KiB
Go
Raw Normal View History

2022-08-31 03:21:18 +00:00
package repo
import (
"context"
"github.com/google/uuid"
"github.com/hay-kot/content/backend/ent"
"github.com/hay-kot/content/backend/ent/group"
"github.com/hay-kot/content/backend/ent/location"
"github.com/hay-kot/content/backend/internal/types"
)
type EntLocationRepository struct {
db *ent.Client
}
func (r *EntLocationRepository) Get(ctx context.Context, ID uuid.UUID) (*ent.Location, error) {
2022-09-01 22:32:03 +00:00
return r.db.Location.Query().
Where(location.ID(ID)).
WithGroup().
WithItems().
Only(ctx)
2022-08-31 03:21:18 +00:00
}
func (r *EntLocationRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]*ent.Location, error) {
return r.db.Location.Query().
Where(location.HasGroupWith(group.ID(groupId))).
2022-08-31 05:22:01 +00:00
WithGroup().
2022-08-31 03:21:18 +00:00
All(ctx)
}
func (r *EntLocationRepository) Create(ctx context.Context, groupdId uuid.UUID, data types.LocationCreate) (*ent.Location, error) {
location, err := r.db.Location.Create().
SetName(data.Name).
SetDescription(data.Description).
SetGroupID(groupdId).
Save(ctx)
location.Edges.Group = &ent.Group{ID: groupdId} // bootstrap group ID
return location, err
}
func (r *EntLocationRepository) Update(ctx context.Context, data types.LocationUpdate) (*ent.Location, error) {
2022-09-01 22:32:03 +00:00
_, err := r.db.Location.UpdateOneID(data.ID).
2022-08-31 03:21:18 +00:00
SetName(data.Name).
SetDescription(data.Description).
Save(ctx)
2022-09-01 22:32:03 +00:00
if err != nil {
return nil, err
}
return r.Get(ctx, data.ID)
2022-08-31 03:21:18 +00:00
}
func (r *EntLocationRepository) Delete(ctx context.Context, id uuid.UUID) error {
return r.db.Location.DeleteOneID(id).Exec(ctx)
}