Organize backend into product executable and shared Go modules
This commit is contained in:
1 parent
981e84a4b4
commit
f0720ec66b
382 files changed
+1944
-6989
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package mutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.sendico.io/tech/sxchnge/shared/database/record"
|
||||
"git.sendico.io/tech/sxchnge/shared/database/repository"
|
||||
"git.sendico.io/tech/sxchnge/shared/logging"
|
||||
"git.sendico.io/tech/sxchnge/shared/logging/fields"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func SetArchived[T storable.Storable](ctx context.Context, logger mlogger.Logger, newArchived bool, objectRef bson.ObjectID, repo repository.Repository) error {
|
||||
objs, err := GetObjects[T](ctx, logger, repository.IDFilter(objectRef), nil, repo)
|
||||
if err != nil {
|
||||
logger.Warn("Failed to fetch object", zap.Error(err), mzap.ObjRef("object_ref", objectRef))
|
||||
return err
|
||||
}
|
||||
|
||||
if len(objs) == 0 {
|
||||
logger.Warn("No objects found to archive", mzap.ObjRef("object_ref", objectRef))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Archive the first object found
|
||||
obj := objs[0]
|
||||
if archivable, ok := any(obj).(interface{ SetArchived(bool) }); ok {
|
||||
archivable.SetArchived(newArchived)
|
||||
if err := repo.Update(ctx, obj); err != nil {
|
||||
logger.Warn("Failed to update object archived status", zap.Error(err), mzap.ObjRef("object_ref", objectRef))
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package mutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
sharedmodel "git.sendico.io/tech/sxchnge/shared/model"
|
||||
|
||||
"git.sendico.io/tech/sxchnge/shared/database/repository"
|
||||
"git.sendico.io/tech/sxchnge/shared/database/repository/builder"
|
||||
"git.sendico.io/tech/sxchnge/shared/logging"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func GetObjects[T any](ctx context.Context, logger mlogger.Logger, filter builder.Query, cursor *sharedmodel.ViewCursor, repo repository.Repository) ([]T, error) {
|
||||
entities := make([]T, 0)
|
||||
decoder := func(cur *mongo.Cursor) error {
|
||||
var next T
|
||||
if e := cur.Decode(&next); e != nil {
|
||||
logger.Warn("Failed to decode entity", zap.Error(e))
|
||||
return e
|
||||
}
|
||||
entities = append(entities, next)
|
||||
return nil
|
||||
}
|
||||
return entities, repo.FindManyByFilter(ctx, repository.ApplyCursor(filter, cursor), decoder)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package mutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.sendico.io/tech/sxchnge/shared/database/record"
|
||||
"git.sendico.io/tech/sxchnge/shared/database/repository"
|
||||
"git.sendico.io/tech/sxchnge/shared/logging"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func GetObjectByID(ctx context.Context, logger mlogger.Logger, id string, val storable.Storable, repo repository.Repository) error {
|
||||
p, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
logger.Warn("Failed to decode object reference", zap.String("reference", id), zap.String("collection", val.Collection()))
|
||||
return err
|
||||
}
|
||||
return repo.Get(ctx, p, val)
|
||||
}
|
||||
Reference in new issue
Block a user