Skip to content

Commit 120c23f

Browse files
committed
feat: add update logic and router
1 parent e5dfff8 commit 120c23f

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package user
2+
3+
import (
4+
"github.com/coderi421/gframework/gmicro/server/restserver/middlewares"
5+
"github.com/coderi421/gframework/pkg/common/core"
6+
jtime "github.com/coderi421/gframework/pkg/common/time"
7+
"github.com/coderi421/goshop/app/pkg/translator/gin"
8+
"github.com/gin-gonic/gin"
9+
"time"
10+
)
11+
12+
type UpdateUserForm struct {
13+
Name string `form:"name" json:"name" binding:"required,min=3,max=10"`
14+
Gender string `form:"gender" json:"gender" binding:"required,oneof=female male"`
15+
Birthday string `form:"birthday" json:"birthday" binding:"required,datetime=2006-01-02"`
16+
}
17+
18+
func (us *userServer) UpdateUser(ctx *gin.Context) {
19+
updateForm := UpdateUserForm{}
20+
if err := ctx.ShouldBind(&updateForm); err != nil {
21+
translator.GinHandleValidatorError(ctx, err, us.trans)
22+
return
23+
}
24+
userID, _ := ctx.Get(middlewares.KeyUserID)
25+
userIDInt := int32(userID.(float64))
26+
userDTO, err := us.srv.User().Get(ctx, userIDInt)
27+
if err != nil {
28+
core.WriteResponse(ctx, err, nil)
29+
return
30+
}
31+
//将前端传递过来的日期格式转换成int
32+
loc, _ := time.LoadLocation("Local") //local的L必须大写
33+
birthDay, _ := time.ParseInLocation("2006-01-02", updateForm.Birthday, loc)
34+
userDTO.NickName = updateForm.Name
35+
userDTO.Birthday = jtime.Time{Time: birthDay}.Time
36+
userDTO.Gender = updateForm.Gender
37+
err = us.srv.User().Update(ctx, userDTO)
38+
if err != nil {
39+
core.WriteResponse(ctx, err, nil)
40+
return
41+
}
42+
core.WriteResponse(ctx, nil, nil)
43+
}

app/shop/custom/router.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@ package custom
22

33
import (
44
"github.com/coderi421/gframework/gmicro/server/restserver"
5-
"github.com/coderi421/goshop/app/shop/custom/internal/controller"
5+
"github.com/coderi421/goshop/app/shop/custom/config"
6+
"github.com/coderi421/goshop/app/shop/custom/internal/controller/user/v1"
7+
"github.com/coderi421/goshop/app/shop/custom/internal/data/rpc"
8+
"github.com/coderi421/goshop/app/shop/custom/internal/service"
69
)
710

8-
func initRouter(g *restserver.Server) {
11+
func initRouter(g *restserver.Server, conf *config.Config) {
912
v1 := g.Group("/v1")
1013
ugroup := v1.Group("/user")
1114

15+
data, err := rpc.GetDataFactoryOr(conf.Registry)
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
serviceFactory := service.NewService(data, conf.Jwt, conf.Sms)
21+
uController := user.NewUserController(g.Translator(), serviceFactory)
1222
{
13-
ucontroller := controller.NewUserController()
14-
ugroup.GET("list", ucontroller.List)
23+
ugroup.POST("pwd_login", uController.Login)
24+
ugroup.POST("register", uController.Register)
25+
26+
jwtAuth := newJWTAuth(conf.Jwt)
27+
//第三方登录模式 暂不用
28+
//jwtStragy:=jwtAuth.(auth.JWTStrategy)
29+
//jwtStragy.LoginHandler()
30+
ugroup.GET("detail", jwtAuth.AuthFunc(), uController.GetUserDetail)
31+
ugroup.PATCH("update", jwtAuth.AuthFunc(), uController.UpdateUser)
1532
}
1633
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.19
44

55
require (
66
github.com/anaskhan96/go-password-encoder v0.0.0-20201010210601-c765b799fd72
7-
github.com/coderi421/gframework v0.0.5
7+
github.com/coderi421/gframework v0.0.6
88
github.com/gin-gonic/gin v1.9.0
99
github.com/hashicorp/consul/api v1.20.0
1010
github.com/spf13/pflag v1.0.5

0 commit comments

Comments
 (0)