2022-09-03 09:17:48 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-24 19:33:38 +00:00
|
|
|
"errors"
|
2022-09-06 19:15:07 +00:00
|
|
|
"fmt"
|
2022-09-03 09:17:48 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/repo"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/services/mappers"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/types"
|
2022-09-06 19:15:07 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-09-03 09:17:48 +00:00
|
|
|
)
|
|
|
|
|
2022-09-24 19:33:38 +00:00
|
|
|
var (
|
|
|
|
ErrNotFound = errors.New("not found")
|
|
|
|
ErrFileNotFound = errors.New("file not found")
|
|
|
|
)
|
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
type ItemService struct {
|
|
|
|
repo *repo.AllRepos
|
2022-09-12 22:47:27 +00:00
|
|
|
|
|
|
|
filepath string
|
2022-09-24 19:33:38 +00:00
|
|
|
// at is a map of tokens to attachment IDs. This is used to store the attachment ID
|
|
|
|
// for issued URLs
|
|
|
|
at attachmentTokens
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *ItemService) GetOne(ctx context.Context, gid uuid.UUID, id uuid.UUID) (*types.ItemOut, error) {
|
2022-09-06 19:15:07 +00:00
|
|
|
result, err := svc.repo.Items.GetOne(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Edges.Group.ID != gid {
|
|
|
|
return nil, ErrNotOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
return mappers.ToItemOut(result), nil
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
2022-09-06 19:15:07 +00:00
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
func (svc *ItemService) GetAll(ctx context.Context, gid uuid.UUID) ([]*types.ItemSummary, error) {
|
|
|
|
items, err := svc.repo.Items.GetAll(ctx, gid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
itemsOut := make([]*types.ItemSummary, len(items))
|
|
|
|
for i, item := range items {
|
|
|
|
itemsOut[i] = mappers.ToItemSummary(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemsOut, nil
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
func (svc *ItemService) Create(ctx context.Context, gid uuid.UUID, data types.ItemCreate) (*types.ItemOut, error) {
|
|
|
|
item, err := svc.repo.Items.Create(ctx, gid, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mappers.ToItemOut(item), nil
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
func (svc *ItemService) Delete(ctx context.Context, gid uuid.UUID, id uuid.UUID) error {
|
2022-09-09 06:05:23 +00:00
|
|
|
item, err := svc.repo.Items.GetOne(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.Edges.Group.ID != gid {
|
|
|
|
return ErrNotOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svc.repo.Items.Delete(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
func (svc *ItemService) Update(ctx context.Context, gid uuid.UUID, data types.ItemUpdate) (*types.ItemOut, error) {
|
2022-09-12 22:47:27 +00:00
|
|
|
item, err := svc.repo.Items.GetOne(ctx, data.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.Edges.Group.ID != gid {
|
|
|
|
return nil, ErrNotOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
item, err = svc.repo.Items.Update(ctx, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mappers.ToItemOut(item), nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:15:07 +00:00
|
|
|
func (svc *ItemService) CsvImport(ctx context.Context, gid uuid.UUID, data [][]string) error {
|
|
|
|
loaded := []csvRow{}
|
|
|
|
|
|
|
|
// Skip first row
|
|
|
|
for _, row := range data[1:] {
|
|
|
|
// Skip empty rows
|
|
|
|
if len(row) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-09-13 04:54:30 +00:00
|
|
|
|
|
|
|
if len(row) != NumOfCols {
|
2022-09-06 19:15:07 +00:00
|
|
|
return ErrInvalidCsv
|
|
|
|
}
|
|
|
|
|
|
|
|
r := newCsvRow(row)
|
|
|
|
loaded = append(loaded, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bootstrap the locations and labels so we can reuse the created IDs for the items
|
|
|
|
locations := map[string]uuid.UUID{}
|
|
|
|
existingLocation, err := svc.repo.Locations.GetAll(ctx, gid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, loc := range existingLocation {
|
|
|
|
locations[loc.Name] = loc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := map[string]uuid.UUID{}
|
|
|
|
existingLabels, err := svc.repo.Labels.GetAll(ctx, gid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, label := range existingLabels {
|
|
|
|
labels[label.Name] = label.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, row := range loaded {
|
|
|
|
|
|
|
|
// Locations
|
|
|
|
if _, ok := locations[row.Location]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Creating Location: ", row.Location)
|
|
|
|
|
|
|
|
result, err := svc.repo.Locations.Create(ctx, gid, types.LocationCreate{
|
|
|
|
Name: row.Location,
|
|
|
|
Description: "",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
locations[row.Location] = result.ID
|
|
|
|
|
|
|
|
// Labels
|
|
|
|
|
|
|
|
for _, label := range row.getLabels() {
|
|
|
|
if _, ok := labels[label]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result, err := svc.repo.Labels.Create(ctx, gid, types.LabelCreate{
|
|
|
|
Name: label,
|
|
|
|
Description: "",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
labels[label] = result.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the items
|
|
|
|
for _, row := range loaded {
|
|
|
|
locationID := locations[row.Location]
|
|
|
|
labelIDs := []uuid.UUID{}
|
|
|
|
for _, label := range row.getLabels() {
|
|
|
|
labelIDs = append(labelIDs, labels[label])
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().
|
2022-09-13 04:54:30 +00:00
|
|
|
Str("name", row.Item.Name).
|
2022-09-06 19:15:07 +00:00
|
|
|
Str("location", row.Location).
|
|
|
|
Strs("labels", row.getLabels()).
|
|
|
|
Str("locationId", locationID.String()).
|
2022-09-13 04:54:30 +00:00
|
|
|
Msgf("Creating Item: %s", row.Item.Name)
|
2022-09-06 19:15:07 +00:00
|
|
|
|
|
|
|
result, err := svc.repo.Items.Create(ctx, gid, types.ItemCreate{
|
2022-09-13 04:54:30 +00:00
|
|
|
ImportRef: row.Item.ImportRef,
|
|
|
|
Name: row.Item.Name,
|
|
|
|
Description: row.Item.Description,
|
2022-09-06 19:15:07 +00:00
|
|
|
LabelIDs: labelIDs,
|
|
|
|
LocationID: locationID,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the item with the rest of the data
|
|
|
|
_, err = svc.repo.Items.Update(ctx, types.ItemUpdate{
|
2022-09-13 04:54:30 +00:00
|
|
|
// Edges
|
|
|
|
LocationID: locationID,
|
|
|
|
LabelIDs: labelIDs,
|
|
|
|
|
|
|
|
// General Fields
|
|
|
|
ID: result.ID,
|
|
|
|
Name: result.Name,
|
|
|
|
Description: result.Description,
|
|
|
|
Insured: row.Item.Insured,
|
|
|
|
Notes: row.Item.Notes,
|
|
|
|
|
|
|
|
// Identifies the item as imported
|
|
|
|
SerialNumber: row.Item.SerialNumber,
|
|
|
|
ModelNumber: row.Item.ModelNumber,
|
|
|
|
Manufacturer: row.Item.Manufacturer,
|
|
|
|
|
|
|
|
// Purchase
|
|
|
|
PurchaseFrom: row.Item.PurchaseFrom,
|
|
|
|
PurchasePrice: row.Item.PurchasePrice,
|
|
|
|
PurchaseTime: row.Item.PurchaseTime,
|
|
|
|
|
|
|
|
// Warranty
|
|
|
|
LifetimeWarranty: row.Item.LifetimeWarranty,
|
|
|
|
WarrantyExpires: row.Item.WarrantyExpires,
|
|
|
|
WarrantyDetails: row.Item.WarrantyDetails,
|
|
|
|
|
|
|
|
SoldTo: row.Item.SoldTo,
|
|
|
|
SoldPrice: row.Item.SoldPrice,
|
|
|
|
SoldTime: row.Item.SoldTime,
|
|
|
|
SoldNotes: row.Item.SoldNotes,
|
2022-09-06 19:15:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|