usermngmt/permission/db.go

154 lines
3.6 KiB
Go
Raw Permalink Normal View History

2021-11-10 09:06:33 +00:00
package permission
import (
"context"
"fmt"
"github.com/Selly-Modules/logger"
"github.com/Selly-Modules/usermngmt/database"
"github.com/Selly-Modules/usermngmt/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
func findByID(ctx context.Context, id primitive.ObjectID) (model.DBPermission, error) {
var (
doc model.DBPermission
col = database.GetPermissionCol()
)
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
return doc, err
}
func create(ctx context.Context, doc model.DBPermission) error {
var (
col = database.GetPermissionCol()
)
_, err := col.InsertOne(ctx, doc)
if err != nil {
logger.Error("usermngmt - Permission - InsertOne", logger.LogData{
2022-10-05 10:59:08 +00:00
Source: "usermngmt.create",
Message: err.Error(),
Data: map[string]interface{}{
"doc": doc,
},
2021-11-10 09:06:33 +00:00
})
return fmt.Errorf("error when create permission: %s", err.Error())
}
return nil
}
func updateOneByCondition(ctx context.Context, cond interface{}, payload interface{}) error {
var (
col = database.GetPermissionCol()
)
_, err := col.UpdateOne(ctx, cond, payload)
if err != nil {
logger.Error("usermngmt - Permission - UpdateOne", logger.LogData{
2022-10-05 10:59:08 +00:00
Source: "usermngmt.updateOneByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"payload": payload,
},
2021-11-10 09:06:33 +00:00
})
return fmt.Errorf("error when update permission: %s", err.Error())
}
return err
}
2021-11-16 08:34:56 +00:00
func deleteOneByCondition(ctx context.Context, cond interface{}) error {
var (
col = database.GetPermissionCol()
)
_, err := col.DeleteOne(ctx, cond)
if err != nil {
logger.Error("usermngmt - Permission - DeleteOne", logger.LogData{
2022-10-05 10:59:08 +00:00
Source: "usermngmt.deleteOneByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
},
2021-11-16 08:34:56 +00:00
})
return fmt.Errorf("error when delete permission: %s", err.Error())
}
return err
}
2021-11-10 09:06:33 +00:00
func findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBPermission) {
var (
col = database.GetPermissionCol()
)
docs = make([]model.DBPermission, 0)
cursor, err := col.Find(ctx, cond, opts...)
if err != nil {
logger.Error("usermngmt - Permission - Find", logger.LogData{
2022-10-05 10:59:08 +00:00
Source: "usermngmt.findByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"opts": opts,
},
2021-11-10 09:06:33 +00:00
})
return
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &docs); err != nil {
logger.Error("usermngmt - Permission - Decode", logger.LogData{
2022-10-05 10:59:08 +00:00
Source: "usermngmt.findByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"opts": opts,
},
2021-11-10 09:06:33 +00:00
})
return
}
return
}
// countByCondition ...
func countByCondition(ctx context.Context, cond interface{}) int64 {
var (
col = database.GetPermissionCol()
)
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - Permission - CountDocuments", logger.LogData{
2022-10-05 10:59:08 +00:00
Source: "usermngmt.countByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
},
2021-11-10 09:06:33 +00:00
})
}
return total
}
2021-11-16 04:11:04 +00:00
func isPermissionIDExisted(ctx context.Context, permissionID primitive.ObjectID) bool {
var (
2021-11-16 09:05:37 +00:00
col = database.GetPermissionCol()
2021-11-16 04:11:04 +00:00
)
// Find
cond := bson.M{
"_id": permissionID,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - Permission - CountDocuments", logger.LogData{
2022-10-05 10:59:08 +00:00
Source: "usermngmt.isPermissionIDExisted",
Message: err.Error(),
Data: map[string]interface{}{
"condition": cond,
},
2021-11-16 04:11:04 +00:00
})
return false
}
return total != 0
}