Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
91b2ba30f6 | ||
|
c2828592ac |
@@ -159,6 +159,15 @@ func (c *Collection[T]) Get(tx *Snapshot, id uint64) *T {
|
||||
return c.ByID.Get(tx, item)
|
||||
}
|
||||
|
||||
func (c *Collection[T]) Has(tx *Snapshot, id uint64) bool {
|
||||
if tx == nil {
|
||||
tx = c.db.Snapshot()
|
||||
}
|
||||
item := new(T)
|
||||
c.setID(item, id)
|
||||
return c.ByID.Has(tx, item)
|
||||
}
|
||||
|
||||
func (c *Collection[T]) Insert(tx *Snapshot, userItem *T) error {
|
||||
if tx == nil {
|
||||
return c.db.Update(func(tx *Snapshot) error {
|
||||
@@ -237,6 +246,27 @@ func (c *Collection[T]) update(tx *Snapshot, userItem *T) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Collection[T]) UpdateFunc(tx *Snapshot, id uint64, update func(item *T) error) error {
|
||||
if tx == nil {
|
||||
return c.db.Update(func(tx *Snapshot) error {
|
||||
return c.updateFunc(tx, id, update)
|
||||
})
|
||||
}
|
||||
return c.updateFunc(tx, id, update)
|
||||
}
|
||||
|
||||
func (c *Collection[T]) updateFunc(tx *Snapshot, id uint64, update func(item *T) error) error {
|
||||
item := c.Get(tx, id)
|
||||
if item == nil {
|
||||
return errs.NotFound
|
||||
}
|
||||
if err := update(item); err != nil {
|
||||
return err
|
||||
}
|
||||
c.setID(item, id) // Don't allow the ID to change.
|
||||
return c.update(tx, item)
|
||||
}
|
||||
|
||||
func (c *Collection[T]) Upsert(tx *Snapshot, item *T) error {
|
||||
if tx == nil {
|
||||
return c.db.Update(func(tx *Snapshot) error {
|
||||
|
Reference in New Issue
Block a user