1
0
Fork 1
mirror of https://github.com/hay-kot/homebox.git synced 2024-12-22 23:06:31 +00:00
homebox/backend/internal/repo/repo_locations.go

168 lines
3.8 KiB
Go
Raw Normal View History

2022-08-31 03:21:18 +00:00
package repo
import (
"context"
"time"
2022-08-31 03:21:18 +00:00
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/ent"
"github.com/hay-kot/homebox/backend/ent/group"
"github.com/hay-kot/homebox/backend/ent/location"
"github.com/hay-kot/homebox/backend/ent/predicate"
2022-08-31 03:21:18 +00:00
)
type LocationRepository struct {
2022-08-31 03:21:18 +00:00
db *ent.Client
}
type (
LocationCreate struct {
Name string `json:"name"`
Description string `json:"description"`
}
LocationUpdate struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
LocationSummary struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
LocationOutCount struct {
LocationSummary
ItemCount int `json:"itemCount"`
}
LocationOut struct {
LocationSummary
Items []ItemSummary `json:"items"`
}
)
func mapLocationSummary(location *ent.Location) LocationSummary {
return LocationSummary{
ID: location.ID,
Name: location.Name,
Description: location.Description,
CreatedAt: location.CreatedAt,
UpdatedAt: location.UpdatedAt,
}
}
var (
mapLocationOutErr = mapTErrFunc(mapLocationOut)
)
func mapLocationOut(location *ent.Location) LocationOut {
return LocationOut{
LocationSummary: LocationSummary{
ID: location.ID,
Name: location.Name,
Description: location.Description,
CreatedAt: location.CreatedAt,
UpdatedAt: location.UpdatedAt,
},
Items: mapEach(location.Edges.Items, mapItemSummary),
}
2022-09-03 09:17:48 +00:00
}
// GetALlWithCount returns all locations with item count field populated
func (r *LocationRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]LocationOutCount, error) {
query := `--sql
2022-09-03 09:17:48 +00:00
SELECT
id,
name,
description,
created_at,
updated_at,
(
SELECT
COUNT(*)
FROM
items
WHERE
items.location_items = locations.id
) as item_count
FROM
locations
WHERE
locations.group_locations = ?
`
rows, err := r.db.Sql().QueryContext(ctx, query, groupId)
if err != nil {
return nil, err
}
list := []LocationOutCount{}
2022-09-03 09:17:48 +00:00
for rows.Next() {
var ct LocationOutCount
err := rows.Scan(&ct.ID, &ct.Name, &ct.Description, &ct.CreatedAt, &ct.UpdatedAt, &ct.ItemCount)
2022-09-03 09:17:48 +00:00
if err != nil {
return nil, err
}
2022-09-03 09:17:48 +00:00
list = append(list, ct)
}
return list, err
}
func (r *LocationRepository) getOne(ctx context.Context, where ...predicate.Location) (LocationOut, error) {
return mapLocationOutErr(r.db.Location.Query().
Where(where...).
2022-09-01 22:32:03 +00:00
WithGroup().
2022-09-03 09:17:48 +00:00
WithItems(func(iq *ent.ItemQuery) {
iq.WithLabel()
}).
Only(ctx))
2022-08-31 03:21:18 +00:00
}
func (r *LocationRepository) Get(ctx context.Context, ID uuid.UUID) (LocationOut, error) {
return r.getOne(ctx, location.ID(ID))
}
func (r *LocationRepository) GetOneByGroup(ctx context.Context, GID, ID uuid.UUID) (LocationOut, error) {
return r.getOne(ctx, location.ID(ID), location.HasGroupWith(group.ID(GID)))
}
func (r *LocationRepository) Create(ctx context.Context, gid uuid.UUID, data LocationCreate) (LocationOut, error) {
2022-08-31 03:21:18 +00:00
location, err := r.db.Location.Create().
SetName(data.Name).
SetDescription(data.Description).
SetGroupID(gid).
2022-08-31 03:21:18 +00:00
Save(ctx)
2022-09-06 19:15:07 +00:00
if err != nil {
return LocationOut{}, err
2022-09-06 19:15:07 +00:00
}
location.Edges.Group = &ent.Group{ID: gid} // bootstrap group ID
return mapLocationOut(location), nil
2022-08-31 03:21:18 +00:00
}
func (r *LocationRepository) Update(ctx context.Context, data LocationUpdate) (LocationOut, 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 LocationOut{}, err
2022-09-01 22:32:03 +00:00
}
return r.Get(ctx, data.ID)
2022-08-31 03:21:18 +00:00
}
func (r *LocationRepository) Delete(ctx context.Context, id uuid.UUID) error {
2022-08-31 03:21:18 +00:00
return r.db.Location.DeleteOneID(id).Exec(ctx)
}