Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
5ffd50bdea | ||
|
91b2ba30f6 | ||
|
c2828592ac |
@@ -159,6 +159,15 @@ func (c *Collection[T]) Get(tx *Snapshot, id uint64) *T {
|
|||||||
return c.ByID.Get(tx, item)
|
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 {
|
func (c *Collection[T]) Insert(tx *Snapshot, userItem *T) error {
|
||||||
if tx == nil {
|
if tx == nil {
|
||||||
return c.db.Update(func(tx *Snapshot) error {
|
return c.db.Update(func(tx *Snapshot) error {
|
||||||
@@ -237,6 +246,27 @@ func (c *Collection[T]) update(tx *Snapshot, userItem *T) error {
|
|||||||
return nil
|
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 {
|
func (c *Collection[T]) Upsert(tx *Snapshot, item *T) error {
|
||||||
if tx == nil {
|
if tx == nil {
|
||||||
return c.db.Update(func(tx *Snapshot) error {
|
return c.db.Update(func(tx *Snapshot) error {
|
||||||
@@ -257,6 +287,36 @@ func (c *Collection[T]) upsert(tx *Snapshot, item *T) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Collection[T]) UpsertFunc(tx *Snapshot, id uint64, update func(item *T) error) error {
|
||||||
|
if tx == nil {
|
||||||
|
c.db.Update(func(tx *Snapshot) error {
|
||||||
|
return c.upsertFunc(tx, id, update)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return c.upsertFunc(tx, id, update)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collection[T]) upsertFunc(tx *Snapshot, id uint64, update func(item *T) error) error {
|
||||||
|
insert := false
|
||||||
|
|
||||||
|
item := c.Get(tx, id)
|
||||||
|
if item == nil {
|
||||||
|
item = new(T)
|
||||||
|
insert = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := update(item); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.setID(item, id) // Don't allow the ID to change.
|
||||||
|
|
||||||
|
if insert {
|
||||||
|
return c.insert(tx, item)
|
||||||
|
}
|
||||||
|
return c.update(tx, item)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Collection[T]) Delete(tx *Snapshot, itemID uint64) error {
|
func (c *Collection[T]) Delete(tx *Snapshot, itemID uint64) error {
|
||||||
if tx == nil {
|
if tx == nil {
|
||||||
return c.db.Update(func(tx *Snapshot) error {
|
return c.db.Update(func(tx *Snapshot) error {
|
||||||
|
Reference in New Issue
Block a user