fix userModel

This commit is contained in:
Hoang 2021-11-09 14:16:34 +07:00
parent c055635ace
commit efa28749de
5 changed files with 50 additions and 45 deletions

View File

@ -42,10 +42,10 @@ func (s Service) Create(payload CreateOptions) error {
return nil
}
func (payload CreateOptions) newUser() (result User, err error) {
func (payload CreateOptions) newUser() (result dbUser, err error) {
timeNow := now()
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
return User{
return dbUser{
ID: mongodb.NewObjectID(),
Name: payload.Name,
SearchString: getSearchString(payload.Name, payload.Phone, payload.Email),

View File

@ -17,7 +17,7 @@ type AllQuery struct {
}
// All ...
func (s Service) All(queryParams AllQuery) (r ResponseUserAll) {
func (s Service) All(queryParams AllQuery) (r UserAll) {
var (
ctx = context.Background()
wg sync.WaitGroup
@ -56,12 +56,12 @@ func (s Service) All(queryParams AllQuery) (r ResponseUserAll) {
return
}
func getResponseList(ctx context.Context, users []User) []ResponseUser {
res := make([]ResponseUser, 0)
func getResponseList(ctx context.Context, users []dbUser) []User {
res := make([]User, 0)
for _, user := range users {
role, _ := s.roleFindByID(ctx, user.RoleID)
res = append(res, ResponseUser{
res = append(res, User{
ID: user.ID.Hex(),
Name: user.Name,
Phone: user.Phone,

16
db.go
View File

@ -68,7 +68,7 @@ func (s Service) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID)
return total != 0
}
func (s Service) userCreate(ctx context.Context, doc User) error {
func (s Service) userCreate(ctx context.Context, doc dbUser) error {
var (
col = s.getUserCollection()
)
@ -103,20 +103,20 @@ func (s Service) userUpdateOneByCondition(ctx context.Context, cond interface{},
return err
}
func (s Service) userFindByID(ctx context.Context, id primitive.ObjectID) (User, error) {
func (s Service) userFindByID(ctx context.Context, id primitive.ObjectID) (dbUser, error) {
var (
col = s.getUserCollection()
doc User
doc dbUser
)
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
return doc, err
}
func (s Service) userFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []User) {
func (s Service) userFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []dbUser) {
var (
col = s.getUserCollection()
)
docs = make([]User, 0)
docs = make([]dbUser, 0)
cursor, err := col.Find(ctx, cond, opts...)
if err != nil {
@ -155,11 +155,11 @@ func (s Service) userCountByCondition(ctx context.Context, cond interface{}) int
return total
}
func (s Service) roleFindByID(ctx context.Context, id primitive.ObjectID) (Role, error) {
func (s Service) roleFindByID(ctx context.Context, id primitive.ObjectID) (dbRole, error) {
var (
col = s.getRoleCollection()
doc Role
doc dbRole
)
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
return doc, err
}
}

32
db_model.go Normal file
View File

@ -0,0 +1,32 @@
package usermngmt
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// dbUser ...
type dbUser struct {
ID primitive.ObjectID `bson:"_id"`
Name string `bson:"name"`
SearchString string `bson:"searchString"`
Phone string `bson:"phone"` // unique
Email string `bson:"email"` // unique
HashedPassword string `bson:"hashedPassword"`
Status string `bson:"status"`
RoleID primitive.ObjectID `bson:"roleId"`
Other string `bson:"other"`
CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
}
// dbRole ...
type dbRole struct {
ID primitive.ObjectID `bson:"_id" json:"_id"`
Name string `bson:"name" json:"name"`
Code string `bson:"code" json:"code"`
IsAdmin bool `bson:"isAdmin" json:"isAdmin"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"`
}

View File

@ -2,27 +2,10 @@ package usermngmt
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// User ...
type User struct {
ID primitive.ObjectID `bson:"_id" json:"_id"`
Name string `bson:"name" json:"name"`
SearchString string `bson:"searchString" json:"-"`
Phone string `bson:"phone" json:"phone"` // unique
Email string `bson:"email" json:"email"` // unique
HashedPassword string `bson:"hashedPassword" json:"-"`
Status string `bson:"status" json:"status"`
RoleID primitive.ObjectID `bson:"roleId" json:"roleId"`
Other string `bson:"other" json:"other"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"`
}
// ResponseUser ...
type ResponseUser struct {
ID string `json:"_id"`
Name string `json:"name"`
Phone string `json:"phone"`
@ -34,16 +17,6 @@ type ResponseUser struct {
UpdatedAt time.Time `json:"updatedAt"`
}
// Role ...
type Role struct {
ID primitive.ObjectID `bson:"_id" json:"_id"`
Name string `bson:"name" json:"name"`
Code string `bson:"code" json:"code"`
IsAdmin bool `bson:"isAdmin" json:"isAdmin"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"`
}
type RoleShort struct {
ID string `json:"_id"`
Name string `json:"name"`
@ -51,9 +24,9 @@ type RoleShort struct {
}
type (
// ResponseUserAll ...
ResponseUserAll struct {
List []ResponseUser `json:"list"`
Total int64 `json:"total"`
// UserAll ...
UserAll struct {
List []User `json:"list"`
Total int64 `json:"total"`
}
)