Skip to content

Commit

Permalink
CheckVersionandUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
sd-cyberinc committed Feb 9, 2021
1 parent 36ce91f commit 264b9e1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions repository/Repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repository

import (
"errors"
"fmt"
"net/http"
"strconv"
Expand All @@ -24,6 +25,7 @@ type Repository interface {
GetAllUnscopedForTenant(uow *UnitOfWork, out interface{}, tenantID uuid.UUID, queryProcessors []QueryProcessor) microappError.DatabaseError
GetCount(uow *UnitOfWork, out *int64, entity interface{}, queryProcessors []QueryProcessor) microappError.DatabaseError
GetCountForTenant(uow *UnitOfWork, out *int64, tenantID uuid.UUID, entity interface{}, queryProcessors []QueryProcessor) microappError.DatabaseError
CheckVersionandUpdate(uow *UnitOfWork, entity interface{}, queryProcessors []QueryProcessor) microappError.DatabaseError

Add(uow *UnitOfWork, out interface{}) microappError.DatabaseError
Update(uow *UnitOfWork, out interface{}) microappError.DatabaseError
Expand Down Expand Up @@ -377,6 +379,21 @@ func (repository *GormRepository) Update(uow *UnitOfWork, entity interface{}) mi
return nil
}

// CheckVersionandUpdate specified Entity after checking for version change
func (repository *GormRepository) CheckVersionandUpdate(uow *UnitOfWork, entity interface{}, queryProcessors []QueryProcessor) microappError.DatabaseError {
var count int64
if err := repository.GetCount(uow, &count, entity, queryProcessors); err != nil {
return microappError.NewDatabaseError(err)
}
if count == 0 {
return microappError.NewDatabaseError(errors.New("row data modified"))
}
if err := uow.DB.Model(entity).Updates(entity).Error; err != nil {
return microappError.NewDatabaseError(err)
}
return nil
}

// Delete specified Entity
func (repository *GormRepository) Delete(uow *UnitOfWork, entity interface{}, where ...interface{}) microappError.DatabaseError {
if err := uow.DB.Delete(entity, where...).Error; err != nil {
Expand Down

0 comments on commit 264b9e1

Please sign in to comment.