From f1f4de12bd9f7f4cf67e003c2496a742190fd686 Mon Sep 17 00:00:00 2001 From: TianJun Date: Wed, 28 Apr 2021 18:57:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E8=BD=ACuint=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rest/api/user_rest.go | 14 ++++++-------- src/util/util.go | 6 ++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/rest/api/user_rest.go b/src/rest/api/user_rest.go index dd3b0e4..a1dfbfe 100644 --- a/src/rest/api/user_rest.go +++ b/src/rest/api/user_rest.go @@ -5,8 +5,8 @@ import ( "lingye-gin/src/service" "lingye-gin/src/service/dto" "lingye-gin/src/service/query" + "lingye-gin/src/util" "net/http" - "strconv" ) func DescribeUsers(c *gin.Context) { @@ -23,8 +23,7 @@ func DescribeUsers(c *gin.Context) { func DescribeUserById(c *gin.Context) { id := c.Params.ByName("id") - uid, _ := strconv.Atoi(id) - user := new(service.UserService).DescribeUserById(uint(uid)) + user := new(service.UserService).DescribeUserById(util.StringToUInt(id)) if user.ID == 0 { c.JSON(http.StatusNotFound, gin.H{ "code": http.StatusNotFound, @@ -52,8 +51,7 @@ func CreateUser(c *gin.Context) { func ModifyUserById(c *gin.Context) { id := c.Params.ByName("id") - uid, _ := strconv.Atoi(id) - localUser := new(service.UserService).DescribeUserById(uint(uid)) + localUser := new(service.UserService).DescribeUserById(util.StringToUInt(id)) if localUser.ID == 0 { c.JSON(http.StatusNotFound, gin.H{ "code": http.StatusNotFound, @@ -73,8 +71,8 @@ func ModifyUserById(c *gin.Context) { func DeleteUserById(c *gin.Context) { id := c.Params.ByName("id") - uid, _ := strconv.Atoi(id) - localUser := new(service.UserService).DescribeUserById(uint(uid)) + uid := util.StringToUInt(id) + localUser := new(service.UserService).DescribeUserById(uid) if localUser.ID == 0 { c.JSON(http.StatusNotFound, gin.H{ "code": http.StatusNotFound, @@ -82,7 +80,7 @@ func DeleteUserById(c *gin.Context) { }) return } else { - new(service.UserService).RemoveById(uint(uid)) + new(service.UserService).RemoveById(uid) c.JSON(http.StatusOK, gin.H{ "code": http.StatusOK, "message": "success", diff --git a/src/util/util.go b/src/util/util.go index e70f2d2..80a6cc9 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -127,3 +127,9 @@ func StructCopy(source interface{}, target interface{}) { } } } + +// 字符串转uint +func StringToUInt(number string) uint { + result, _ := strconv.Atoi(number) + return uint(result) +}