This commit is contained in:
mr
2026-03-04 13:51:43 +01:00
parent 5d18512f67
commit 2bfcfb5736
8 changed files with 16 additions and 16 deletions

View File

@@ -87,17 +87,17 @@ func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
return res, 200, nil
}
func ModelGenericUpdateOne(change map[string]interface{}, id string, a Accessor) (map[string]interface{}, int, error) {
func ModelGenericUpdateOne(change map[string]interface{}, id string, a Accessor) (DBObject, map[string]interface{}, int, error) {
r, c, err := a.LoadOne(id)
if err != nil {
return nil, c, err
return nil, nil, c, err
}
obj := &AbstractObject{}
b, _ := json.Marshal(r)
json.Unmarshal(b, obj)
ok, r := r.CanUpdate(obj)
if !ok {
return nil, 403, errors.New("you are not allowed to update :" + a.GetType().String())
return nil, nil, 403, errors.New("you are not allowed to update :" + a.GetType().String())
}
r.UpToDate(a.GetUser(), a.GetPeerID(), false)
if a.GetPeerID() == r.GetCreatorID() {
@@ -105,24 +105,24 @@ func ModelGenericUpdateOne(change map[string]interface{}, id string, a Accessor)
r.Sign()
}
if a.ShouldVerifyAuth() && !r.VerifyAuth("update", a.GetRequest()) {
return nil, 403, errors.New("you are not allowed to access :" + a.GetType().String())
return nil, nil, 403, errors.New("you are not allowed to access :" + a.GetType().String())
}
loaded := r.Serialize(r) // get the loaded object
for k, v := range change { // apply the changes, with a flatten method
loaded[k] = v
}
return loaded, 200, nil
return r, loaded, 200, nil
}
// GenericLoadOne loads one object from the database (generic)
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
func GenericUpdateOne(change map[string]interface{}, id string, a Accessor, new DBObject) (DBObject, int, error) {
loaded, c, err := ModelGenericUpdateOne(change, id, a)
func GenericUpdateOne(change map[string]interface{}, id string, a Accessor) (DBObject, int, error) {
obj, loaded, c, err := ModelGenericUpdateOne(change, id, a)
if err != nil {
return nil, c, err
}
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded, new), id, a.GetType().String())
id, code, err := mongo.MONGOService.UpdateOne(obj.Deserialize(loaded, obj), id, a.GetType().String())
if err != nil {
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
return nil, code, err