Skip to content

Commit

Permalink
fix: user password issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jun 14, 2024
1 parent 5c8c119 commit 27dea09
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
10 changes: 9 additions & 1 deletion controllers/base_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ func (ctr *BaseControllerV2[T]) Post(c *gin.Context) {
HandleErrorBadRequest(c, err)
return
}
m := any(&model).(interfaces.Model)
u := GetUserFromContextV2(c)
m := any(&model).(interfaces.ModelV2)
m.SetId(primitive.NewObjectID())
m.SetCreated(u.Id)
m.SetUpdated(u.Id)
col := ctr.modelSvc.GetCol()
res, err := col.GetCollection().InsertOne(col.GetContext(), m)
if err != nil {
Expand Down Expand Up @@ -81,6 +84,11 @@ func (ctr *BaseControllerV2[T]) PutById(c *gin.Context) {
return
}

u := GetUserFromContextV2(c)
m := any(&model).(interfaces.ModelV2)
m.SetId(primitive.NewObjectID())
m.SetUpdated(u.Id)

if err := ctr.modelSvc.ReplaceById(id, model); err != nil {
HandleErrorInternalServerError(c, err)
return
Expand Down
5 changes: 5 additions & 0 deletions controllers/router_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ func InitRoutes(app *gin.Engine) (err error) {
},
))
RegisterController(groups.AuthGroup, "/users", NewControllerV2[models.UserV2](
Action{
Method: http.MethodPost,
Path: "",
HandlerFunc: PostUser,
},
Action{
Method: http.MethodPost,
Path: "/:id/change-password",
Expand Down
43 changes: 42 additions & 1 deletion controllers/user_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

func PostUser(c *gin.Context) {
var payload struct {
Username string `json:"username"`
Password string `json:"password"`
Role string `json:"role"`
Email string `json:"email"`
}
if err := c.ShouldBindJSON(&payload); err != nil {
HandleErrorBadRequest(c, err)
return
}
u := GetUserFromContextV2(c)
model := models.UserV2{
Username: payload.Username,
Password: utils.EncryptMd5(payload.Password),
Role: payload.Role,
Email: payload.Email,
}
model.SetCreated(u.Id)
model.SetUpdated(u.Id)
id, err := service.NewModelServiceV2[models.UserV2]().InsertOne(model)
if err != nil {
HandleErrorInternalServerError(c, err)
return
}

result, err := service.NewModelServiceV2[models.UserV2]().GetById(id)
if err != nil {
HandleErrorInternalServerError(c, err)
return
}

HandleSuccessWithData(c, result)

}

func PostUserChangePassword(c *gin.Context) {
// get id
id, err := primitive.ObjectIDFromHex(c.Param("id"))
Expand Down Expand Up @@ -48,7 +84,12 @@ func PostUserChangePassword(c *gin.Context) {

func GetUserMe(c *gin.Context) {
u := GetUserFromContextV2(c)
HandleSuccessWithData(c, u)
_u, err := service.NewModelServiceV2[models.UserV2]().GetById(u.Id)
if err != nil {
HandleErrorInternalServerError(c, err)
return
}
HandleSuccessWithData(c, _u)
}

func PutUserById(c *gin.Context) {
Expand Down
7 changes: 7 additions & 0 deletions interfaces/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ type Model interface {
SetId(id primitive.ObjectID)
}

type ModelV2 interface {
GetId() (id primitive.ObjectID)
SetId(id primitive.ObjectID)
SetCreated(by primitive.ObjectID)
SetUpdated(by primitive.ObjectID)
}

type ModelId int

const (
Expand Down

0 comments on commit 27dea09

Please sign in to comment.