forked from mirrors/homebox
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},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue