2022-08-31 03:21:18 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-27 23:52:13 +00:00
|
|
|
"time"
|
2022-08-31 03:21:18 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/ent"
|
2022-09-27 23:52:13 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/ent/group"
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/ent/location"
|
2022-09-27 23:52:13 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/ent/predicate"
|
2022-08-31 03:21:18 +00:00
|
|
|
)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
type LocationRepository struct {
|
2022-08-31 03:21:18 +00:00
|
|
|
db *ent.Client
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
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
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *LocationRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]LocationOutCount, error) {
|
2022-09-05 08:26:21 +00:00
|
|
|
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 = ?
|
2022-10-15 21:29:33 +00:00
|
|
|
ORDER BY
|
|
|
|
locations.name ASC
|
|
|
|
`
|
2022-09-03 09:17:48 +00:00
|
|
|
|
|
|
|
rows, err := r.db.Sql().QueryContext(ctx, query, groupId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
list := []LocationOutCount{}
|
2022-09-03 09:17:48 +00:00
|
|
|
for rows.Next() {
|
2022-09-27 23:52:13 +00:00
|
|
|
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-27 23:52:13 +00:00
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
list = append(list, ct)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, err
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
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()
|
|
|
|
}).
|
2022-09-27 23:52:13 +00:00
|
|
|
Only(ctx))
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +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).
|
2022-09-27 23:52:13 +00:00
|
|
|
SetGroupID(gid).
|
2022-08-31 03:21:18 +00:00
|
|
|
Save(ctx)
|
|
|
|
|
2022-09-06 19:15:07 +00:00
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return LocationOut{}, err
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
location.Edges.Group = &ent.Group{ID: gid} // bootstrap group ID
|
|
|
|
return mapLocationOut(location), nil
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +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 {
|
2022-09-27 23:52:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +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)
|
|
|
|
}
|