mirror of
https://github.com/hay-kot/homebox.git
synced 2025-06-28 14:48:34 +00:00
feat: enhanced search functions (#260)
* make login case insensitive * expand query to support by Field and By AID search * type generation * new API callers * rework search to support field queries * improve unnecessary data fetches * clear stores on logout * change verbage * add labels
This commit is contained in:
parent
7b28973c60
commit
bd06fdafaf
18 changed files with 637 additions and 133 deletions
|
@ -8,11 +8,34 @@ import (
|
|||
|
||||
type AssetID int
|
||||
|
||||
func (aid AssetID) Nil() bool {
|
||||
return aid.Int() <= 0
|
||||
}
|
||||
|
||||
func (aid AssetID) Int() int {
|
||||
return int(aid)
|
||||
}
|
||||
|
||||
func ParseAssetIDBytes(d []byte) (AID AssetID, ok bool) {
|
||||
d = bytes.Replace(d, []byte(`"`), []byte(``), -1)
|
||||
d = bytes.Replace(d, []byte(`-`), []byte(``), -1)
|
||||
|
||||
aidInt, err := strconv.Atoi(string(d))
|
||||
if err != nil {
|
||||
return AssetID(-1), false
|
||||
}
|
||||
|
||||
return AssetID(aidInt), true
|
||||
}
|
||||
|
||||
func ParseAssetID(s string) (AID AssetID, ok bool) {
|
||||
return ParseAssetIDBytes([]byte(s))
|
||||
}
|
||||
|
||||
func (aid AssetID) MarshalJSON() ([]byte, error) {
|
||||
aidStr := fmt.Sprintf("%06d", aid)
|
||||
aidStr = fmt.Sprintf("%s-%s", aidStr[:3], aidStr[3:])
|
||||
return []byte(fmt.Sprintf(`"%s"`, aidStr)), nil
|
||||
|
||||
}
|
||||
|
||||
func (aid *AssetID) UnmarshalJSON(d []byte) error {
|
||||
|
@ -26,5 +49,4 @@ func (aid *AssetID) UnmarshalJSON(d []byte) error {
|
|||
|
||||
*aid = AssetID(aidInt)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package repo
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
@ -19,14 +20,21 @@ type ItemsRepository struct {
|
|||
}
|
||||
|
||||
type (
|
||||
FieldQuery struct {
|
||||
Name string
|
||||
Value string
|
||||
}
|
||||
|
||||
ItemQuery struct {
|
||||
Page int
|
||||
PageSize int
|
||||
Search string `json:"search"`
|
||||
AssetID AssetID `json:"assetId"`
|
||||
LocationIDs []uuid.UUID `json:"locationIds"`
|
||||
LabelIDs []uuid.UUID `json:"labelIds"`
|
||||
SortBy string `json:"sortBy"`
|
||||
IncludeArchived bool `json:"includeArchived"`
|
||||
Fields []FieldQuery
|
||||
}
|
||||
|
||||
ItemField struct {
|
||||
|
@ -326,7 +334,26 @@ func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q Ite
|
|||
)
|
||||
}
|
||||
|
||||
if !q.AssetID.Nil() {
|
||||
qb = qb.Where(item.AssetID(q.AssetID.Int()))
|
||||
}
|
||||
|
||||
if len(q.Fields) > 0 {
|
||||
predicates := make([]predicate.Item, 0, len(q.Fields))
|
||||
for _, f := range q.Fields {
|
||||
predicates = append(predicates, item.HasFieldsWith(
|
||||
itemfield.And(
|
||||
itemfield.Name(f.Name),
|
||||
itemfield.TextValue(f.Value),
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
qb = qb.Where(item.Or(predicates...))
|
||||
}
|
||||
|
||||
count, err := qb.Count(ctx)
|
||||
|
||||
if err != nil {
|
||||
return PaginationResult[ItemSummary]{}, err
|
||||
}
|
||||
|
@ -473,8 +500,8 @@ func (e *ItemsRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID)
|
|||
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))).
|
||||
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))).
|
||||
SetName(data.Name).
|
||||
SetDescription(data.Description).
|
||||
SetLocationID(data.LocationID).
|
||||
|
@ -587,3 +614,62 @@ func (e *ItemsRepository) UpdateByGroup(ctx context.Context, gid uuid.UUID, data
|
|||
|
||||
return e.GetOne(ctx, data.ID)
|
||||
}
|
||||
|
||||
func (e *ItemsRepository) GetAllCustomFieldValues(ctx context.Context, GID uuid.UUID, name string) ([]string, error) {
|
||||
type st struct {
|
||||
Value string `json:"text_value"`
|
||||
}
|
||||
|
||||
var values []st
|
||||
|
||||
err := e.db.Item.Query().
|
||||
Where(
|
||||
item.HasGroupWith(group.ID(GID)),
|
||||
).
|
||||
QueryFields().
|
||||
Where(
|
||||
itemfield.Name(name),
|
||||
).
|
||||
Unique(true).
|
||||
Select(itemfield.FieldTextValue).
|
||||
Scan(ctx, &values)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get field values: %w", err)
|
||||
}
|
||||
|
||||
valueStrings := make([]string, len(values))
|
||||
for i, f := range values {
|
||||
valueStrings[i] = f.Value
|
||||
}
|
||||
|
||||
return valueStrings, nil
|
||||
}
|
||||
|
||||
func (e *ItemsRepository) GetAllCustomFieldNames(ctx context.Context, GID uuid.UUID) ([]string, error) {
|
||||
type st struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
var fields []st
|
||||
|
||||
err := e.db.Debug().Item.Query().
|
||||
Where(
|
||||
item.HasGroupWith(group.ID(GID)),
|
||||
).
|
||||
QueryFields().
|
||||
Unique(true).
|
||||
Select(itemfield.FieldName).
|
||||
Scan(ctx, &fields)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get custom fields: %w", err)
|
||||
}
|
||||
|
||||
fieldNames := make([]string, len(fields))
|
||||
for i, f := range fields {
|
||||
fieldNames[i] = f.Name
|
||||
}
|
||||
|
||||
return fieldNames, nil
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func itemFactory() ItemCreate {
|
||||
|
@ -273,3 +274,48 @@ func TestItemsRepository_Update(t *testing.T) {
|
|||
assert.Equal(t, updateData.WarrantyDetails, got.WarrantyDetails)
|
||||
assert.Equal(t, updateData.LifetimeWarranty, got.LifetimeWarranty)
|
||||
}
|
||||
|
||||
func TestItemRepository_GetAllCustomFields(t *testing.T) {
|
||||
const FIELDS_COUNT = 5
|
||||
|
||||
entity := useItems(t, 1)[0]
|
||||
|
||||
fields := make([]ItemField, FIELDS_COUNT)
|
||||
names := make([]string, FIELDS_COUNT)
|
||||
values := make([]string, FIELDS_COUNT)
|
||||
|
||||
for i := 0; i < FIELDS_COUNT; i++ {
|
||||
name := fk.Str(10)
|
||||
fields[i] = ItemField{
|
||||
Name: name,
|
||||
Type: "text",
|
||||
TextValue: fk.Str(10),
|
||||
}
|
||||
names[i] = name
|
||||
values[i] = fields[i].TextValue
|
||||
}
|
||||
|
||||
_, err := tRepos.Items.UpdateByGroup(context.Background(), tGroup.ID, ItemUpdate{
|
||||
ID: entity.ID,
|
||||
Name: entity.Name,
|
||||
LocationID: entity.Location.ID,
|
||||
Fields: fields,
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
// Test getting all fields
|
||||
{
|
||||
results, err := tRepos.Items.GetAllCustomFieldNames(context.Background(), tGroup.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, names, results)
|
||||
}
|
||||
|
||||
// Test getting all values from field
|
||||
{
|
||||
results, err := tRepos.Items.GetAllCustomFieldValues(context.Background(), tUser.GroupID, names[0])
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, values[:1], results)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ func (e *UserRepository) GetOneId(ctx context.Context, id uuid.UUID) (UserOut, e
|
|||
|
||||
func (e *UserRepository) GetOneEmail(ctx context.Context, email string) (UserOut, error) {
|
||||
return mapUserOutErr(e.db.User.Query().
|
||||
Where(user.Email(email)).
|
||||
Where(user.EmailEqualFold(email)).
|
||||
WithGroup().
|
||||
Only(ctx),
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue