161 lines
5.6 KiB
Go
161 lines
5.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
|
mgb "go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
type Owner struct {
|
|
Name string `json:"name,omitempty" bson:"name,omitempty"`
|
|
Logo string `json:"logo,omitempty" bson:"logo,omitempty"`
|
|
}
|
|
|
|
// GenericLoadOne loads one object from the database (generic)
|
|
func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
|
|
data.GenerateID()
|
|
data.StoreDraftDefault()
|
|
data.UpToDate(a.GetUser(), true)
|
|
f := dbs.Filters{
|
|
Or: map[string][]dbs.Filter{
|
|
"abstractresource.abstractobject.name": {{
|
|
Operator: dbs.LIKE.String(),
|
|
Value: data.GetName(),
|
|
}},
|
|
"abstractobject.name": {{
|
|
Operator: dbs.LIKE.String(),
|
|
Value: data.GetName(),
|
|
}},
|
|
},
|
|
}
|
|
if !data.VerifyAuth(a.GetRequest()) {
|
|
return nil, 403, errors.New("you are not allowed to access this collaborative area")
|
|
}
|
|
if cursor, _, _ := a.Search(&f, "", data.IsDrafted()); len(cursor) > 0 {
|
|
return nil, 409, errors.New(a.GetType().String() + " with name " + data.GetName() + " already exists")
|
|
}
|
|
err := validate.Struct(data)
|
|
if err != nil {
|
|
return nil, 422, err
|
|
}
|
|
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), a.GetType().String())
|
|
if err != nil {
|
|
a.GetLogger().Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
|
return nil, code, err
|
|
}
|
|
return a.LoadOne(id)
|
|
}
|
|
|
|
// GenericLoadOne loads one object from the database (generic)
|
|
func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
|
|
res, code, err := a.LoadOne(id)
|
|
if !res.CanDelete() {
|
|
return nil, 403, errors.New("you are not allowed to delete this collaborative area")
|
|
}
|
|
if err != nil {
|
|
a.GetLogger().Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
|
return nil, code, err
|
|
}
|
|
if !res.VerifyAuth(a.GetRequest()) {
|
|
return nil, 403, errors.New("you are not allowed to access this collaborative area")
|
|
}
|
|
_, code, err = mongo.MONGOService.DeleteOne(id, a.GetType().String())
|
|
if err != nil {
|
|
a.GetLogger().Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
|
|
return nil, code, err
|
|
}
|
|
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) {
|
|
r, c, err := a.LoadOne(id)
|
|
if err != nil {
|
|
return nil, c, err
|
|
}
|
|
ok, newSet := r.CanUpdate(set)
|
|
if !ok {
|
|
return nil, 403, errors.New("you are not allowed to delete this collaborative area")
|
|
}
|
|
set = newSet
|
|
r.UpToDate(a.GetUser(), false)
|
|
if !r.VerifyAuth(a.GetRequest()) {
|
|
return nil, 403, errors.New("you are not allowed to access this collaborative area")
|
|
}
|
|
change := set.Serialize(set) // get the changes
|
|
loaded := r.Serialize(r) // get the loaded object
|
|
|
|
for k, v := range change { // apply the changes, with a flatten method
|
|
loaded[k] = v
|
|
}
|
|
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())
|
|
return nil, code, err
|
|
}
|
|
return a.LoadOne(id)
|
|
}
|
|
|
|
func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, error), a Accessor) (DBObject, int, error) {
|
|
var data T
|
|
res_mongo, code, err := mongo.MONGOService.LoadOne(id, a.GetType().String())
|
|
if err != nil {
|
|
a.GetLogger().Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
|
return nil, code, err
|
|
}
|
|
res_mongo.Decode(&data)
|
|
if !data.VerifyAuth(a.GetRequest()) {
|
|
return nil, 403, errors.New("you are not allowed to access this collaborative area")
|
|
}
|
|
return f(data)
|
|
}
|
|
|
|
func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, onlyDraft bool, f func(DBObject) ShallowDBObject, a Accessor) ([]ShallowDBObject, int, error) {
|
|
objs := []ShallowDBObject{}
|
|
var results []T
|
|
if err != nil {
|
|
a.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
|
return nil, code, err
|
|
}
|
|
if err = res.All(mongo.MngoCtx, &results); err != nil {
|
|
return nil, 404, err
|
|
}
|
|
for _, r := range results {
|
|
if !r.VerifyAuth(a.GetRequest()) || f(r) == nil || (onlyDraft && !r.IsDrafted()) || (!onlyDraft && r.IsDrafted()) {
|
|
continue
|
|
}
|
|
objs = append(objs, f(r))
|
|
}
|
|
return objs, 200, nil
|
|
}
|
|
|
|
func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, onlyDraft bool, wfa Accessor) ([]ShallowDBObject, int, error) {
|
|
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType().String())
|
|
fmt.Println("res_mongo", res_mongo)
|
|
return genericLoadAll[T](res_mongo, code, err, onlyDraft, f, wfa)
|
|
}
|
|
|
|
func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilters *dbs.Filters,
|
|
f func(DBObject) ShallowDBObject, onlyDraft bool, wfa Accessor) ([]ShallowDBObject, int, error) {
|
|
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
|
filters = defaultFilters
|
|
}
|
|
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType().String())
|
|
return genericLoadAll[T](res_mongo, code, err, onlyDraft, f, wfa)
|
|
}
|
|
|
|
// GenericLoadOne loads one object from the database (generic)
|
|
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
|
|
func GenericRawUpdateOne(set DBObject, id string, a Accessor) (DBObject, int, error) {
|
|
id, code, err := mongo.MONGOService.UpdateOne(set, id, a.GetType().String())
|
|
if err != nil {
|
|
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
|
return nil, code, err
|
|
}
|
|
return a.LoadOne(id)
|
|
}
|