2022-09-02 01:52:40 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-27 23:52:13 +00:00
|
|
|
"time"
|
2022-09-02 01:52:40 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/ent"
|
|
|
|
"github.com/hay-kot/homebox/backend/ent/group"
|
|
|
|
"github.com/hay-kot/homebox/backend/ent/label"
|
2022-09-27 23:52:13 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/ent/predicate"
|
2022-09-02 01:52:40 +00:00
|
|
|
)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
type LabelRepository struct {
|
2022-09-02 01:52:40 +00:00
|
|
|
db *ent.Client
|
|
|
|
}
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
|
|
|
LabelCreate struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
}
|
|
|
|
|
|
|
|
LabelUpdate struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
}
|
|
|
|
|
|
|
|
LabelSummary struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
LabelOut struct {
|
|
|
|
LabelSummary
|
|
|
|
Items []ItemSummary `json:"items"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func mapLabelSummary(label *ent.Label) LabelSummary {
|
|
|
|
return LabelSummary{
|
|
|
|
ID: label.ID,
|
|
|
|
Name: label.Name,
|
|
|
|
Description: label.Description,
|
|
|
|
CreatedAt: label.CreatedAt,
|
|
|
|
UpdatedAt: label.UpdatedAt,
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 01:52:40 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
var (
|
|
|
|
mapLabelOutErr = mapTErrFunc(mapLabelOut)
|
|
|
|
mapLabelsOut = mapTEachErrFunc(mapLabelSummary)
|
|
|
|
)
|
|
|
|
|
|
|
|
func mapLabelOut(label *ent.Label) LabelOut {
|
|
|
|
return LabelOut{
|
|
|
|
LabelSummary: mapLabelSummary(label),
|
|
|
|
Items: mapEach(label.Edges.Items, mapItemSummary),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LabelRepository) getOne(ctx context.Context, where ...predicate.Label) (LabelOut, error) {
|
|
|
|
return mapLabelOutErr(r.db.Label.Query().
|
|
|
|
Where(where...).
|
2022-09-02 01:52:40 +00:00
|
|
|
WithGroup().
|
|
|
|
WithItems().
|
2022-09-27 23:52:13 +00:00
|
|
|
Only(ctx),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LabelRepository) GetOne(ctx context.Context, ID uuid.UUID) (LabelOut, error) {
|
|
|
|
return r.getOne(ctx, label.ID(ID))
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *LabelRepository) GetOneByGroup(ctx context.Context, gid, ld uuid.UUID) (LabelOut, error) {
|
|
|
|
return r.getOne(ctx, label.ID(ld), label.HasGroupWith(group.ID(gid)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LabelRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]LabelSummary, error) {
|
|
|
|
return mapLabelsOut(r.db.Label.Query().
|
2022-09-02 01:52:40 +00:00
|
|
|
Where(label.HasGroupWith(group.ID(groupId))).
|
2022-10-15 21:29:33 +00:00
|
|
|
Order(ent.Asc(label.FieldName)).
|
2022-09-02 01:52:40 +00:00
|
|
|
WithGroup().
|
2022-09-27 23:52:13 +00:00
|
|
|
All(ctx),
|
|
|
|
)
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *LabelRepository) Create(ctx context.Context, groupdId uuid.UUID, data LabelCreate) (LabelOut, error) {
|
2022-09-02 01:52:40 +00:00
|
|
|
label, err := r.db.Label.Create().
|
|
|
|
SetName(data.Name).
|
|
|
|
SetDescription(data.Description).
|
|
|
|
SetColor(data.Color).
|
|
|
|
SetGroupID(groupdId).
|
|
|
|
Save(ctx)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return LabelOut{}, err
|
|
|
|
}
|
|
|
|
|
2022-09-02 01:52:40 +00:00
|
|
|
label.Edges.Group = &ent.Group{ID: groupdId} // bootstrap group ID
|
2022-09-27 23:52:13 +00:00
|
|
|
return mapLabelOut(label), err
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *LabelRepository) Update(ctx context.Context, data LabelUpdate) (LabelOut, error) {
|
2022-09-02 01:52:40 +00:00
|
|
|
_, err := r.db.Label.UpdateOneID(data.ID).
|
|
|
|
SetName(data.Name).
|
|
|
|
SetDescription(data.Description).
|
|
|
|
SetColor(data.Color).
|
|
|
|
Save(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return LabelOut{}, err
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
return r.GetOne(ctx, data.ID)
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *LabelRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
2022-09-02 01:52:40 +00:00
|
|
|
return r.db.Label.DeleteOneID(id).Exec(ctx)
|
|
|
|
}
|