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/location"
|
|
|
|
"github.com/hay-kot/content/backend/internal/types"
|
|
|
|
)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
type LocationRepository struct {
|
2022-08-31 03:21:18 +00:00
|
|
|
db *ent.Client
|
|
|
|
}
|
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
type LocationWithCount struct {
|
|
|
|
*ent.Location
|
|
|
|
ItemCount int `json:"itemCount"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetALlWithCount returns all locations with item count field populated
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *LocationRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]LocationWithCount, 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 := []LocationWithCount{}
|
|
|
|
for rows.Next() {
|
|
|
|
var loc ent.Location
|
|
|
|
var ct LocationWithCount
|
|
|
|
err := rows.Scan(&loc.ID, &loc.Name, &loc.Description, &loc.CreatedAt, &loc.UpdatedAt, &ct.ItemCount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ct.Location = &loc
|
|
|
|
list = append(list, ct)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, err
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *LocationRepository) 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().
|
2022-09-03 09:17:48 +00:00
|
|
|
WithItems(func(iq *ent.ItemQuery) {
|
|
|
|
iq.WithLabel()
|
|
|
|
}).
|
2022-09-01 22:32:03 +00:00
|
|
|
Only(ctx)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *LocationRepository) Create(ctx context.Context, groupdId uuid.UUID, data types.LocationCreate) (*ent.Location, error) {
|
2022-08-31 03:21:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *LocationRepository) 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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|