mirror of
https://github.com/hay-kot/homebox.git
synced 2024-12-18 21:16:31 +00:00
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
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) {
|
|
return r.db.Location.Query().
|
|
Where(location.ID(ID)).
|
|
WithGroup().
|
|
WithItems().
|
|
Only(ctx)
|
|
}
|
|
|
|
func (r *EntLocationRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]*ent.Location, error) {
|
|
return r.db.Location.Query().
|
|
Where(location.HasGroupWith(group.ID(groupId))).
|
|
WithGroup().
|
|
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) {
|
|
_, err := r.db.Location.UpdateOneID(data.ID).
|
|
SetName(data.Name).
|
|
SetDescription(data.Description).
|
|
Save(ctx)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return r.Get(ctx, data.ID)
|
|
}
|
|
|
|
func (r *EntLocationRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
|
return r.db.Location.DeleteOneID(id).Exec(ctx)
|
|
}
|