mirror of
https://github.com/hay-kot/homebox.git
synced 2024-12-01 04:55:40 +00:00
conditionally increment asset IDs
This commit is contained in:
parent
46eaef12eb
commit
de670a00ed
3 changed files with 59 additions and 6 deletions
|
@ -8,17 +8,38 @@ type AllServices struct {
|
||||||
Items *ItemService
|
Items *ItemService
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(repos *repo.AllRepos) *AllServices {
|
type OptionsFunc func(*options)
|
||||||
|
|
||||||
|
type options struct {
|
||||||
|
autoIncrementAssetID bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithAutoIncrementAssetID(v bool) func(*options) {
|
||||||
|
return func(o *options) {
|
||||||
|
o.autoIncrementAssetID = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(repos *repo.AllRepos, opts ...OptionsFunc) *AllServices {
|
||||||
if repos == nil {
|
if repos == nil {
|
||||||
panic("repos cannot be nil")
|
panic("repos cannot be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options := &options{
|
||||||
|
autoIncrementAssetID: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(options)
|
||||||
|
}
|
||||||
|
|
||||||
return &AllServices{
|
return &AllServices{
|
||||||
User: &UserService{repos},
|
User: &UserService{repos},
|
||||||
Group: &GroupService{repos},
|
Group: &GroupService{repos},
|
||||||
Items: &ItemService{
|
Items: &ItemService{
|
||||||
repo: repos,
|
repo: repos,
|
||||||
at: attachmentTokens{},
|
at: attachmentTokens{},
|
||||||
|
autoIncrementAssetID: options.autoIncrementAssetID,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,21 @@ type ItemService struct {
|
||||||
// at is a map of tokens to attachment IDs. This is used to store the attachment ID
|
// at is a map of tokens to attachment IDs. This is used to store the attachment ID
|
||||||
// for issued URLs
|
// for issued URLs
|
||||||
at attachmentTokens
|
at attachmentTokens
|
||||||
|
|
||||||
|
autoIncrementAssetID bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (svc *ItemService) Create(ctx Context, item repo.ItemCreate) (repo.ItemOut, error) {
|
||||||
|
if svc.autoIncrementAssetID {
|
||||||
|
highest, err := svc.repo.Items.GetHighestAssetID(ctx, ctx.GID)
|
||||||
|
if err != nil {
|
||||||
|
return repo.ItemOut{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
item.AssetID = repo.AssetID(highest + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return svc.repo.Items.Create(ctx, ctx.GID, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *ItemService) EnsureAssetID(ctx context.Context, GID uuid.UUID) (int, error) {
|
func (svc *ItemService) EnsureAssetID(ctx context.Context, GID uuid.UUID) (int, error) {
|
||||||
|
@ -140,6 +155,14 @@ func (svc *ItemService) CsvImport(ctx context.Context, GID uuid.UUID, data [][]s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
highest := repo.AssetID(-1)
|
||||||
|
if svc.autoIncrementAssetID {
|
||||||
|
highest, err = svc.repo.Items.GetHighestAssetID(ctx, GID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create the items
|
// Create the items
|
||||||
var count int
|
var count int
|
||||||
for _, row := range loaded {
|
for _, row := range loaded {
|
||||||
|
@ -165,13 +188,20 @@ func (svc *ItemService) CsvImport(ctx context.Context, GID uuid.UUID, data [][]s
|
||||||
Str("location", row.Location).
|
Str("location", row.Location).
|
||||||
Msgf("Creating Item: %s", row.Item.Name)
|
Msgf("Creating Item: %s", row.Item.Name)
|
||||||
|
|
||||||
result, err := svc.repo.Items.Create(ctx, GID, repo.ItemCreate{
|
data := repo.ItemCreate{
|
||||||
ImportRef: row.Item.ImportRef,
|
ImportRef: row.Item.ImportRef,
|
||||||
Name: row.Item.Name,
|
Name: row.Item.Name,
|
||||||
Description: row.Item.Description,
|
Description: row.Item.Description,
|
||||||
LabelIDs: labelIDs,
|
LabelIDs: labelIDs,
|
||||||
LocationID: locationID,
|
LocationID: locationID,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
if svc.autoIncrementAssetID {
|
||||||
|
highest++
|
||||||
|
data.AssetID = highest
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := svc.repo.Items.Create(ctx, GID, data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return count, err
|
return count, err
|
||||||
|
|
|
@ -71,6 +71,7 @@ type (
|
||||||
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
|
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
AssetID AssetID `json:"-"`
|
||||||
|
|
||||||
// Edges
|
// Edges
|
||||||
LocationID uuid.UUID `json:"locationId"`
|
LocationID uuid.UUID `json:"locationId"`
|
||||||
|
@ -431,7 +432,8 @@ func (e *ItemsRepository) Create(ctx context.Context, gid uuid.UUID, data ItemCr
|
||||||
SetName(data.Name).
|
SetName(data.Name).
|
||||||
SetDescription(data.Description).
|
SetDescription(data.Description).
|
||||||
SetGroupID(gid).
|
SetGroupID(gid).
|
||||||
SetLocationID(data.LocationID)
|
SetLocationID(data.LocationID).
|
||||||
|
SetAssetID(int(data.AssetID))
|
||||||
|
|
||||||
if data.LabelIDs != nil && len(data.LabelIDs) > 0 {
|
if data.LabelIDs != nil && len(data.LabelIDs) > 0 {
|
||||||
q.AddLabelIDs(data.LabelIDs...)
|
q.AddLabelIDs(data.LabelIDs...)
|
||||||
|
|
Loading…
Reference in a new issue