dao、service、controller分层打通
This commit is contained in:
+27
-19
@@ -3,7 +3,7 @@ package dao
|
|||||||
import (
|
import (
|
||||||
"lingye-gin/src/config"
|
"lingye-gin/src/config"
|
||||||
"lingye-gin/src/entity"
|
"lingye-gin/src/entity"
|
||||||
"lingye-gin/src/service/dto"
|
"lingye-gin/src/service/query"
|
||||||
"lingye-gin/src/util"
|
"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 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 = pageConfig.Limit((page - 1) * pageSize).Offset(pageSize)
|
||||||
pageConfig.Find(&users)
|
pageConfig = pageConfig.Find(&users)
|
||||||
return users
|
// 统计数量
|
||||||
|
pageConfig.Count(&total)
|
||||||
|
return users, total
|
||||||
}
|
}
|
||||||
|
|
||||||
func test() {
|
func (UserDAO) UpdateAll(user *entity.User) {
|
||||||
//
|
config.SqlExcutor.Save(&user)
|
||||||
//user := entity.User{Username: "lingye"}
|
}
|
||||||
//
|
|
||||||
//userDAO := dao.UserDAO{}
|
// 更新特定字段
|
||||||
//userDAO.Insert(&user)
|
func (UserDAO) UpdateById(user *entity.User) {
|
||||||
//userDAO.SelectAll()
|
model := &entity.User{}
|
||||||
//userDAO.SelectOne(1)
|
model.ID = user.ID
|
||||||
//userDAO.SelectList(user)
|
config.SqlExcutor.Model(model).Updates(map[string]interface{}{
|
||||||
//
|
"username": user.UserName,
|
||||||
//dto := dto2.UserDTO{}
|
})
|
||||||
//dto.Username = "测试"
|
}
|
||||||
//userDAO.SelectPage(dto)
|
|
||||||
|
// 删除
|
||||||
|
func (UserDAO) DeleteById(id uint) {
|
||||||
|
user := entity.User{}
|
||||||
|
user.ID = id
|
||||||
|
config.SqlExcutor.Delete(&user)
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-10
@@ -5,16 +5,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
base.BaseDO
|
base.LingYeDO
|
||||||
UserName string `gorm:"column:user_name;type:varchar(255);not null;index:idx_username" json:"username" form:"username"`
|
UserName string `gorm:"column:user_name;type:varchar(255);not null;index:idx_username"`
|
||||||
NickName string `gorm:"column:nick_name;type:varchar(255)" json:"nickname" form:"nickname"`
|
NickName string `gorm:"column:nick_name;type:varchar(255)"`
|
||||||
Email string `gorm:"column:email;type:varchar(255)" json:"email" form:"email"`
|
Email string `gorm:"column:email;type:varchar(255)"`
|
||||||
PhoneNumber string `gorm:"column:phone_number;type:varchar(255)" json:"phoneNumber" form:"phoneNumber"`
|
PhoneNumber string `gorm:"column:phone_number;type:varchar(255)"`
|
||||||
Sex string `gorm:"column:sex;type:varchar(255)" json:"sex" form:"sex"`
|
Sex string `gorm:"column:sex;type:varchar(255)"`
|
||||||
Avatar string `gorm:"column:avatar;type:varchar(255)" json:"avatar" form:"avatar"`
|
Avatar string `gorm:"column:avatar;type:varchar(255)"`
|
||||||
Password string `gorm:"column:password;type:varchar(255);not null" json:"password" form:"password"`
|
Password string `gorm:"column:password;type:varchar(255);not null"`
|
||||||
Status string `gorm:"column:status;type:varchar(255);not null" json:"status" form:"status"`
|
Status string `gorm:"column:status;type:varchar(255);not null"`
|
||||||
Remark string `gorm:"column:remark;type:text(0)" json:"remark" form:"remark"`
|
Remark string `gorm:"column:remark;type:text(0)"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置User的表名为`sys_user`
|
// 设置User的表名为`sys_user`
|
||||||
|
|||||||
@@ -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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -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",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
-2
@@ -2,7 +2,7 @@ package rest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
v3 "lingye-gin/src/rest/api"
|
"lingye-gin/src/rest/api"
|
||||||
v1 "lingye-gin/src/rest/v1"
|
v1 "lingye-gin/src/rest/v1"
|
||||||
v2 "lingye-gin/src/rest/v2"
|
v2 "lingye-gin/src/rest/v2"
|
||||||
"lingye-gin/src/util"
|
"lingye-gin/src/util"
|
||||||
@@ -39,5 +39,14 @@ var Urls = [...]RequestApi{
|
|||||||
// v2
|
// v2
|
||||||
{GroupName: "v2", GroupHandleFunction: util.VerifySign, Mode: "get", RelativePath: "/students", HandleFunction: v2.DescribeStudents},
|
{GroupName: "v2", GroupHandleFunction: util.VerifySign, Mode: "get", RelativePath: "/students", HandleFunction: v2.DescribeStudents},
|
||||||
// api jwt
|
// 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},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package dto
|
|||||||
import "lingye-gin/src/base"
|
import "lingye-gin/src/base"
|
||||||
|
|
||||||
type UserDTO struct {
|
type UserDTO struct {
|
||||||
base.SimplePage
|
base.LingYeDTO
|
||||||
Username string
|
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,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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user