mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-06 18:48:34 +00:00
items and location item count
This commit is contained in:
parent
11dcff450c
commit
f4f7123073
19 changed files with 1350 additions and 50 deletions
51
backend/internal/repo/repo_items.go
Normal file
51
backend/internal/repo/repo_items.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
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().
|
||||
Only(ctx)
|
||||
}
|
||||
|
||||
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) {
|
||||
return e.db.Item.Create().
|
||||
SetName(data.Name).
|
||||
SetDescription(data.Description).
|
||||
SetGroupID(gid).
|
||||
AddLabelIDs(data.LabelIDs...).
|
||||
SetLocationID(data.LocationID).
|
||||
Save(ctx)
|
||||
}
|
||||
|
||||
func (e *ItemsRepository) Delete(ctx context.Context, gid uuid.UUID, id uuid.UUID) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (e *ItemsRepository) Update(ctx context.Context, gid uuid.UUID, data types.ItemUpdate) (*ent.Item, error) {
|
||||
panic("implement me")
|
||||
}
|
|
@ -5,7 +5,6 @@ import (
|
|||
|
||||
"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/location"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
)
|
||||
|
@ -14,21 +13,64 @@ type EntLocationRepository struct {
|
|||
db *ent.Client
|
||||
}
|
||||
|
||||
type LocationWithCount struct {
|
||||
*ent.Location
|
||||
ItemCount int `json:"itemCount"`
|
||||
}
|
||||
|
||||
// GetALlWithCount returns all locations with item count field populated
|
||||
func (r *EntLocationRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]LocationWithCount, error) {
|
||||
query := `
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
description,
|
||||
created_at,
|
||||
updated_at,
|
||||
(
|
||||
SELECT
|
||||
COUNT(*)
|
||||
FROM
|
||||
items
|
||||
WHERE
|
||||
items.location_items = locations.id
|
||||
) as item_count
|
||||
FROM
|
||||
locations
|
||||
WHERE
|
||||
locations.group_locations = ?
|
||||
`
|
||||
|
||||
rows, err := r.db.Sql().QueryContext(ctx, query, groupId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := []LocationWithCount{}
|
||||
for rows.Next() {
|
||||
var loc ent.Location
|
||||
var ct LocationWithCount
|
||||
err := rows.Scan(&loc.ID, &loc.Name, &loc.Description, &loc.CreatedAt, &loc.UpdatedAt, &ct.ItemCount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ct.Location = &loc
|
||||
list = append(list, ct)
|
||||
}
|
||||
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (r *EntLocationRepository) Get(ctx context.Context, ID uuid.UUID) (*ent.Location, error) {
|
||||
return r.db.Location.Query().
|
||||
Where(location.ID(ID)).
|
||||
WithGroup().
|
||||
WithItems().
|
||||
WithItems(func(iq *ent.ItemQuery) {
|
||||
iq.WithLabel()
|
||||
}).
|
||||
Only(ctx)
|
||||
}
|
||||
|
||||
func (r *EntLocationRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]*ent.Location, error) {
|
||||
return r.db.Location.Query().
|
||||
Where(location.HasGroupWith(group.ID(groupId))).
|
||||
WithGroup().
|
||||
All(ctx)
|
||||
}
|
||||
|
||||
func (r *EntLocationRepository) Create(ctx context.Context, groupdId uuid.UUID, data types.LocationCreate) (*ent.Location, error) {
|
||||
location, err := r.db.Location.Create().
|
||||
SetName(data.Name).
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hay-kot/content/backend/ent"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
"github.com/hay-kot/content/backend/pkgs/faker"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -31,26 +30,30 @@ func Test_Locations_Get(t *testing.T) {
|
|||
testRepos.Locations.Delete(context.Background(), loc.ID)
|
||||
}
|
||||
|
||||
func Test_Locations_GetAll(t *testing.T) {
|
||||
created := make([]*ent.Location, 6)
|
||||
func Test_LocationsGetAllWithCount(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
result, err := testRepos.Locations.Create(ctx, testGroup.ID, types.LocationCreate{
|
||||
Name: fk.RandomString(10),
|
||||
Description: fk.RandomString(100),
|
||||
})
|
||||
|
||||
for i := 0; i < 6; i++ {
|
||||
result, err := testRepos.Locations.Create(context.Background(), testGroup.ID, types.LocationCreate{
|
||||
Name: fk.RandomString(10),
|
||||
Description: fk.RandomString(100),
|
||||
})
|
||||
testRepos.Items.Create(ctx, testGroup.ID, types.ItemCreate{
|
||||
Name: fk.RandomString(10),
|
||||
Description: fk.RandomString(100),
|
||||
LocationID: result.ID,
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
created[i] = result
|
||||
}
|
||||
|
||||
locations, err := testRepos.Locations.GetAll(context.Background(), testGroup.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 6, len(locations))
|
||||
|
||||
for _, loc := range created {
|
||||
testRepos.Locations.Delete(context.Background(), loc.ID)
|
||||
results, err := testRepos.Locations.GetAll(context.Background(), testGroup.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
for _, loc := range results {
|
||||
if loc.ID == result.ID {
|
||||
assert.Equal(t, 1, loc.ItemCount)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Locations_Create(t *testing.T) {
|
||||
|
|
|
@ -9,6 +9,7 @@ type AllRepos struct {
|
|||
Groups *EntGroupRepository
|
||||
Locations *EntLocationRepository
|
||||
Labels *EntLabelRepository
|
||||
Items *ItemsRepository
|
||||
}
|
||||
|
||||
func EntAllRepos(db *ent.Client) *AllRepos {
|
||||
|
@ -18,5 +19,6 @@ func EntAllRepos(db *ent.Client) *AllRepos {
|
|||
Groups: &EntGroupRepository{db},
|
||||
Locations: &EntLocationRepository{db},
|
||||
Labels: &EntLabelRepository{db},
|
||||
Items: &ItemsRepository{db},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ type AllServices struct {
|
|||
Admin *AdminService
|
||||
Location *LocationService
|
||||
Labels *LabelService
|
||||
Items *ItemService
|
||||
}
|
||||
|
||||
func NewServices(repos *repo.AllRepos) *AllServices {
|
||||
|
@ -15,5 +16,6 @@ func NewServices(repos *repo.AllRepos) *AllServices {
|
|||
Admin: &AdminService{repos},
|
||||
Location: &LocationService{repos},
|
||||
Labels: &LabelService{repos},
|
||||
Items: &ItemService{repos},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,12 +6,58 @@ import (
|
|||
)
|
||||
|
||||
func ToItemSummary(item *ent.Item) *types.ItemSummary {
|
||||
var location *types.LocationSummary
|
||||
if item.Edges.Location != nil {
|
||||
location = ToLocationSummary(item.Edges.Location)
|
||||
}
|
||||
|
||||
var labels []*types.LabelSummary
|
||||
if item.Edges.Label != nil {
|
||||
labels = MapEach(item.Edges.Label, ToLabelSummary)
|
||||
}
|
||||
|
||||
return &types.ItemSummary{
|
||||
ID: item.ID,
|
||||
LocationID: item.Edges.Location.ID,
|
||||
Name: item.Name,
|
||||
Description: item.Description,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
|
||||
// Edges
|
||||
Location: location,
|
||||
Labels: labels,
|
||||
|
||||
// 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,
|
||||
}
|
||||
}
|
||||
|
||||
func ToItemSummaryErr(item *ent.Item, err error) (*types.ItemSummary, error) {
|
||||
return ToItemSummary(item), err
|
||||
}
|
||||
|
||||
func ToItemOut(item *ent.Item) *types.ItemOut {
|
||||
return &types.ItemOut{
|
||||
ItemSummary: *ToItemSummary(item),
|
||||
}
|
||||
}
|
||||
|
||||
func ToItemOutErr(item *ent.Item, err error) (*types.ItemOut, error) {
|
||||
return ToItemOut(item), err
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
func ToLabelSummary(label *ent.Label) *types.LabelSummary {
|
||||
return &types.LabelSummary{
|
||||
ID: label.ID,
|
||||
GroupID: label.Edges.Group.ID,
|
||||
Name: label.Name,
|
||||
Description: label.Description,
|
||||
CreatedAt: label.CreatedAt,
|
||||
|
|
|
@ -2,13 +2,30 @@ package mappers
|
|||
|
||||
import (
|
||||
"github.com/hay-kot/content/backend/ent"
|
||||
"github.com/hay-kot/content/backend/internal/repo"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
)
|
||||
|
||||
func ToLocationCount(location *repo.LocationWithCount) *types.LocationCount {
|
||||
return &types.LocationCount{
|
||||
LocationSummary: types.LocationSummary{
|
||||
ID: location.ID,
|
||||
Name: location.Name,
|
||||
Description: location.Description,
|
||||
CreatedAt: location.CreatedAt,
|
||||
UpdatedAt: location.UpdatedAt,
|
||||
},
|
||||
ItemCount: location.ItemCount,
|
||||
}
|
||||
}
|
||||
|
||||
func ToLocationCountErr(location *repo.LocationWithCount, err error) (*types.LocationCount, error) {
|
||||
return ToLocationCount(location), err
|
||||
}
|
||||
|
||||
func ToLocationSummary(location *ent.Location) *types.LocationSummary {
|
||||
return &types.LocationSummary{
|
||||
ID: location.ID,
|
||||
GroupID: location.Edges.Group.ID,
|
||||
Name: location.Name,
|
||||
Description: location.Description,
|
||||
CreatedAt: location.CreatedAt,
|
||||
|
@ -22,8 +39,14 @@ func ToLocationSummaryErr(location *ent.Location, err error) (*types.LocationSum
|
|||
|
||||
func ToLocationOut(location *ent.Location) *types.LocationOut {
|
||||
return &types.LocationOut{
|
||||
LocationSummary: *ToLocationSummary(location),
|
||||
Items: MapEach(location.Edges.Items, ToItemSummary),
|
||||
LocationSummary: types.LocationSummary{
|
||||
ID: location.ID,
|
||||
Name: location.Name,
|
||||
Description: location.Description,
|
||||
CreatedAt: location.CreatedAt,
|
||||
UpdatedAt: location.UpdatedAt,
|
||||
},
|
||||
Items: MapEach(location.Edges.Items, ToItemSummary),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
45
backend/internal/services/service_items.go
Normal file
45
backend/internal/services/service_items.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/content/backend/internal/repo"
|
||||
"github.com/hay-kot/content/backend/internal/services/mappers"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
)
|
||||
|
||||
type ItemService struct {
|
||||
repo *repo.AllRepos
|
||||
}
|
||||
|
||||
func (svc *ItemService) GetOne(ctx context.Context, gid uuid.UUID, id uuid.UUID) (*types.ItemOut, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
func (svc *ItemService) Delete(ctx context.Context, gid uuid.UUID, id uuid.UUID) error {
|
||||
panic("implement me")
|
||||
}
|
||||
func (svc *ItemService) Update(ctx context.Context, gid uuid.UUID, data types.ItemUpdate) (*types.ItemOut, error) {
|
||||
panic("implement me")
|
||||
}
|
|
@ -32,23 +32,23 @@ func (svc *LocationService) GetOne(ctx context.Context, groupId uuid.UUID, id uu
|
|||
return mappers.ToLocationOut(location), nil
|
||||
}
|
||||
|
||||
func (svc *LocationService) GetAll(ctx context.Context, groupId uuid.UUID) ([]*types.LocationSummary, error) {
|
||||
func (svc *LocationService) GetAll(ctx context.Context, groupId uuid.UUID) ([]*types.LocationCount, error) {
|
||||
locations, err := svc.repos.Locations.GetAll(ctx, groupId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
locationsOut := make([]*types.LocationSummary, len(locations))
|
||||
locationsOut := make([]*types.LocationCount, len(locations))
|
||||
for i, location := range locations {
|
||||
locationsOut[i] = mappers.ToLocationSummary(location)
|
||||
locationsOut[i] = mappers.ToLocationCount(&location)
|
||||
}
|
||||
|
||||
return locationsOut, nil
|
||||
}
|
||||
|
||||
func (svc *LocationService) Create(ctx context.Context, groupId uuid.UUID, data types.LocationCreate) (*types.LocationSummary, error) {
|
||||
func (svc *LocationService) Create(ctx context.Context, groupId uuid.UUID, data types.LocationCreate) (*types.LocationOut, error) {
|
||||
location, err := svc.repos.Locations.Create(ctx, groupId, data)
|
||||
return mappers.ToLocationSummaryErr(location, err)
|
||||
return mappers.ToLocationOutErr(location, err)
|
||||
}
|
||||
|
||||
func (svc *LocationService) Delete(ctx context.Context, groupId uuid.UUID, id uuid.UUID) error {
|
||||
|
|
83
backend/internal/types/item_types.go
Normal file
83
backend/internal/types/item_types.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ItemCreate struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
|
||||
// Edges
|
||||
LocationID uuid.UUID `json:"locationId"`
|
||||
LabelIDs []uuid.UUID `json:"labelIds"`
|
||||
}
|
||||
|
||||
type ItemUpdate struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
|
||||
// Edges
|
||||
LocationID uuid.UUID `json:"locationId"`
|
||||
LabelIDs []uuid.UUID `json:"labelIds"`
|
||||
|
||||
// Identifications
|
||||
SerialNumber string `json:"serialNumber"`
|
||||
ModelNumber string `json:"modelNumber"`
|
||||
Manufacturer string `json:"manufacturer"`
|
||||
|
||||
// Purchase
|
||||
PurchaseTime time.Time `json:"purchaseTime"`
|
||||
PurchaseFrom string `json:"purchaseFrom"`
|
||||
PurchasePrice float64 `json:"purchasePrice"`
|
||||
|
||||
// Sold
|
||||
SoldTime time.Time `json:"soldTime"`
|
||||
SoldTo string `json:"soldTo"`
|
||||
SoldPrice float64 `json:"soldPrice"`
|
||||
SoldNotes string `json:"soldNotes"`
|
||||
|
||||
// Extras
|
||||
Notes string `json:"notes"`
|
||||
// Fields []*FieldSummary `json:"fields"`
|
||||
}
|
||||
|
||||
type ItemSummary struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
|
||||
// Edges
|
||||
Location *LocationSummary `json:"location"`
|
||||
Labels []*LabelSummary `json:"labels"`
|
||||
|
||||
// Identifications
|
||||
SerialNumber string `json:"serialNumber"`
|
||||
ModelNumber string `json:"modelNumber"`
|
||||
Manufacturer string `json:"manufacturer"`
|
||||
|
||||
// Purchase
|
||||
PurchaseTime time.Time `json:"purchaseTime"`
|
||||
PurchaseFrom string `json:"purchaseFrom"`
|
||||
PurchasePrice float64 `json:"purchasePrice"`
|
||||
|
||||
// Sold
|
||||
SoldTime time.Time `json:"soldTime"`
|
||||
SoldTo string `json:"soldTo"`
|
||||
SoldPrice float64 `json:"soldPrice"`
|
||||
SoldNotes string `json:"soldNotes"`
|
||||
|
||||
// Extras
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
type ItemOut struct {
|
||||
ItemSummary
|
||||
// Future
|
||||
// Fields []*FieldSummary `json:"fields"`
|
||||
}
|
|
@ -19,20 +19,15 @@ type LocationUpdate struct {
|
|||
|
||||
type LocationSummary struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
GroupID uuid.UUID `json:"groupId"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type ItemSummary struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
LocationID uuid.UUID `json:"locationId"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
type LocationCount struct {
|
||||
LocationSummary
|
||||
ItemCount int `json:"itemCount"`
|
||||
}
|
||||
|
||||
type LocationOut struct {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue