sub delete for loop
This commit is contained in:
@@ -30,7 +30,7 @@ func NewAccessor(request *tools.APIRequest) *PurchaseResourceMongoAccessor {
|
|||||||
func (a *PurchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (a *PurchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
return utils.GenericLoadOne(id, a.New(), func(d utils.DBObject) (utils.DBObject, int, error) {
|
return utils.GenericLoadOne(id, a.New(), func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||||
utils.GenericDeleteOne(id, a)
|
utils.GenericDelete(d, a)
|
||||||
return nil, 404, nil
|
return nil, 404, nil
|
||||||
}
|
}
|
||||||
return d, 200, nil
|
return d, 200, nil
|
||||||
@@ -40,7 +40,7 @@ func (a *PurchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int,
|
|||||||
func (a *PurchaseResourceMongoAccessor) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
|
func (a *PurchaseResourceMongoAccessor) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
|
||||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||||
utils.GenericDeleteOne(d.GetID(), a)
|
utils.GenericDelete(d, a)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return d
|
return d
|
||||||
|
|||||||
@@ -82,18 +82,22 @@ func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
|
|||||||
if res == nil {
|
if res == nil {
|
||||||
return res, code, errors.New("not found")
|
return res, code, errors.New("not found")
|
||||||
}
|
}
|
||||||
|
return GenericDelete(res, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenericDelete(res DBObject, a Accessor) (DBObject, int, error) {
|
||||||
if !res.CanDelete() {
|
if !res.CanDelete() {
|
||||||
return nil, 403, errors.New("you are not allowed to delete :" + a.GetType().String())
|
return nil, 403, errors.New("you are not allowed to delete :" + a.GetType().String())
|
||||||
}
|
}
|
||||||
if a.ShouldVerifyAuth() && !res.VerifyAuth("delete", a.GetRequest()) {
|
if a.ShouldVerifyAuth() && !res.VerifyAuth("delete", a.GetRequest()) {
|
||||||
return nil, 403, errors.New("you are not allowed to access " + a.GetType().String())
|
return nil, 403, errors.New("you are not allowed to access " + a.GetType().String())
|
||||||
}
|
}
|
||||||
_, code, err = mongo.MONGOService.DeleteOne(id, a.GetType().String())
|
_, code, err := mongo.MONGOService.DeleteOne(res.GetID(), a.GetType().String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.GetLogger().Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
|
a.GetLogger().Error().Msg("Could not delete " + res.GetID() + " to db. Error: " + err.Error())
|
||||||
return nil, code, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
go NotifyChange(a.GetType(), id, res, true)
|
go NotifyChange(a.GetType(), res.GetID(), res, true)
|
||||||
return res, 200, nil
|
return res, 200, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user