明确依赖顺序

This commit is contained in:
TianJun 2021-04-29 12:36:24 +08:00
parent 96788ec1f0
commit 8ec36e0028
3 changed files with 25 additions and 20 deletions

View File

@ -10,12 +10,12 @@ import (
type UserDAO struct{}
// 新增
func (UserDAO) Insert(resource *entity.User) {
func (dao UserDAO) Insert(resource *entity.User) {
config.SqlExcutor.Create(resource)
}
// ID查询
func (UserDAO) SelectOne(id uint) entity.User {
func (dao UserDAO) SelectOne(id uint) entity.User {
var user entity.User
config.SqlExcutor.First(&user, id)
return user

View File

@ -9,11 +9,14 @@ import (
"net/http"
)
// 统一Service对象
var userService = &service.UserService{}
func DescribeUsers(c *gin.Context) {
var userQuery query.UserQuery
_ = c.BindJSON(&userQuery)
users, total := new(service.UserService).DescribeUsers(userQuery)
users, total := userService.DescribeUsers(userQuery)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"data": users,
@ -23,7 +26,7 @@ func DescribeUsers(c *gin.Context) {
func DescribeUserById(c *gin.Context) {
id := c.Params.ByName("id")
user := new(service.UserService).DescribeUserById(util.StringToUInt(id))
user := userService.DescribeUserById(util.StringToUInt(id))
if user.ID == 0 {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
@ -42,7 +45,7 @@ func CreateUser(c *gin.Context) {
var userDTO dto.UserDTO
// 绑定一个请求主体到一个类型
_ = c.BindJSON(&userDTO)
new(service.UserService).Save(&userDTO)
userService.Save(&userDTO)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"message": "success",
@ -51,7 +54,7 @@ func CreateUser(c *gin.Context) {
func ModifyUserById(c *gin.Context) {
id := c.Params.ByName("id")
localUser := new(service.UserService).DescribeUserById(util.StringToUInt(id))
localUser := userService.DescribeUserById(util.StringToUInt(id))
if localUser.ID == 0 {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
@ -61,7 +64,7 @@ func ModifyUserById(c *gin.Context) {
} else {
var userDTO dto.UserDTO
_ = c.BindJSON(&userDTO)
new(service.UserService).ModifyById(&userDTO)
userService.ModifyById(&userDTO)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"message": "success",
@ -72,7 +75,7 @@ func ModifyUserById(c *gin.Context) {
func DeleteUserById(c *gin.Context) {
id := c.Params.ByName("id")
uid := util.StringToUInt(id)
localUser := new(service.UserService).DescribeUserById(uid)
localUser := userService.DescribeUserById(uid)
if localUser.ID == 0 {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
@ -80,7 +83,7 @@ func DeleteUserById(c *gin.Context) {
})
return
} else {
new(service.UserService).RemoveById(uid)
userService.RemoveById(uid)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"message": "success",

View File

@ -8,37 +8,39 @@ import (
"lingye-gin/src/util"
)
type UserService struct{}
type UserService struct {
userDao dao.UserDAO
}
func (UserService) Save(resource *dto.UserDTO) {
func (service 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)
service.userDao.Insert(record)
}
func (UserService) RemoveById(id uint) {
func (service UserService) RemoveById(id uint) {
if id == 0 {
panic("id is zero")
}
new(dao.UserDAO).DeleteById(id)
service.userDao.DeleteById(id)
}
func (UserService) ModifyById(resource *dto.UserDTO) {
func (service 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)
service.userDao.UpdateById(record)
}
func (UserService) DescribeUsers(condition query.UserQuery) ([]entity.User, int) {
return new(dao.UserDAO).SelectPage(condition)
func (service UserService) DescribeUsers(condition query.UserQuery) ([]entity.User, int) {
return service.userDao.SelectPage(condition)
}
func (UserService) DescribeUserById(id uint) entity.User {
return new(dao.UserDAO).SelectOne(id)
func (service UserService) DescribeUserById(id uint) entity.User {
return service.userDao.SelectOne(id)
}