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"
|
2023-08-02 21:00:57 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services/reporting/eventbus"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/group"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/label"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/predicate"
|
2022-09-02 01:52:40 +00:00
|
|
|
)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
type LabelRepository struct {
|
2023-08-02 21:00:57 +00:00
|
|
|
db *ent.Client
|
|
|
|
bus *eventbus.EventBus
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
2023-08-02 21:00:57 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
|
|
|
LabelCreate struct {
|
2024-01-04 17:55:26 +00:00
|
|
|
Name string `json:"name" validate:"required,min=1,max=255"`
|
2023-03-21 04:32:10 +00:00
|
|
|
Description string `json:"description" validate:"max=255"`
|
2022-09-27 23:52:13 +00:00
|
|
|
Color string `json:"color"`
|
|
|
|
}
|
|
|
|
|
|
|
|
LabelUpdate struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
2024-01-04 17:55:26 +00:00
|
|
|
Name string `json:"name" validate:"required,min=1,max=255"`
|
2023-03-21 04:32:10 +00:00
|
|
|
Description string `json:"description" validate:"max=255"`
|
2022-09-27 23:52:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (r *LabelRepository) publishMutationEvent(groupID uuid.UUID) {
|
2023-08-02 21:00:57 +00:00
|
|
|
if r.bus != nil {
|
2024-04-28 17:21:43 +00:00
|
|
|
r.bus.Publish(eventbus.EventLabelMutation, eventbus.GroupMutationEvent{GID: groupID})
|
2023-08-02 21:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
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().
|
2022-09-27 23:52:13 +00:00
|
|
|
Only(ctx),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (r *LabelRepository) GetOne(ctx context.Context, labelID uuid.UUID) (LabelOut, error) {
|
|
|
|
return r.getOne(ctx, label.ID(labelID))
|
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)))
|
|
|
|
}
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
func (r *LabelRepository) GetAll(ctx context.Context, groupID uuid.UUID) ([]LabelSummary, error) {
|
2022-09-27 23:52:13 +00:00
|
|
|
return mapLabelsOut(r.db.Label.Query().
|
2024-01-04 17:55:26 +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
|
|
|
}
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
func (r *LabelRepository) Create(ctx context.Context, groupID 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).
|
2024-01-04 17:55:26 +00:00
|
|
|
SetGroupID(groupID).
|
2022-09-02 01:52:40 +00:00
|
|
|
Save(ctx)
|
2022-09-27 23:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return LabelOut{}, err
|
|
|
|
}
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
label.Edges.Group = &ent.Group{ID: groupID} // bootstrap group ID
|
|
|
|
r.publishMutationEvent(groupID)
|
2022-09-27 23:52:13 +00:00
|
|
|
return mapLabelOut(label), err
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 04:05:38 +00:00
|
|
|
func (r *LabelRepository) update(ctx context.Context, data LabelUpdate, where ...predicate.Label) (int, error) {
|
|
|
|
if len(where) == 0 {
|
|
|
|
panic("empty where not supported empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.db.Label.Update().
|
|
|
|
Where(where...).
|
2022-09-02 01:52:40 +00:00
|
|
|
SetName(data.Name).
|
|
|
|
SetDescription(data.Description).
|
|
|
|
SetColor(data.Color).
|
|
|
|
Save(ctx)
|
2022-10-30 04:05:38 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
func (r *LabelRepository) UpdateByGroup(ctx context.Context, groupID uuid.UUID, data LabelUpdate) (LabelOut, error) {
|
|
|
|
_, err := r.update(ctx, data, label.ID(data.ID), label.HasGroupWith(group.ID(groupID)))
|
2022-09-02 01:52:40 +00:00
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return LabelOut{}, err
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 17:21:43 +00:00
|
|
|
r.publishMutationEvent(groupID)
|
2022-09-27 23:52:13 +00:00
|
|
|
return r.GetOne(ctx, data.ID)
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
// delete removes the label from the database. This should only be used when
|
2023-10-07 02:51:08 +00:00
|
|
|
// the label's ownership is already confirmed/validated.
|
2023-08-02 21:00:57 +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)
|
|
|
|
}
|
2022-10-30 04:05:38 +00:00
|
|
|
|
|
|
|
func (r *LabelRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID) error {
|
|
|
|
_, err := r.db.Label.Delete().
|
|
|
|
Where(
|
|
|
|
label.ID(id),
|
|
|
|
label.HasGroupWith(group.ID(gid)),
|
|
|
|
).Exec(ctx)
|
2023-08-02 21:00:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.publishMutationEvent(gid)
|
2022-10-30 04:05:38 +00:00
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
return nil
|
2022-10-30 04:05:38 +00:00
|
|
|
}
|