Update try

This commit is contained in:
mr
2026-03-02 15:46:05 +01:00
parent cb3771c17a
commit 44812309db
19 changed files with 64 additions and 80 deletions

View File

@@ -227,7 +227,7 @@ func (a *AbstractAccessor[T]) DeleteOne(id string) (DBObject, int, error) {
return GenericDeleteOne(id, a)
}
func (a *AbstractAccessor[T]) UpdateOne(set DBObject, id string) (DBObject, int, error) {
func (a *AbstractAccessor[T]) UpdateOne(set map[string]interface{}, id string) (DBObject, int, error) {
if len(a.NotImplemented) > 0 && slices.Contains(a.NotImplemented, "UpdateOne") {
return nil, 404, errors.New("not implemented")
}

View File

@@ -86,18 +86,15 @@ func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
return res, 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(set DBObject, id string, a Accessor, new DBObject) (DBObject, int, error) {
func ModelGenericUpdateOne(change map[string]interface{}, id string, a Accessor) (map[string]interface{}, int, error) {
r, c, err := a.LoadOne(id)
if err != nil {
return nil, c, err
}
ok, newSet := r.CanUpdate(set)
ok, r := r.CanUpdate(r)
if !ok {
return nil, 403, errors.New("you are not allowed to delete :" + a.GetType().String())
}
set = newSet
r.UpToDate(a.GetUser(), a.GetPeerID(), false)
if a.GetPeerID() == r.GetCreatorID() {
r.Unsign()
@@ -106,12 +103,21 @@ func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObje
if a.ShouldVerifyAuth() && !r.VerifyAuth("update", a.GetRequest()) {
return nil, 403, errors.New("you are not allowed to access :" + a.GetType().String())
}
change := set.Serialize(set) // get the changes
loaded := r.Serialize(r) // get the loaded object
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
}
// 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)
if err != nil {
return nil, c, err
}
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded, new), id, a.GetType().String())
if err != nil {
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())

View File

@@ -53,7 +53,7 @@ type Accessor interface {
CopyOne(data DBObject) (DBObject, int, error)
StoreOne(data DBObject) (DBObject, int, error)
LoadAll(isDraft bool) ([]ShallowDBObject, int, error)
UpdateOne(set DBObject, id string) (DBObject, int, error)
UpdateOne(set map[string]interface{}, id string) (DBObject, int, error)
Search(filters *dbs.Filters, search string, isDraft bool) ([]ShallowDBObject, int, error)
GetExec(isDraft bool) func(DBObject) ShallowDBObject
}