diff --git a/src/dao/user_dao.go b/src/dao/user_dao.go index 41514a5..865f277 100644 --- a/src/dao/user_dao.go +++ b/src/dao/user_dao.go @@ -3,7 +3,7 @@ package dao import ( "lingye-gin/src/config" "lingye-gin/src/entity" - "lingye-gin/src/service/dto" + "lingye-gin/src/service/query" "lingye-gin/src/util" ) @@ -36,29 +36,37 @@ func (UserDAO) SelectList(resource entity.User) []entity.User { } // 条件分页查询 -func (UserDAO) SelectPage(dto dto.UserDTO) []entity.User { +func (UserDAO) SelectPage(condition query.UserQuery) ([]entity.User, int) { var users []entity.User + var total int - page, pageSize := util.FixPage(dto.Page, dto.PageSize) + page, pageSize := util.FixPage(condition.Page, condition.PageSize) // 分页条件 - pageConfig := config.SqlExcutor.Where("username LIKE ?", "%"+dto.Username+"%") + pageConfig := config.SqlExcutor.Where("username LIKE ?", "%"+condition.Username+"%") // 分页参数 pageConfig = pageConfig.Limit((page - 1) * pageSize).Offset(pageSize) - pageConfig.Find(&users) - return users + pageConfig = pageConfig.Find(&users) + // 统计数量 + pageConfig.Count(&total) + return users, total } -func test() { - // - //user := entity.User{Username: "lingye"} - // - //userDAO := dao.UserDAO{} - //userDAO.Insert(&user) - //userDAO.SelectAll() - //userDAO.SelectOne(1) - //userDAO.SelectList(user) - // - //dto := dto2.UserDTO{} - //dto.Username = "测试" - //userDAO.SelectPage(dto) +func (UserDAO) UpdateAll(user *entity.User) { + config.SqlExcutor.Save(&user) +} + +// 更新特定字段 +func (UserDAO) UpdateById(user *entity.User) { + model := &entity.User{} + model.ID = user.ID + config.SqlExcutor.Model(model).Updates(map[string]interface{}{ + "username": user.UserName, + }) +} + +// 删除 +func (UserDAO) DeleteById(id uint) { + user := entity.User{} + user.ID = id + config.SqlExcutor.Delete(&user) } diff --git a/src/entity/user.go b/src/entity/user.go index c1e5f44..a3a0405 100644 --- a/src/entity/user.go +++ b/src/entity/user.go @@ -5,16 +5,16 @@ import ( ) type User struct { - base.BaseDO - UserName string `gorm:"column:user_name;type:varchar(255);not null;index:idx_username" json:"username" form:"username"` - NickName string `gorm:"column:nick_name;type:varchar(255)" json:"nickname" form:"nickname"` - Email string `gorm:"column:email;type:varchar(255)" json:"email" form:"email"` - PhoneNumber string `gorm:"column:phone_number;type:varchar(255)" json:"phoneNumber" form:"phoneNumber"` - Sex string `gorm:"column:sex;type:varchar(255)" json:"sex" form:"sex"` - Avatar string `gorm:"column:avatar;type:varchar(255)" json:"avatar" form:"avatar"` - Password string `gorm:"column:password;type:varchar(255);not null" json:"password" form:"password"` - Status string `gorm:"column:status;type:varchar(255);not null" json:"status" form:"status"` - Remark string `gorm:"column:remark;type:text(0)" json:"remark" form:"remark"` + base.LingYeDO + UserName string `gorm:"column:user_name;type:varchar(255);not null;index:idx_username"` + NickName string `gorm:"column:nick_name;type:varchar(255)"` + Email string `gorm:"column:email;type:varchar(255)"` + PhoneNumber string `gorm:"column:phone_number;type:varchar(255)"` + Sex string `gorm:"column:sex;type:varchar(255)"` + Avatar string `gorm:"column:avatar;type:varchar(255)"` + Password string `gorm:"column:password;type:varchar(255);not null"` + Status string `gorm:"column:status;type:varchar(255);not null"` + Remark string `gorm:"column:remark;type:text(0)"` } // 设置User的表名为`sys_user` diff --git a/src/rest/api/student_rest.go b/src/rest/api/student_rest.go deleted file mode 100644 index 6917f6e..0000000 --- a/src/rest/api/student_rest.go +++ /dev/null @@ -1,20 +0,0 @@ -package api - -import ( - "github.com/gin-gonic/gin" -) - -type Student struct { - ID uint `json:"id"` - Username string `json:"username"` - Age uint `json:"age"` -} - -func DescribeStudents(c *gin.Context) { - var students []Student - c.JSON(200, gin.H{ - "data": students, - "page": 1, - "pageSize": 15, - }) -} diff --git a/src/rest/api/user_rest.go b/src/rest/api/user_rest.go new file mode 100644 index 0000000..dd3b0e4 --- /dev/null +++ b/src/rest/api/user_rest.go @@ -0,0 +1,91 @@ +package api + +import ( + "github.com/gin-gonic/gin" + "lingye-gin/src/service" + "lingye-gin/src/service/dto" + "lingye-gin/src/service/query" + "net/http" + "strconv" +) + +func DescribeUsers(c *gin.Context) { + var userQuery query.UserQuery + _ = c.BindJSON(&userQuery) + + users, total := new(service.UserService).DescribeUsers(userQuery) + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "data": users, + "total": total, + }) +} + +func DescribeUserById(c *gin.Context) { + id := c.Params.ByName("id") + uid, _ := strconv.Atoi(id) + user := new(service.UserService).DescribeUserById(uint(uid)) + if user.ID == 0 { + c.JSON(http.StatusNotFound, gin.H{ + "code": http.StatusNotFound, + "message": "user not found", + }) + return + } + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "message": "success", + "data": user, + }) +} + +func CreateUser(c *gin.Context) { + var userDTO dto.UserDTO + // 绑定一个请求主体到一个类型 + _ = c.BindJSON(&userDTO) + new(service.UserService).Save(&userDTO) + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "message": "success", + }) +} + +func ModifyUserById(c *gin.Context) { + id := c.Params.ByName("id") + uid, _ := strconv.Atoi(id) + localUser := new(service.UserService).DescribeUserById(uint(uid)) + if localUser.ID == 0 { + c.JSON(http.StatusNotFound, gin.H{ + "code": http.StatusNotFound, + "message": "user not found", + }) + return + } else { + var userDTO dto.UserDTO + _ = c.BindJSON(&userDTO) + new(service.UserService).ModifyById(&userDTO) + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "message": "success", + }) + } +} + +func DeleteUserById(c *gin.Context) { + id := c.Params.ByName("id") + uid, _ := strconv.Atoi(id) + localUser := new(service.UserService).DescribeUserById(uint(uid)) + if localUser.ID == 0 { + c.JSON(http.StatusNotFound, gin.H{ + "code": http.StatusNotFound, + "message": "user not found", + }) + return + } else { + new(service.UserService).RemoveById(uint(uid)) + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "message": "success", + }) + } +} diff --git a/src/rest/urls.go b/src/rest/urls.go index 00f6f9d..85199ec 100644 --- a/src/rest/urls.go +++ b/src/rest/urls.go @@ -2,7 +2,7 @@ package rest import ( "github.com/gin-gonic/gin" - v3 "lingye-gin/src/rest/api" + "lingye-gin/src/rest/api" v1 "lingye-gin/src/rest/v1" v2 "lingye-gin/src/rest/v2" "lingye-gin/src/util" @@ -39,5 +39,14 @@ var Urls = [...]RequestApi{ // v2 {GroupName: "v2", GroupHandleFunction: util.VerifySign, Mode: "get", RelativePath: "/students", HandleFunction: v2.DescribeStudents}, // api jwt - {GroupName: "api", Mode: "get", RelativePath: "/students", HandleFunction: v3.DescribeStudents}, + // 获取所有用户 + {GroupName: "api", Mode: "get", RelativePath: "/users", HandleFunction: api.DescribeUsers}, + // 根据ID获取用户 + {GroupName: "api", Mode: "get", RelativePath: "/users/:id", HandleFunction: api.DescribeUserById}, + // 保存用户 + {GroupName: "api", Mode: "post", RelativePath: "/users", HandleFunction: api.CreateUser}, + // 根据ID更新用户 + {GroupName: "api", Mode: "put", RelativePath: "/users/:id", HandleFunction: api.ModifyUserById}, + // 根据ID删除用户 + {GroupName: "api", Mode: "delete", RelativePath: "/users/:id", HandleFunction: api.DeleteUserById}, } diff --git a/src/service/dto/user_dto.go b/src/service/dto/user_dto.go index c121cad..e36bdac 100644 --- a/src/service/dto/user_dto.go +++ b/src/service/dto/user_dto.go @@ -3,6 +3,8 @@ package dto import "lingye-gin/src/base" type UserDTO struct { - base.SimplePage - Username string + base.LingYeDTO + UserName string `json:"username" form:"username"` + Email string `json:"email" form:"email"` + Password string `json:"password" form:"password"` } diff --git a/src/service/query/user_query.go b/src/service/query/user_query.go new file mode 100644 index 0000000..3603c20 --- /dev/null +++ b/src/service/query/user_query.go @@ -0,0 +1,8 @@ +package query + +import "lingye-gin/src/base" + +type UserQuery struct { + base.SimplePage + Username string `json:"username"` +} diff --git a/src/service/user_service.go b/src/service/user_service.go new file mode 100644 index 0000000..b60af9e --- /dev/null +++ b/src/service/user_service.go @@ -0,0 +1,44 @@ +package service + +import ( + "lingye-gin/src/dao" + "lingye-gin/src/entity" + "lingye-gin/src/service/dto" + "lingye-gin/src/service/query" + "lingye-gin/src/util" +) + +type UserService struct{} + +func (UserService) Save(resource *dto.UserDTO) { + if resource.ID != 0 { + panic("id is not zero") + } + record := &entity.User{} + util.StructCopy(resource, record) + new(dao.UserDAO).Insert(record) +} + +func (UserService) RemoveById(id uint) { + if id == 0 { + panic("id is zero") + } + new(dao.UserDAO).DeleteById(id) +} + +func (UserService) ModifyById(resource *dto.UserDTO) { + if resource.ID == 0 { + panic("id is zero") + } + record := &entity.User{} + util.StructCopy(resource, record) + new(dao.UserDAO).UpdateById(record) +} + +func (UserService) DescribeUsers(condition query.UserQuery) ([]entity.User, int) { + return new(dao.UserDAO).SelectPage(condition) +} + +func (UserService) DescribeUserById(id uint) entity.User { + return new(dao.UserDAO).SelectOne(id) +}