fix userModel
This commit is contained in:
parent
c055635ace
commit
efa28749de
|
@ -42,10 +42,10 @@ func (s Service) Create(payload CreateOptions) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (payload CreateOptions) newUser() (result User, err error) {
|
func (payload CreateOptions) newUser() (result dbUser, err error) {
|
||||||
timeNow := now()
|
timeNow := now()
|
||||||
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
|
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
|
||||||
return User{
|
return dbUser{
|
||||||
ID: mongodb.NewObjectID(),
|
ID: mongodb.NewObjectID(),
|
||||||
Name: payload.Name,
|
Name: payload.Name,
|
||||||
SearchString: getSearchString(payload.Name, payload.Phone, payload.Email),
|
SearchString: getSearchString(payload.Name, payload.Phone, payload.Email),
|
||||||
|
|
|
@ -17,7 +17,7 @@ type AllQuery struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// All ...
|
// All ...
|
||||||
func (s Service) All(queryParams AllQuery) (r ResponseUserAll) {
|
func (s Service) All(queryParams AllQuery) (r UserAll) {
|
||||||
var (
|
var (
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
@ -56,12 +56,12 @@ func (s Service) All(queryParams AllQuery) (r ResponseUserAll) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getResponseList(ctx context.Context, users []User) []ResponseUser {
|
func getResponseList(ctx context.Context, users []dbUser) []User {
|
||||||
res := make([]ResponseUser, 0)
|
res := make([]User, 0)
|
||||||
|
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
role, _ := s.roleFindByID(ctx, user.RoleID)
|
role, _ := s.roleFindByID(ctx, user.RoleID)
|
||||||
res = append(res, ResponseUser{
|
res = append(res, User{
|
||||||
ID: user.ID.Hex(),
|
ID: user.ID.Hex(),
|
||||||
Name: user.Name,
|
Name: user.Name,
|
||||||
Phone: user.Phone,
|
Phone: user.Phone,
|
||||||
|
|
14
db.go
14
db.go
|
@ -68,7 +68,7 @@ func (s Service) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID)
|
||||||
return total != 0
|
return total != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) userCreate(ctx context.Context, doc User) error {
|
func (s Service) userCreate(ctx context.Context, doc dbUser) error {
|
||||||
var (
|
var (
|
||||||
col = s.getUserCollection()
|
col = s.getUserCollection()
|
||||||
)
|
)
|
||||||
|
@ -103,20 +103,20 @@ func (s Service) userUpdateOneByCondition(ctx context.Context, cond interface{},
|
||||||
return err
|
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 (
|
var (
|
||||||
col = s.getUserCollection()
|
col = s.getUserCollection()
|
||||||
doc User
|
doc dbUser
|
||||||
)
|
)
|
||||||
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
||||||
return doc, err
|
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 (
|
var (
|
||||||
col = s.getUserCollection()
|
col = s.getUserCollection()
|
||||||
)
|
)
|
||||||
docs = make([]User, 0)
|
docs = make([]dbUser, 0)
|
||||||
|
|
||||||
cursor, err := col.Find(ctx, cond, opts...)
|
cursor, err := col.Find(ctx, cond, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -155,10 +155,10 @@ func (s Service) userCountByCondition(ctx context.Context, cond interface{}) int
|
||||||
return total
|
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 (
|
var (
|
||||||
col = s.getRoleCollection()
|
col = s.getRoleCollection()
|
||||||
doc Role
|
doc dbRole
|
||||||
)
|
)
|
||||||
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
||||||
return doc, err
|
return doc, err
|
||||||
|
|
|
@ -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"`
|
||||||
|
}
|
35
model.go
35
model.go
|
@ -2,27 +2,10 @@ package usermngmt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// User ...
|
// User ...
|
||||||
type User struct {
|
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"`
|
ID string `json:"_id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
|
@ -34,16 +17,6 @@ type ResponseUser struct {
|
||||||
UpdatedAt time.Time `json:"updatedAt"`
|
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 {
|
type RoleShort struct {
|
||||||
ID string `json:"_id"`
|
ID string `json:"_id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
@ -51,9 +24,9 @@ type RoleShort struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// ResponseUserAll ...
|
// UserAll ...
|
||||||
ResponseUserAll struct {
|
UserAll struct {
|
||||||
List []ResponseUser `json:"list"`
|
List []User `json:"list"`
|
||||||
Total int64 `json:"total"`
|
Total int64 `json:"total"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue