usermngmt/user/db.go

292 lines
6.7 KiB
Go
Raw Normal View History

2021-11-10 01:44:22 +00:00
package user
import (
"context"
"fmt"
"github.com/Selly-Modules/logger"
2021-11-10 04:01:39 +00:00
"github.com/Selly-Modules/usermngmt/database"
"github.com/Selly-Modules/usermngmt/model"
2021-11-10 01:44:22 +00:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
2021-11-16 04:11:04 +00:00
func isPhoneNumberExisted(ctx context.Context, phone string) bool {
var (
col = database.GetUserCol()
)
// Find
cond := bson.M{
2021-11-24 06:15:40 +00:00
"phone": phone,
"deleted": false,
2021-11-16 04:11:04 +00:00
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - User - CountDocuments", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.isPhoneNumberExisted",
Message: err.Error(),
Data: cond,
2021-11-16 04:11:04 +00:00
})
return true
}
return total != 0
}
func isEmailExisted(ctx context.Context, email string) bool {
var (
col = database.GetUserCol()
)
// Find
cond := bson.M{
2021-11-24 06:15:40 +00:00
"email": email,
"deleted": false,
2021-11-16 04:11:04 +00:00
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - User - CountDocuments", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.isEmailExisted",
Message: err.Error(),
Data: cond,
2021-11-16 04:11:04 +00:00
})
return true
}
return total != 0
}
2021-11-24 06:15:40 +00:00
func isRoleExisted(ctx context.Context, roleID primitive.ObjectID) bool {
2021-11-10 04:01:39 +00:00
var (
col = database.GetRoleCol()
)
2021-11-10 01:44:22 +00:00
// Find
cond := bson.M{
"_id": roleID,
}
2021-11-10 04:01:39 +00:00
total, err := col.CountDocuments(ctx, cond)
2021-11-10 01:44:22 +00:00
if err != nil {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - Role - CountDocuments", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.isRoleExisted",
Message: err.Error(),
Data: cond,
2021-11-10 01:44:22 +00:00
})
}
return total != 0
}
2021-11-10 04:42:23 +00:00
func roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
2021-11-10 01:44:22 +00:00
var (
2021-11-10 02:54:49 +00:00
doc model.DBRole
2021-11-10 04:01:39 +00:00
col = database.GetRoleCol()
2021-11-10 01:44:22 +00:00
)
2021-11-10 04:01:39 +00:00
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
2021-11-10 01:44:22 +00:00
return doc, err
}
2022-03-29 10:26:35 +00:00
func roleFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBRole) {
var (
col = database.GetRoleCol()
)
docs = make([]model.DBRole, 0)
cursor, err := col.Find(ctx, cond, opts...)
if err != nil {
logger.Error("usermngmt - Role - Find", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.roleFindByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"opts": opts,
},
2022-03-29 10:26:35 +00:00
})
return
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &docs); err != nil {
logger.Error("usermngmt - Role - Decode", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.roleFindByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"opts": opts,
},
2022-03-29 10:26:35 +00:00
})
return
}
return
}
2022-03-25 07:16:52 +00:00
func permissionFindByCondition(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-06 02:25:24 +00:00
Source: "usermngmt.user.permissionFindByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"opts": opts,
},
2022-03-25 07:16:52 +00:00
})
return
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &docs); err != nil {
logger.Error("usermngmt - Permission - Decode", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.permissionFindByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"opts": opts,
},
2022-03-25 07:16:52 +00:00
})
return
}
return
}
2021-11-10 09:53:31 +00:00
// permissionCountByCondition ...
func permissionCountByCondition(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-06 02:25:24 +00:00
Source: "usermngmt.user.permissionCountByCondition",
Message: err.Error(),
Data: cond,
2021-11-10 09:53:31 +00:00
})
}
return total
}
2021-11-10 04:42:23 +00:00
func create(ctx context.Context, doc model.DBUser) error {
2021-11-10 04:01:39 +00:00
var (
col = database.GetUserCol()
)
_, err := col.InsertOne(ctx, doc)
2021-11-10 01:44:22 +00:00
if err != nil {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - InsertOne", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.create",
Message: err.Error(),
Data: doc,
2021-11-10 01:44:22 +00:00
})
return fmt.Errorf("error when create user: %s", err.Error())
}
return nil
}
2021-11-10 04:42:23 +00:00
func updateOneByCondition(ctx context.Context, cond interface{}, payload interface{}) error {
2021-11-10 04:01:39 +00:00
var (
col = database.GetUserCol()
)
_, err := col.UpdateOne(ctx, cond, payload)
2021-11-10 01:44:22 +00:00
if err != nil {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - UpdateOne", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.updateOneByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"payload": payload,
},
2021-11-10 07:50:43 +00:00
})
return fmt.Errorf("error when update user: %s", err.Error())
}
return err
}
func updateManyByCondition(ctx context.Context, cond interface{}, payload interface{}) error {
var (
col = database.GetUserCol()
)
_, err := col.UpdateMany(ctx, cond, payload)
if err != nil {
logger.Error("usermngmt - User - UpdateMany", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.updateManyByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"payload": payload,
},
2021-11-10 01:44:22 +00:00
})
return fmt.Errorf("error when update user: %s", err.Error())
}
return err
}
2021-11-10 04:42:23 +00:00
func findByID(ctx context.Context, id primitive.ObjectID) (model.DBUser, error) {
2021-11-10 01:44:22 +00:00
var (
2021-11-10 02:54:49 +00:00
doc model.DBUser
2021-11-10 04:01:39 +00:00
col = database.GetUserCol()
2021-11-10 01:44:22 +00:00
)
2021-11-24 06:15:40 +00:00
err := col.FindOne(ctx, bson.M{"_id": id, "deleted": false}).Decode(&doc)
2021-11-10 01:44:22 +00:00
return doc, err
}
2021-11-10 04:42:23 +00:00
func findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBUser) {
2021-11-10 04:01:39 +00:00
var (
col = database.GetUserCol()
)
2021-11-10 02:54:49 +00:00
docs = make([]model.DBUser, 0)
2021-11-10 01:44:22 +00:00
2021-11-10 04:01:39 +00:00
cursor, err := col.Find(ctx, cond, opts...)
2021-11-10 01:44:22 +00:00
if err != nil {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Find", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.findByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"opts": opts,
},
2021-11-10 01:44:22 +00:00
})
return
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &docs); err != nil {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Decode", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.findByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
"opts": opts,
},
2021-11-10 01:44:22 +00:00
})
return
}
return
}
// countByCondition ...
2021-11-10 04:42:23 +00:00
func countByCondition(ctx context.Context, cond interface{}) int64 {
2021-11-10 04:01:39 +00:00
var (
col = database.GetUserCol()
)
total, err := col.CountDocuments(ctx, cond)
2021-11-10 01:44:22 +00:00
if err != nil {
logger.Error("usermngmt - Count", logger.LogData{
2022-10-06 02:25:24 +00:00
Source: "usermngmt.user.countByCondition",
Message: err.Error(),
Data: map[string]interface{}{
"cond": cond,
},
2021-11-10 01:44:22 +00:00
})
}
return total
}
2021-11-10 09:06:33 +00:00
func findOneByCondition(ctx context.Context, cond interface{}) (model.DBUser, error) {
var (
col = database.GetUserCol()
doc model.DBUser
)
err := col.FindOne(ctx, cond).Decode(&doc)
return doc, err
}