明确依赖顺序

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{} type UserDAO struct{}
// 新增 // 新增
func (UserDAO) Insert(resource *entity.User) { func (dao UserDAO) Insert(resource *entity.User) {
config.SqlExcutor.Create(resource) config.SqlExcutor.Create(resource)
} }
// ID查询 // ID查询
func (UserDAO) SelectOne(id uint) entity.User { func (dao UserDAO) SelectOne(id uint) entity.User {
var user entity.User var user entity.User
config.SqlExcutor.First(&user, id) config.SqlExcutor.First(&user, id)
return user return user

View File

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

View File

@ -8,37 +8,39 @@ import (
"lingye-gin/src/util" "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 { if resource.ID != 0 {
panic("id is not zero") panic("id is not zero")
} }
record := &entity.User{} record := &entity.User{}
util.StructCopy(resource, record) 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 { if id == 0 {
panic("id is zero") 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 { if resource.ID == 0 {
panic("id is zero") panic("id is zero")
} }
record := &entity.User{} record := &entity.User{}
util.StructCopy(resource, record) util.StructCopy(resource, record)
new(dao.UserDAO).UpdateById(record) service.userDao.UpdateById(record)
} }
func (UserService) DescribeUsers(condition query.UserQuery) ([]entity.User, int) { func (service UserService) DescribeUsers(condition query.UserQuery) ([]entity.User, int) {
return new(dao.UserDAO).SelectPage(condition) return service.userDao.SelectPage(condition)
} }
func (UserService) DescribeUserById(id uint) entity.User { func (service UserService) DescribeUserById(id uint) entity.User {
return new(dao.UserDAO).SelectOne(id) return service.userDao.SelectOne(id)
} }