2022-09-03 09:17:48 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-27 23:52:13 +00:00
|
|
|
"time"
|
2022-09-03 09:17:48 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
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/item"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/itemfield"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/label"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/location"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/predicate"
|
2022-09-03 09:17:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ItemsRepository struct {
|
|
|
|
db *ent.Client
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
2022-10-13 05:13:07 +00:00
|
|
|
ItemQuery struct {
|
2022-11-01 07:30:42 +00:00
|
|
|
Page int
|
|
|
|
PageSize int
|
|
|
|
Search string `json:"search"`
|
|
|
|
LocationIDs []uuid.UUID `json:"locationIds"`
|
|
|
|
LabelIDs []uuid.UUID `json:"labelIds"`
|
|
|
|
SortBy string `json:"sortBy"`
|
|
|
|
IncludeArchived bool `json:"includeArchived"`
|
2022-10-13 05:13:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 05:41:27 +00:00
|
|
|
ItemField struct {
|
|
|
|
ID uuid.UUID `json:"id,omitempty"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
TextValue string `json:"textValue"`
|
|
|
|
NumberValue int `json:"numberValue"`
|
|
|
|
BooleanValue bool `json:"booleanValue"`
|
|
|
|
TimeValue time.Time `json:"timeValue,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
ItemCreate struct {
|
2022-10-24 04:54:39 +00:00
|
|
|
ImportRef string `json:"-"`
|
|
|
|
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
2022-11-13 22:15:26 +00:00
|
|
|
AssetID AssetID `json:"-"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Edges
|
|
|
|
LocationID uuid.UUID `json:"locationId"`
|
|
|
|
LabelIDs []uuid.UUID `json:"labelIds"`
|
|
|
|
}
|
|
|
|
ItemUpdate struct {
|
2022-10-24 04:54:39 +00:00
|
|
|
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable,x-omitempty"`
|
2022-09-27 23:52:13 +00:00
|
|
|
ID uuid.UUID `json:"id"`
|
2022-11-13 03:57:51 +00:00
|
|
|
AssetID AssetID `json:"assetId"`
|
2022-09-27 23:52:13 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Quantity int `json:"quantity"`
|
|
|
|
Insured bool `json:"insured"`
|
2022-11-01 07:30:42 +00:00
|
|
|
Archived bool `json:"archived"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Edges
|
|
|
|
LocationID uuid.UUID `json:"locationId"`
|
|
|
|
LabelIDs []uuid.UUID `json:"labelIds"`
|
|
|
|
|
|
|
|
// Identifications
|
|
|
|
SerialNumber string `json:"serialNumber"`
|
|
|
|
ModelNumber string `json:"modelNumber"`
|
|
|
|
Manufacturer string `json:"manufacturer"`
|
|
|
|
|
|
|
|
// Warranty
|
|
|
|
LifetimeWarranty bool `json:"lifetimeWarranty"`
|
|
|
|
WarrantyExpires time.Time `json:"warrantyExpires"`
|
|
|
|
WarrantyDetails string `json:"warrantyDetails"`
|
|
|
|
|
|
|
|
// Purchase
|
|
|
|
PurchaseTime time.Time `json:"purchaseTime"`
|
|
|
|
PurchaseFrom string `json:"purchaseFrom"`
|
|
|
|
PurchasePrice float64 `json:"purchasePrice,string"`
|
|
|
|
|
|
|
|
// Sold
|
|
|
|
SoldTime time.Time `json:"soldTime"`
|
|
|
|
SoldTo string `json:"soldTo"`
|
|
|
|
SoldPrice float64 `json:"soldPrice,string"`
|
|
|
|
SoldNotes string `json:"soldNotes"`
|
|
|
|
|
|
|
|
// Extras
|
2022-10-16 05:41:27 +00:00
|
|
|
Notes string `json:"notes"`
|
|
|
|
Fields []ItemField `json:"fields"`
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemSummary struct {
|
|
|
|
ImportRef string `json:"-"`
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Quantity int `json:"quantity"`
|
|
|
|
Insured bool `json:"insured"`
|
2022-11-01 07:30:42 +00:00
|
|
|
Archived bool `json:"archived"`
|
2022-09-27 23:52:13 +00:00
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
|
|
|
|
|
|
// Edges
|
2022-10-24 04:54:39 +00:00
|
|
|
Location *LocationSummary `json:"location,omitempty" extensions:"x-nullable,x-omitempty"`
|
|
|
|
Labels []LabelSummary `json:"labels"`
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemOut struct {
|
2022-10-24 04:54:39 +00:00
|
|
|
Parent *ItemSummary `json:"parent,omitempty" extensions:"x-nullable,x-omitempty"`
|
2022-09-27 23:52:13 +00:00
|
|
|
ItemSummary
|
2022-11-13 03:57:51 +00:00
|
|
|
AssetID AssetID `json:"assetId,string"`
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
SerialNumber string `json:"serialNumber"`
|
|
|
|
ModelNumber string `json:"modelNumber"`
|
|
|
|
Manufacturer string `json:"manufacturer"`
|
|
|
|
|
|
|
|
// Warranty
|
|
|
|
LifetimeWarranty bool `json:"lifetimeWarranty"`
|
|
|
|
WarrantyExpires time.Time `json:"warrantyExpires"`
|
|
|
|
WarrantyDetails string `json:"warrantyDetails"`
|
|
|
|
|
|
|
|
// Purchase
|
|
|
|
PurchaseTime time.Time `json:"purchaseTime"`
|
|
|
|
PurchaseFrom string `json:"purchaseFrom"`
|
|
|
|
PurchasePrice float64 `json:"purchasePrice,string"`
|
|
|
|
|
|
|
|
// Sold
|
|
|
|
SoldTime time.Time `json:"soldTime"`
|
|
|
|
SoldTo string `json:"soldTo"`
|
|
|
|
SoldPrice float64 `json:"soldPrice,string"`
|
|
|
|
SoldNotes string `json:"soldNotes"`
|
|
|
|
|
|
|
|
// Extras
|
|
|
|
Notes string `json:"notes"`
|
|
|
|
|
|
|
|
Attachments []ItemAttachment `json:"attachments"`
|
2022-10-24 04:54:39 +00:00
|
|
|
Fields []ItemField `json:"fields"`
|
|
|
|
Children []ItemSummary `json:"children"`
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mapItemsSummaryErr = mapTEachErrFunc(mapItemSummary)
|
|
|
|
)
|
|
|
|
|
|
|
|
func mapItemSummary(item *ent.Item) ItemSummary {
|
2022-10-24 04:54:39 +00:00
|
|
|
var location *LocationSummary
|
2022-09-27 23:52:13 +00:00
|
|
|
if item.Edges.Location != nil {
|
2022-10-24 04:54:39 +00:00
|
|
|
loc := mapLocationSummary(item.Edges.Location)
|
|
|
|
location = &loc
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 04:54:39 +00:00
|
|
|
labels := make([]LabelSummary, len(item.Edges.Label))
|
2022-09-27 23:52:13 +00:00
|
|
|
if item.Edges.Label != nil {
|
|
|
|
labels = mapEach(item.Edges.Label, mapLabelSummary)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ItemSummary{
|
|
|
|
ID: item.ID,
|
|
|
|
Name: item.Name,
|
|
|
|
Description: item.Description,
|
|
|
|
Quantity: item.Quantity,
|
|
|
|
CreatedAt: item.CreatedAt,
|
|
|
|
UpdatedAt: item.UpdatedAt,
|
2022-11-01 07:30:42 +00:00
|
|
|
Archived: item.Archived,
|
2022-09-27 23:52:13 +00:00
|
|
|
|
|
|
|
// Edges
|
|
|
|
Location: location,
|
|
|
|
Labels: labels,
|
|
|
|
|
|
|
|
// Warranty
|
|
|
|
Insured: item.Insured,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
mapItemOutErr = mapTErrFunc(mapItemOut)
|
|
|
|
)
|
|
|
|
|
2022-10-16 05:41:27 +00:00
|
|
|
func mapFields(fields []*ent.ItemField) []ItemField {
|
|
|
|
result := make([]ItemField, len(fields))
|
|
|
|
for i, f := range fields {
|
|
|
|
result[i] = ItemField{
|
|
|
|
ID: f.ID,
|
|
|
|
Type: f.Type.String(),
|
|
|
|
Name: f.Name,
|
|
|
|
TextValue: f.TextValue,
|
|
|
|
NumberValue: f.NumberValue,
|
|
|
|
BooleanValue: f.BooleanValue,
|
|
|
|
TimeValue: f.TimeValue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func mapItemOut(item *ent.Item) ItemOut {
|
|
|
|
var attachments []ItemAttachment
|
|
|
|
if item.Edges.Attachments != nil {
|
|
|
|
attachments = mapEach(item.Edges.Attachments, ToItemAttachment)
|
|
|
|
}
|
|
|
|
|
2022-10-16 05:41:27 +00:00
|
|
|
var fields []ItemField
|
|
|
|
if item.Edges.Fields != nil {
|
|
|
|
fields = mapFields(item.Edges.Fields)
|
|
|
|
}
|
|
|
|
|
2022-10-24 04:54:39 +00:00
|
|
|
var children []ItemSummary
|
|
|
|
if item.Edges.Children != nil {
|
|
|
|
children = mapEach(item.Edges.Children, mapItemSummary)
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent *ItemSummary
|
|
|
|
if item.Edges.Parent != nil {
|
|
|
|
v := mapItemSummary(item.Edges.Parent)
|
|
|
|
parent = &v
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
return ItemOut{
|
2022-10-24 04:54:39 +00:00
|
|
|
Parent: parent,
|
2022-11-13 03:57:51 +00:00
|
|
|
AssetID: AssetID(item.AssetID),
|
2022-09-27 23:52:13 +00:00
|
|
|
ItemSummary: mapItemSummary(item),
|
|
|
|
LifetimeWarranty: item.LifetimeWarranty,
|
|
|
|
WarrantyExpires: item.WarrantyExpires,
|
|
|
|
WarrantyDetails: item.WarrantyDetails,
|
|
|
|
|
|
|
|
// Identification
|
|
|
|
SerialNumber: item.SerialNumber,
|
|
|
|
ModelNumber: item.ModelNumber,
|
|
|
|
Manufacturer: item.Manufacturer,
|
|
|
|
|
|
|
|
// Purchase
|
|
|
|
PurchaseTime: item.PurchaseTime,
|
|
|
|
PurchaseFrom: item.PurchaseFrom,
|
|
|
|
PurchasePrice: item.PurchasePrice,
|
|
|
|
|
|
|
|
// Sold
|
|
|
|
SoldTime: item.SoldTime,
|
|
|
|
SoldTo: item.SoldTo,
|
|
|
|
SoldPrice: item.SoldPrice,
|
|
|
|
SoldNotes: item.SoldNotes,
|
|
|
|
|
|
|
|
// Extras
|
|
|
|
Notes: item.Notes,
|
|
|
|
Attachments: attachments,
|
2022-10-16 05:41:27 +00:00
|
|
|
Fields: fields,
|
2022-10-24 04:54:39 +00:00
|
|
|
Children: children,
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ItemsRepository) getOne(ctx context.Context, where ...predicate.Item) (ItemOut, error) {
|
|
|
|
q := e.db.Item.Query().Where(where...)
|
|
|
|
|
|
|
|
return mapItemOutErr(q.
|
2022-09-03 09:17:48 +00:00
|
|
|
WithFields().
|
|
|
|
WithLabel().
|
|
|
|
WithLocation().
|
|
|
|
WithGroup().
|
2022-10-24 04:54:39 +00:00
|
|
|
WithChildren().
|
|
|
|
WithParent().
|
2022-09-12 22:47:27 +00:00
|
|
|
WithAttachments(func(aq *ent.AttachmentQuery) {
|
|
|
|
aq.WithDocument()
|
|
|
|
}).
|
2022-09-27 23:52:13 +00:00
|
|
|
Only(ctx),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOne returns a single item by ID. If the item does not exist, an error is returned.
|
|
|
|
// See also: GetOneByGroup to ensure that the item belongs to a specific group.
|
|
|
|
func (e *ItemsRepository) GetOne(ctx context.Context, id uuid.UUID) (ItemOut, error) {
|
|
|
|
return e.getOne(ctx, item.ID(id))
|
|
|
|
}
|
|
|
|
|
2022-10-16 01:46:57 +00:00
|
|
|
func (e *ItemsRepository) CheckRef(ctx context.Context, GID uuid.UUID, ref string) (bool, error) {
|
|
|
|
q := e.db.Item.Query().Where(item.HasGroupWith(group.ID(GID)))
|
|
|
|
return q.Where(item.ImportRef(ref)).Exist(ctx)
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
// GetOneByGroup returns a single item by ID. If the item does not exist, an error is returned.
|
|
|
|
// GetOneByGroup ensures that the item belongs to a specific group.
|
|
|
|
func (e *ItemsRepository) GetOneByGroup(ctx context.Context, gid, id uuid.UUID) (ItemOut, error) {
|
|
|
|
return e.getOne(ctx, item.ID(id), item.HasGroupWith(group.ID(gid)))
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 05:13:07 +00:00
|
|
|
// QueryByGroup returns a list of items that belong to a specific group based on the provided query.
|
|
|
|
func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q ItemQuery) (PaginationResult[ItemSummary], error) {
|
2022-11-01 07:30:42 +00:00
|
|
|
qb := e.db.Item.Query().Where(
|
|
|
|
item.HasGroupWith(group.ID(gid)),
|
|
|
|
)
|
|
|
|
|
|
|
|
if q.IncludeArchived {
|
|
|
|
qb = qb.Where(
|
|
|
|
item.Or(
|
|
|
|
item.Archived(true),
|
|
|
|
item.Archived(false),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
qb = qb.Where(item.Archived(false))
|
|
|
|
}
|
2022-10-13 05:13:07 +00:00
|
|
|
|
|
|
|
if len(q.LabelIDs) > 0 {
|
|
|
|
labels := make([]predicate.Item, 0, len(q.LabelIDs))
|
|
|
|
for _, l := range q.LabelIDs {
|
|
|
|
labels = append(labels, item.HasLabelWith(label.ID(l)))
|
|
|
|
}
|
|
|
|
qb = qb.Where(item.Or(labels...))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.LocationIDs) > 0 {
|
|
|
|
locations := make([]predicate.Item, 0, len(q.LocationIDs))
|
|
|
|
for _, l := range q.LocationIDs {
|
|
|
|
locations = append(locations, item.HasLocationWith(location.ID(l)))
|
|
|
|
}
|
|
|
|
qb = qb.Where(item.Or(locations...))
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.Search != "" {
|
|
|
|
qb.Where(
|
|
|
|
item.Or(
|
|
|
|
item.NameContainsFold(q.Search),
|
|
|
|
item.DescriptionContainsFold(q.Search),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.Page != -1 || q.PageSize != -1 {
|
|
|
|
qb = qb.
|
|
|
|
Offset(calculateOffset(q.Page, q.PageSize)).
|
|
|
|
Limit(q.PageSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := mapItemsSummaryErr(
|
2022-10-15 21:29:33 +00:00
|
|
|
qb.Order(ent.Asc(item.FieldName)).
|
|
|
|
WithLabel().
|
2022-10-13 05:13:07 +00:00
|
|
|
WithLocation().
|
|
|
|
All(ctx),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return PaginationResult[ItemSummary]{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := qb.Count(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return PaginationResult[ItemSummary]{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return PaginationResult[ItemSummary]{
|
|
|
|
Page: q.Page,
|
|
|
|
PageSize: q.PageSize,
|
|
|
|
Total: count,
|
|
|
|
Items: items,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-27 23:52:13 +00:00
|
|
|
func (e *ItemsRepository) GetAll(ctx context.Context, gid uuid.UUID) ([]ItemSummary, error) {
|
|
|
|
return mapItemsSummaryErr(e.db.Item.Query().
|
2022-09-03 09:17:48 +00:00
|
|
|
Where(item.HasGroupWith(group.ID(gid))).
|
|
|
|
WithLabel().
|
|
|
|
WithLocation().
|
2022-09-27 23:52:13 +00:00
|
|
|
All(ctx))
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 03:57:51 +00:00
|
|
|
func (e *ItemsRepository) GetAllZeroAssetID(ctx context.Context, GID uuid.UUID) ([]ItemSummary, error) {
|
|
|
|
q := e.db.Item.Query().Where(
|
|
|
|
item.HasGroupWith(group.ID(GID)),
|
|
|
|
item.AssetID(0),
|
|
|
|
).Order(
|
|
|
|
ent.Asc(item.FieldCreatedAt),
|
|
|
|
)
|
|
|
|
|
|
|
|
return mapItemsSummaryErr(q.All(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ItemsRepository) GetHighestAssetID(ctx context.Context, GID uuid.UUID) (AssetID, error) {
|
|
|
|
q := e.db.Item.Query().Where(
|
|
|
|
item.HasGroupWith(group.ID(GID)),
|
|
|
|
).Order(
|
|
|
|
ent.Desc(item.FieldAssetID),
|
|
|
|
).Limit(1)
|
|
|
|
|
|
|
|
result, err := q.First(ctx)
|
|
|
|
if err != nil {
|
2022-11-13 22:38:47 +00:00
|
|
|
if ent.IsNotFound(err) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2022-11-13 03:57:51 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return AssetID(result.AssetID), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ItemsRepository) SetAssetID(ctx context.Context, GID uuid.UUID, ID uuid.UUID, assetID AssetID) error {
|
|
|
|
q := e.db.Item.Update().Where(
|
|
|
|
item.HasGroupWith(group.ID(GID)),
|
|
|
|
item.ID(ID),
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := q.SetAssetID(int(assetID)).Save(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (e *ItemsRepository) Create(ctx context.Context, gid uuid.UUID, data ItemCreate) (ItemOut, error) {
|
2022-09-05 08:26:21 +00:00
|
|
|
q := e.db.Item.Create().
|
2022-10-16 01:46:57 +00:00
|
|
|
SetImportRef(data.ImportRef).
|
2022-09-03 09:17:48 +00:00
|
|
|
SetName(data.Name).
|
|
|
|
SetDescription(data.Description).
|
|
|
|
SetGroupID(gid).
|
2022-11-13 22:15:26 +00:00
|
|
|
SetLocationID(data.LocationID).
|
|
|
|
SetAssetID(int(data.AssetID))
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
if data.LabelIDs != nil && len(data.LabelIDs) > 0 {
|
|
|
|
q.AddLabelIDs(data.LabelIDs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := q.Save(ctx)
|
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return ItemOut{}, err
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-27 23:52:13 +00:00
|
|
|
func (e *ItemsRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID) error {
|
|
|
|
_, err := e.db.Item.
|
|
|
|
Delete().
|
|
|
|
Where(
|
|
|
|
item.ID(id),
|
|
|
|
item.HasGroupWith(group.ID(gid)),
|
|
|
|
).Exec(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ItemsRepository) UpdateByGroup(ctx context.Context, gid uuid.UUID, data ItemUpdate) (ItemOut, error) {
|
|
|
|
q := e.db.Item.Update().Where(item.ID(data.ID), item.HasGroupWith(group.ID(gid))).
|
2022-09-05 08:26:21 +00:00
|
|
|
SetName(data.Name).
|
|
|
|
SetDescription(data.Description).
|
|
|
|
SetLocationID(data.LocationID).
|
|
|
|
SetSerialNumber(data.SerialNumber).
|
|
|
|
SetModelNumber(data.ModelNumber).
|
|
|
|
SetManufacturer(data.Manufacturer).
|
2022-11-01 07:30:42 +00:00
|
|
|
SetArchived(data.Archived).
|
2022-09-05 08:26:21 +00:00
|
|
|
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).
|
2022-11-13 03:57:51 +00:00
|
|
|
SetQuantity(data.Quantity).
|
|
|
|
SetAssetID(int(data.AssetID))
|
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 {
|
2022-09-27 23:52:13 +00:00
|
|
|
return ItemOut{}, err
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
set := newIDSet(currentLabels)
|
2022-09-12 22:47:27 +00:00
|
|
|
|
|
|
|
for _, l := range data.LabelIDs {
|
2022-09-27 23:52:13 +00:00
|
|
|
if set.Contains(l) {
|
2022-09-12 22:47:27 +00:00
|
|
|
set.Remove(l)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
q.AddLabelIDs(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
if set.Len() > 0 {
|
|
|
|
q.RemoveLabelIDs(set.Slice()...)
|
|
|
|
}
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-10-24 04:54:39 +00:00
|
|
|
if data.ParentID != uuid.Nil {
|
|
|
|
q.SetParentID(data.ParentID)
|
|
|
|
} else {
|
|
|
|
q.ClearParent()
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
err = q.Exec(ctx)
|
2022-09-05 08:26:21 +00:00
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return ItemOut{}, err
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 05:41:27 +00:00
|
|
|
fields, err := e.db.ItemField.Query().Where(itemfield.HasItemWith(item.ID(data.ID))).All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ItemOut{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldIds := newIDSet(fields)
|
|
|
|
|
|
|
|
// Update Existing Fields
|
|
|
|
for _, f := range data.Fields {
|
|
|
|
if f.ID == uuid.Nil {
|
|
|
|
// Create New Field
|
|
|
|
_, err = e.db.ItemField.Create().
|
|
|
|
SetItemID(data.ID).
|
|
|
|
SetType(itemfield.Type(f.Type)).
|
|
|
|
SetName(f.Name).
|
|
|
|
SetTextValue(f.TextValue).
|
|
|
|
SetNumberValue(f.NumberValue).
|
|
|
|
SetBooleanValue(f.BooleanValue).
|
|
|
|
SetTimeValue(f.TimeValue).
|
|
|
|
Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ItemOut{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := e.db.ItemField.Update().
|
|
|
|
Where(
|
|
|
|
itemfield.ID(f.ID),
|
|
|
|
itemfield.HasItemWith(item.ID(data.ID)),
|
|
|
|
).
|
|
|
|
SetType(itemfield.Type(f.Type)).
|
|
|
|
SetName(f.Name).
|
|
|
|
SetTextValue(f.TextValue).
|
|
|
|
SetNumberValue(f.NumberValue).
|
|
|
|
SetBooleanValue(f.BooleanValue).
|
|
|
|
SetTimeValue(f.TimeValue)
|
|
|
|
|
|
|
|
_, err = opt.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ItemOut{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldIds.Remove(f.ID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete Fields that are no longer present
|
|
|
|
if fieldIds.Len() > 0 {
|
|
|
|
_, err = e.db.ItemField.Delete().
|
|
|
|
Where(
|
|
|
|
itemfield.IDIn(fieldIds.Slice()...),
|
|
|
|
itemfield.HasItemWith(item.ID(data.ID)),
|
|
|
|
).Exec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ItemOut{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
return e.GetOne(ctx, data.ID)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|