优化
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"kra/internal/server/httpx"
|
||||
"kra/internal/service"
|
||||
"kra/internal/service/dto"
|
||||
@@ -17,15 +15,23 @@ func NewAuthority(service *service.AuthorityService) *Authority { return &Author
|
||||
func (h *Authority) List(c *gin.Context) {
|
||||
items, err := h.service.Authorities(c.Request.Context())
|
||||
if err != nil {
|
||||
httpx.Fail(c, "获取失败")
|
||||
httpx.Fail(c, "获取失败"+err.Error())
|
||||
return
|
||||
}
|
||||
httpx.Write(c, httpx.CodeSuccess, items, "获取成功")
|
||||
}
|
||||
func (h *Authority) Create(c *gin.Context) {
|
||||
var req dto.AuthorityRequest
|
||||
if c.ShouldBindJSON(&req) != nil || req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "AuthorityId值不能为空")
|
||||
return
|
||||
}
|
||||
if req.AuthorityName == "" {
|
||||
httpx.Fail(c, "AuthorityName值不能为空")
|
||||
return
|
||||
}
|
||||
value, err := h.service.CreateAuthorityRequest(c.Request.Context(), &req)
|
||||
@@ -37,8 +43,20 @@ func (h *Authority) Create(c *gin.Context) {
|
||||
}
|
||||
func (h *Authority) Copy(c *gin.Context) {
|
||||
var req dto.CopyAuthorityRequest
|
||||
if c.ShouldBindJSON(&req) != nil || req.Authority.AuthorityID == 0 || req.OldAuthorityID == 0 {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.OldAuthorityID == 0 {
|
||||
httpx.Fail(c, "OldAuthorityId值不能为空")
|
||||
return
|
||||
}
|
||||
if req.Authority.AuthorityID == 0 {
|
||||
httpx.Fail(c, "AuthorityId值不能为空")
|
||||
return
|
||||
}
|
||||
if req.Authority.AuthorityName == "" {
|
||||
httpx.Fail(c, "AuthorityName值不能为空")
|
||||
return
|
||||
}
|
||||
value, err := h.service.CopyAuthorityRequest(c.Request.Context(), &req)
|
||||
@@ -50,8 +68,16 @@ func (h *Authority) Copy(c *gin.Context) {
|
||||
}
|
||||
func (h *Authority) Update(c *gin.Context) {
|
||||
var req dto.AuthorityRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "AuthorityId值不能为空")
|
||||
return
|
||||
}
|
||||
if req.AuthorityName == "" {
|
||||
httpx.Fail(c, "AuthorityName值不能为空")
|
||||
return
|
||||
}
|
||||
value, err := h.service.UpdateAuthorityRequest(c.Request.Context(), &req)
|
||||
@@ -63,8 +89,12 @@ func (h *Authority) Update(c *gin.Context) {
|
||||
}
|
||||
func (h *Authority) Delete(c *gin.Context) {
|
||||
var req dto.DeleteAuthorityRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "AuthorityId值不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.DeleteAuthority(c.Request.Context(), req.AuthorityID); err != nil {
|
||||
@@ -75,8 +105,12 @@ func (h *Authority) Delete(c *gin.Context) {
|
||||
}
|
||||
func (h *Authority) SetUsers(c *gin.Context) {
|
||||
var req dto.SetRoleUsersRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "角色ID不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.SetAuthorityUsers(c.Request.Context(), req.AuthorityID, req.UserIDs); err != nil {
|
||||
@@ -86,8 +120,12 @@ func (h *Authority) SetUsers(c *gin.Context) {
|
||||
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
|
||||
}
|
||||
func (h *Authority) Users(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("authorityId"), 10, 64)
|
||||
ids, err := h.service.AuthorityUserIDs(c.Request.Context(), uint(id))
|
||||
var req dto.SetRoleUsersRequest
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
ids, err := h.service.AuthorityUserIDs(c.Request.Context(), req.AuthorityID)
|
||||
if err != nil {
|
||||
httpx.Fail(c, "获取失败"+err.Error())
|
||||
return
|
||||
@@ -99,8 +137,12 @@ func (h *Authority) Users(c *gin.Context) {
|
||||
}
|
||||
func (h *Authority) SetDataScope(c *gin.Context) {
|
||||
var req dto.SetDataScopeRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "角色ID不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.SetDataScope(c.Request.Context(), req.AuthorityID, req.DataScope, req.DepartmentIDs); err != nil {
|
||||
@@ -110,8 +152,12 @@ func (h *Authority) SetDataScope(c *gin.Context) {
|
||||
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
|
||||
}
|
||||
func (h *Authority) DataScopeDepartments(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Query("authorityId"), 10, 64)
|
||||
ids, err := h.service.DataScopeDepartmentIDs(c.Request.Context(), uint(id))
|
||||
var req dto.SetDataScopeRequest
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
ids, err := h.service.DataScopeDepartmentIDs(c.Request.Context(), req.AuthorityID)
|
||||
if err != nil {
|
||||
httpx.Fail(c, "获取失败"+err.Error())
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user