2022-09-02 01:52:40 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/hay-kot/content/backend/ent"
|
|
|
|
"github.com/hay-kot/content/backend/ent/group"
|
|
|
|
"github.com/hay-kot/content/backend/ent/label"
|
|
|
|
"github.com/hay-kot/content/backend/internal/types"
|
|
|
|
)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
type LabelRepository struct {
|
2022-09-02 01:52:40 +00:00
|
|
|
db *ent.Client
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *LabelRepository) Get(ctx context.Context, ID uuid.UUID) (*ent.Label, error) {
|
2022-09-02 01:52:40 +00:00
|
|
|
return r.db.Label.Query().
|
|
|
|
Where(label.ID(ID)).
|
|
|
|
WithGroup().
|
|
|
|
WithItems().
|
|
|
|
Only(ctx)
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *LabelRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]*ent.Label, error) {
|
2022-09-02 01:52:40 +00:00
|
|
|
return r.db.Label.Query().
|
|
|
|
Where(label.HasGroupWith(group.ID(groupId))).
|
|
|
|
WithGroup().
|
|
|
|
All(ctx)
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *LabelRepository) Create(ctx context.Context, groupdId uuid.UUID, data types.LabelCreate) (*ent.Label, 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)
|
|
|
|
|
|
|
|
label.Edges.Group = &ent.Group{ID: groupdId} // bootstrap group ID
|
|
|
|
return label, err
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *LabelRepository) Update(ctx context.Context, data types.LabelUpdate) (*ent.Label, 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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Get(ctx, data.ID)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|