usermngmt/user/db.go

152 lines
3.2 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"
)
func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool {
2021-11-10 04:01:39 +00:00
var (
col = database.GetUserCol()
)
2021-11-10 01:44:22 +00:00
// Find
cond := bson.M{
"$or": []bson.M{
{
"phone": phone,
},
{
"email": email,
},
},
}
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 {
logger.Error("usermngmt - countUserByCondition", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return true
}
return total != 0
}
func (h Handle) isRoleIDExisted(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 {
logger.Error("usermngmt - countRoleByCondition", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return false
}
return total != 0
}
2021-11-10 02:54:49 +00:00
func (h Handle) 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
}
2021-11-10 02:54:49 +00:00
func (h Handle) 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 {
logger.Error("usermngmt - Create", logger.LogData{
"doc": doc,
"err": err.Error(),
})
return fmt.Errorf("error when create user: %s", err.Error())
}
return nil
}
func (h Handle) 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 {
logger.Error("usermngmt - Update", logger.LogData{
"cond": cond,
"payload": payload,
"err": err.Error(),
})
return fmt.Errorf("error when update user: %s", err.Error())
}
return err
}
2021-11-10 02:54:49 +00:00
func (h Handle) 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-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
}
2021-11-10 02:54:49 +00:00
func (h Handle) 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 {
logger.Error("usermngmt - All", logger.LogData{
"cond": cond,
"opts": opts,
"err": err.Error(),
})
return
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &docs); err != nil {
logger.Error("usermngmt - All - decode", logger.LogData{
"cond": cond,
"opts": opts,
"err": err.Error(),
})
return
}
return
}
// countByCondition ...
func (h Handle) 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{
"err": err.Error(),
"cond": cond,
})
}
return total
}