1、变更项目结构
2、新增说明文档
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"lingye-gin/src/config"
|
||||
"lingye-gin/src/modules/system/entity"
|
||||
"lingye-gin/src/modules/system/service/query"
|
||||
"lingye-gin/src/util"
|
||||
)
|
||||
|
||||
type UserDAO struct{}
|
||||
|
||||
// 新增
|
||||
func (dao UserDAO) Insert(resource *entity.User) {
|
||||
config.SqlExcutor.Create(resource)
|
||||
}
|
||||
|
||||
// ID查询
|
||||
func (dao UserDAO) SelectOne(id uint) entity.User {
|
||||
var user entity.User
|
||||
config.SqlExcutor.First(&user, id)
|
||||
return user
|
||||
}
|
||||
|
||||
// 查询所有记录
|
||||
func (UserDAO) SelectAll() []entity.User {
|
||||
var users []entity.User
|
||||
config.SqlExcutor.Find(&users)
|
||||
return users
|
||||
}
|
||||
|
||||
// 条件查询
|
||||
func (UserDAO) SelectList(resource entity.User) []entity.User {
|
||||
var users []entity.User
|
||||
config.SqlExcutor.Where("username LIKE ?", "%"+resource.UserName+"%").Find(&users)
|
||||
return users
|
||||
}
|
||||
|
||||
// 条件分页查询
|
||||
func (UserDAO) SelectPage(condition query.UserQuery) ([]entity.User, int) {
|
||||
var users []entity.User
|
||||
var total int
|
||||
|
||||
page, pageSize := util.FixPage(condition.Page, condition.PageSize)
|
||||
// 分页条件
|
||||
pageConfig := config.SqlExcutor.Where("username LIKE ?", "%"+condition.Username+"%")
|
||||
// 分页参数
|
||||
pageConfig = pageConfig.Limit((page - 1) * pageSize).Offset(pageSize)
|
||||
pageConfig = pageConfig.Find(&users)
|
||||
// 统计数量
|
||||
pageConfig.Count(&total)
|
||||
return users, total
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"lingye-gin/src/base"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
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`
|
||||
func (User) TableName() string {
|
||||
return "sys_user"
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"lingye-gin/src/modules/system/service"
|
||||
"lingye-gin/src/modules/system/service/dto"
|
||||
"lingye-gin/src/modules/system/service/query"
|
||||
"lingye-gin/src/util"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 统一Service对象
|
||||
var userService = &service.UserService{}
|
||||
|
||||
func DescribeUsers(c *gin.Context) {
|
||||
var userQuery query.UserQuery
|
||||
_ = c.BindJSON(&userQuery)
|
||||
|
||||
users, total := 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")
|
||||
user := userService.DescribeUserById(util.StringToUInt(id))
|
||||
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)
|
||||
userService.Save(&userDTO)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusOK,
|
||||
"message": "success",
|
||||
})
|
||||
}
|
||||
|
||||
func ModifyUserById(c *gin.Context) {
|
||||
id := c.Params.ByName("id")
|
||||
localUser := userService.DescribeUserById(util.StringToUInt(id))
|
||||
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)
|
||||
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 := util.StringToUInt(id)
|
||||
localUser := userService.DescribeUserById(uid)
|
||||
if localUser.ID == 0 {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"code": http.StatusNotFound,
|
||||
"message": "user not found",
|
||||
})
|
||||
return
|
||||
} else {
|
||||
userService.RemoveById(uid)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusOK,
|
||||
"message": "success",
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package dto
|
||||
|
||||
import "lingye-gin/src/base"
|
||||
|
||||
type UserDTO struct {
|
||||
base.LingYeDTO
|
||||
UserName string `json:"username" form:"username"`
|
||||
Email string `json:"email" form:"email"`
|
||||
Password string `json:"password" form:"password"`
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package query
|
||||
|
||||
import "lingye-gin/src/base"
|
||||
|
||||
type UserQuery struct {
|
||||
base.SimplePage
|
||||
Username string `json:"username"`
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"lingye-gin/src/modules/system/dao"
|
||||
"lingye-gin/src/modules/system/entity"
|
||||
"lingye-gin/src/modules/system/service/dto"
|
||||
"lingye-gin/src/modules/system/service/query"
|
||||
"lingye-gin/src/util"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
userDao dao.UserDAO
|
||||
}
|
||||
|
||||
func (service UserService) Save(resource *dto.UserDTO) {
|
||||
if resource.ID != 0 {
|
||||
panic("id is not zero")
|
||||
}
|
||||
record := &entity.User{}
|
||||
util.StructCopy(resource, record)
|
||||
service.userDao.Insert(record)
|
||||
}
|
||||
|
||||
func (service UserService) RemoveById(id uint) {
|
||||
if id == 0 {
|
||||
panic("id is zero")
|
||||
}
|
||||
service.userDao.DeleteById(id)
|
||||
}
|
||||
|
||||
func (service UserService) ModifyById(resource *dto.UserDTO) {
|
||||
if resource.ID == 0 {
|
||||
panic("id is zero")
|
||||
}
|
||||
record := &entity.User{}
|
||||
util.StructCopy(resource, record)
|
||||
service.userDao.UpdateById(record)
|
||||
}
|
||||
|
||||
func (service UserService) DescribeUsers(condition query.UserQuery) ([]entity.User, int) {
|
||||
return service.userDao.SelectPage(condition)
|
||||
}
|
||||
|
||||
func (service UserService) DescribeUserById(id uint) entity.User {
|
||||
return service.userDao.SelectOne(id)
|
||||
}
|
||||
Reference in New Issue
Block a user