2022-09-03 09:17:48 +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/item"
|
|
|
|
"github.com/hay-kot/content/backend/internal/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ItemsRepository struct {
|
|
|
|
db *ent.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ItemsRepository) GetOne(ctx context.Context, id uuid.UUID) (*ent.Item, error) {
|
|
|
|
return e.db.Item.Query().
|
|
|
|
Where(item.ID(id)).
|
|
|
|
WithFields().
|
|
|
|
WithLabel().
|
|
|
|
WithLocation().
|
|
|
|
WithGroup().
|
2022-09-12 22:47:27 +00:00
|
|
|
WithAttachments(func(aq *ent.AttachmentQuery) {
|
|
|
|
aq.WithDocument()
|
|
|
|
}).
|
2022-09-03 09:17:48 +00:00
|
|
|
Only(ctx)
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
// GetAll returns all the items in the database with the Labels and Locations eager loaded.
|
2022-09-03 09:17:48 +00:00
|
|
|
func (e *ItemsRepository) GetAll(ctx context.Context, gid uuid.UUID) ([]*ent.Item, error) {
|
|
|
|
return e.db.Item.Query().
|
|
|
|
Where(item.HasGroupWith(group.ID(gid))).
|
|
|
|
WithLabel().
|
|
|
|
WithLocation().
|
|
|
|
All(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ItemsRepository) Create(ctx context.Context, gid uuid.UUID, data types.ItemCreate) (*ent.Item, error) {
|
2022-09-05 08:26:21 +00:00
|
|
|
q := e.db.Item.Create().
|
2022-09-03 09:17:48 +00:00
|
|
|
SetName(data.Name).
|
|
|
|
SetDescription(data.Description).
|
|
|
|
SetGroupID(gid).
|
2022-09-05 08:26:21 +00:00
|
|
|
SetLocationID(data.LocationID)
|
|
|
|
|
|
|
|
if data.LabelIDs != nil && len(data.LabelIDs) > 0 {
|
|
|
|
q.AddLabelIDs(data.LabelIDs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := q.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.GetOne(ctx, result.ID)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (e *ItemsRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
|
|
|
return e.db.Item.DeleteOneID(id).Exec(ctx)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (e *ItemsRepository) Update(ctx context.Context, data types.ItemUpdate) (*ent.Item, error) {
|
|
|
|
q := e.db.Item.UpdateOneID(data.ID).
|
|
|
|
SetName(data.Name).
|
|
|
|
SetDescription(data.Description).
|
|
|
|
SetLocationID(data.LocationID).
|
|
|
|
SetSerialNumber(data.SerialNumber).
|
|
|
|
SetModelNumber(data.ModelNumber).
|
|
|
|
SetManufacturer(data.Manufacturer).
|
|
|
|
SetPurchaseTime(data.PurchaseTime).
|
|
|
|
SetPurchaseFrom(data.PurchaseFrom).
|
|
|
|
SetPurchasePrice(data.PurchasePrice).
|
|
|
|
SetSoldTime(data.SoldTime).
|
|
|
|
SetSoldTo(data.SoldTo).
|
|
|
|
SetSoldPrice(data.SoldPrice).
|
|
|
|
SetSoldNotes(data.SoldNotes).
|
2022-09-09 18:20:38 +00:00
|
|
|
SetNotes(data.Notes).
|
|
|
|
SetLifetimeWarranty(data.LifetimeWarranty).
|
2022-09-12 22:47:27 +00:00
|
|
|
SetInsured(data.Insured).
|
2022-09-09 18:20:38 +00:00
|
|
|
SetWarrantyExpires(data.WarrantyExpires).
|
2022-09-12 22:47:27 +00:00
|
|
|
SetWarrantyDetails(data.WarrantyDetails).
|
|
|
|
SetQuantity(data.Quantity)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
currentLabels, err := e.db.Item.Query().Where(item.ID(data.ID)).QueryLabel().All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
set := EntitiesToIDSet(currentLabels)
|
|
|
|
|
|
|
|
for _, l := range data.LabelIDs {
|
|
|
|
if set.Has(l) {
|
|
|
|
set.Remove(l)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
q.AddLabelIDs(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
if set.Len() > 0 {
|
|
|
|
q.RemoveLabelIDs(set.Slice()...)
|
|
|
|
}
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
err = q.Exec(ctx)
|
2022-09-05 08:26:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.GetOne(ctx, data.ID)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|