refactor🎨 : 修改文件操作函数

This commit is contained in:
zhangwenjian
2020-12-21 22:02:40 +08:00
parent 37a2c585e1
commit 746123c888
12 changed files with 128 additions and 169 deletions
+40 -43
View File
@@ -3,7 +3,7 @@ package sys_file
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/service"
common "go-admin/common/models"
"go-admin/tools/app"
"net/http"
"go-admin/app/admin/models"
@@ -20,19 +20,16 @@ type SysFileInfo struct {
func (e *SysFileInfo) GetSysFileInfoList(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
d := new(dto.SysFileInfoSearch)
search := new(dto.SysFileInfoSearch)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
req := d.Generate()
//查询列表
err = req.Bind(c)
err = c.ShouldBind(search)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
app.Error(c, 422, err, "参数验证失败")
return
}
@@ -44,13 +41,13 @@ func (e *SysFileInfo) GetSysFileInfoList(c *gin.Context) {
serviceStudent := service.SysFileInfo{}
serviceStudent.MsgID = msgID
serviceStudent.Orm = db
err = serviceStudent.GetSysFileInfoPage(req, p, &list, &count)
err = serviceStudent.GetSysFileInfoPage(search, p, &list, &count)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
e.PageOK(c, list, int(count), search.PageIndex, search.PageSize, "查询成功")
}
func (e *SysFileInfo) GetSysFileInfo(c *gin.Context) {
@@ -63,12 +60,13 @@ func (e *SysFileInfo) GetSysFileInfo(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
//查看详情
req := control.Generate()
err = req.Bind(c)
err = c.ShouldBindUri(control)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
app.Error(c, 422, err, "参数验证失败")
return
}
var object models.SysFileInfo
//数据权限检查
@@ -77,7 +75,7 @@ func (e *SysFileInfo) GetSysFileInfo(c *gin.Context) {
serviceSysFileInfo := service.SysFileInfo{}
serviceSysFileInfo.MsgID = msgID
serviceSysFileInfo.Orm = db
err = serviceSysFileInfo.GetSysFileInfo(req, p, &object)
err = serviceSysFileInfo.GetSysFileInfo(control, p, &object)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
@@ -96,32 +94,32 @@ func (e *SysFileInfo) InsertSysFileInfo(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
//新增操作
req := control.Generate()
err = req.Bind(c)
err = c.ShouldBindUri(control)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
app.Error(c, 422, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
err = c.ShouldBind(control)
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
app.Error(c, 422, err, "参数验证失败")
return
}
// 设置创建人
object.SetCreateBy(tools.GetUserIdUint(c))
control.CreateBy = tools.GetUserId(c)
serviceSysFileInfo := service.SysFileInfo{}
serviceSysFileInfo.Orm = db
serviceSysFileInfo.MsgID = msgID
err = serviceSysFileInfo.InsertSysFileInfo(object)
err = serviceSysFileInfo.InsertSysFileInfo(control)
if err != nil {
log.Error(err)
e.Error(c, http.StatusInternalServerError, err, "创建失败")
return
}
e.OK(c, object.GetId(), "创建成功")
e.OK(c, control.ID, "创建成功")
}
func (e *SysFileInfo) UpdateSysFileInfo(c *gin.Context) {
@@ -133,20 +131,20 @@ func (e *SysFileInfo) UpdateSysFileInfo(c *gin.Context) {
}
msgID := tools.GenerateMsgIDFromContext(c)
req := control.Generate()
//更新操作
err = req.Bind(c)
err = c.ShouldBindUri(control)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
app.Error(c, 422, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
err = c.ShouldBind(control)
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
app.Error(c, 422, err, "参数验证失败")
return
}
object.SetUpdateBy(tools.GetUserIdUint(c))
// 设置创建人
control.UpdateBy = tools.GetUserId(c)
//数据权限检查
p := actions.GetPermissionFromContext(c)
@@ -154,12 +152,12 @@ func (e *SysFileInfo) UpdateSysFileInfo(c *gin.Context) {
serviceSysFileInfo := service.SysFileInfo{}
serviceSysFileInfo.Orm = db
serviceSysFileInfo.MsgID = msgID
err = serviceSysFileInfo.UpdateSysFileInfo(object, p)
err = serviceSysFileInfo.UpdateSysFileInfo(control, p)
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "更新成功")
e.OK(c, control.ID, "更新成功")
}
func (e *SysFileInfo) DeleteSysFileInfo(c *gin.Context) {
@@ -172,22 +170,21 @@ func (e *SysFileInfo) DeleteSysFileInfo(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
//删除操作
req := control.Generate()
err = req.Bind(c)
err = c.ShouldBindUri(control)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %s", msgID, err)
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
app.Error(c, 422, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
err = c.ShouldBind(control)
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
log.Errorf("msgID[%s] 参数验证错误, error:%s", msgID, err)
app.Error(c, 422, err, "参数验证失败")
return
}
// 设置编辑人
object.SetUpdateBy(tools.GetUserIdUint(c))
control.UpdateBy = tools.GetUserId(c)
// 数据权限检查
p := actions.GetPermissionFromContext(c)
@@ -195,10 +192,10 @@ func (e *SysFileInfo) DeleteSysFileInfo(c *gin.Context) {
serviceSysFileInfo := service.SysFileInfo{}
serviceSysFileInfo.Orm = db
serviceSysFileInfo.MsgID = msgID
err = serviceSysFileInfo.RemoveSysFileInfo(req, object, p)
err = serviceSysFileInfo.RemoveSysFileInfo(control, p)
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "删除成功")
e.OK(c, control.Id, "删除成功")
}
+4 -6
View File
@@ -1,22 +1,20 @@
package models
import (
"gorm.io/gorm"
"go-admin/common/models"
)
type SysFileInfo struct {
gorm.Model
models.ControlBy
models.Model
Type string `json:"type" gorm:"type:varchar(255);comment:类型"` //
Name string `json:"name" gorm:"type:varchar(255);comment:名称"` //
Size string `json:"size" gorm:"type:int(11);comment:大小"` //
PId uint `json:"pId" gorm:"type:int(11);comment:目录"` //
PId int `json:"pId" gorm:"type:int(11);comment:目录"` //
Source string `json:"source" gorm:"type:varchar(255);comment:来源"` //
Url string `json:"url" gorm:"type:varchar(255);comment:地址"` //
FullUrl string `json:"fullUrl" gorm:"type:varchar(255);comment:全地址"` //
models.ControlBy
models.ModelTime
}
func (SysFileInfo) TableName() string {
+9 -61
View File
@@ -1,26 +1,17 @@
package dto
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/tools"
)
type SysFileInfoSearch struct {
dto.Pagination `search:"-"`
ID uint `form:"ID" search:"type:exact;column:id;table:sys_file_info" comment:"标识"`
ID int `form:"ID" search:"type:exact;column:id;table:sys_file_info" comment:"标识"`
Type string `form:"type" search:"type:exact;column:type;table:sys_file_info" comment:"类型"`
Name string `form:"name" search:"type:exact;column:name;table:sys_file_info" comment:"名称"`
PId string `form:"pId" search:"type:exact;column:p_id;table:sys_file_info" comment:"目录"`
Source string `form:"source" search:"type:exact;column:source;table:sys_file_info" comment:"来源"`
}
@@ -28,54 +19,22 @@ func (m *SysFileInfoSearch) GetNeedSearch() interface{} {
return *m
}
func (m *SysFileInfoSearch) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBind(m)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %s", msgID, err.Error())
}
return err
}
func (m *SysFileInfoSearch) Generate() dto.Index {
o := *m
return &o
}
type SysFileInfoControl struct {
ID uint `uri:"ID" comment:"标识"` // 标识
ID int `uri:"id" comment:"标识"` // 标识
Type string `json:"type" comment:"类型"`
Name string `json:"name" comment:"名称"`
Size string `json:"size" comment:"大小"`
PId uint `json:"pId" comment:"目录"`
PId int `json:"pId" comment:"目录"`
Source string `json:"source" comment:"来源"`
Url string `json:"url" comment:"地址"`
FullUrl string `json:"fullUrl" comment:"全地址"`
CreateBy int `json:"-"`
UpdateBy int `json:"-"`
}
func (s *SysFileInfoControl) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error())
return err
}
err = ctx.ShouldBind(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error())
}
return err
}
func (s *SysFileInfoControl) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysFileInfoControl) GenerateM() (common.ActiveRecord, error) {
func (s *SysFileInfoControl) Generate() (*models.SysFileInfo, error) {
return &models.SysFileInfo{
Model: gorm.Model{ID: s.ID},
Model: common.Model{ID: s.ID},
Type: s.Type,
Name: s.Name,
Size: s.Size,
@@ -83,22 +42,11 @@ func (s *SysFileInfoControl) GenerateM() (common.ActiveRecord, error) {
Source: s.Source,
Url: s.Url,
FullUrl: s.FullUrl,
ControlBy: common.ControlBy{CreateBy: s.CreateBy, UpdateBy: s.UpdateBy},
}, nil
}
func (s *SysFileInfoControl) GetId() interface{} {
return s.ID
}
type SysFileInfoById struct {
dto.ObjectById
}
func (s *SysFileInfoById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysFileInfoById) GenerateM() (common.ActiveRecord, error) {
return &models.SysFileInfo{}, nil
UpdateBy int `json:"-"`
}
+27 -16
View File
@@ -3,10 +3,10 @@ package service
import (
"errors"
"go-admin/app/admin/models"
"go-admin/app/admin/service/dto"
"go-admin/common/actions"
cDto "go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/common/service"
"gorm.io/gorm"
)
@@ -16,7 +16,7 @@ type SysFileInfo struct {
}
// GetSysFileInfoPage 获取SysFileInfo列表
func (e *SysFileInfo) GetSysFileInfoPage(c cDto.Index, p *actions.DataPermission, list *[]models.SysFileInfo, count *int64) error {
func (e *SysFileInfo) GetSysFileInfoPage(c *dto.SysFileInfoSearch, p *actions.DataPermission, list *[]models.SysFileInfo, count *int64) error {
var err error
var data models.SysFileInfo
msgID := e.MsgID
@@ -37,7 +37,7 @@ func (e *SysFileInfo) GetSysFileInfoPage(c cDto.Index, p *actions.DataPermission
}
// GetSysFileInfo 获取SysFileInfo对象
func (e *SysFileInfo) GetSysFileInfo(d cDto.Control, p *actions.DataPermission, model *models.SysFileInfo) error {
func (e *SysFileInfo) GetSysFileInfo(d *dto.SysFileInfoById, p *actions.DataPermission, model *models.SysFileInfo) error {
var err error
var data models.SysFileInfo
msgID := e.MsgID
@@ -61,13 +61,19 @@ func (e *SysFileInfo) GetSysFileInfo(d cDto.Control, p *actions.DataPermission,
}
// InsertSysFileInfo 创建SysFileInfo对象
func (e *SysFileInfo) InsertSysFileInfo(model common.ActiveRecord) error {
func (e *SysFileInfo) InsertSysFileInfo(model *dto.SysFileInfoControl) error {
var err error
var data models.SysFileInfo
var data *models.SysFileInfo
msgID := e.MsgID
data, err = model.Generate()
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
err = e.Orm.Model(&data).
Create(model).Error
Create(data).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
@@ -76,20 +82,25 @@ func (e *SysFileInfo) InsertSysFileInfo(model common.ActiveRecord) error {
}
// UpdateSysFileInfo 修改SysFileInfo对象
func (e *SysFileInfo) UpdateSysFileInfo(c common.ActiveRecord, p *actions.DataPermission) error {
func (e *SysFileInfo) UpdateSysFileInfo(c *dto.SysFileInfoControl, p *actions.DataPermission) error {
var err error
var data models.SysFileInfo
var data *models.SysFileInfo
msgID := e.MsgID
db := e.Orm.Model(&data).
Scopes(
actions.Permission(data.TableName(), p),
).Where(c.GetId()).Updates(c)
if db.Error != nil {
data, err = c.Generate()
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.RowsAffected == 0 {
err = e.Orm.Debug().Model(&data).
Scopes(
actions.Permission(data.TableName(), p),
).Where("id = ?", c.ID).Updates(&data).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if err == gorm.ErrRecordNotFound {
return errors.New("无权更新该数据")
}
@@ -97,7 +108,7 @@ func (e *SysFileInfo) UpdateSysFileInfo(c common.ActiveRecord, p *actions.DataPe
}
// RemoveSysFileInfo 删除SysFileInfo
func (e *SysFileInfo) RemoveSysFileInfo(d cDto.Control, c common.ActiveRecord, p *actions.DataPermission) error {
func (e *SysFileInfo) RemoveSysFileInfo(d *dto.SysFileInfoById, p *actions.DataPermission) error {
var err error
var data models.SysFileInfo
msgID := e.MsgID
@@ -105,7 +116,7 @@ func (e *SysFileInfo) RemoveSysFileInfo(d cDto.Control, c common.ActiveRecord, p
db := e.Orm.Model(&data).
Scopes(
actions.Permission(data.TableName(), p),
).Where(d.GetId()).Delete(c)
).Where(d.GetId()).Delete(&data)
if db.Error != nil {
err = db.Error
log.Errorf("MsgID[%s] Delete error: %s", msgID, err)
@@ -29,9 +29,9 @@ func _1599190683670Test(db *gorm.DB, version string) error {
}
list4 := []system.SysConfig{
{Model: gorm.Model{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now(), DeletedAt: gorm.DeletedAt{}}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "主框架页-默认皮肤样式名称", ConfigKey: "sys_index_skinName", ConfigValue: "skin-blue", ConfigType: "Y", Remark: "蓝色 skin-blue、绿色 skin-green、紫色 skin-purple、红色 skin-red、黄色 skin-yellow"},
{Model: gorm.Model{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now(), DeletedAt: gorm.DeletedAt{}}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "用户管理-账号初始密码", ConfigKey: "sys.user.initPassword", ConfigValue: "123456", ConfigType: "Y", Remark: "初始化密码 123456"},
{Model: gorm.Model{ID: 3, CreatedAt: time.Now(), UpdatedAt: time.Now(), DeletedAt: gorm.DeletedAt{}}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "主框架页-侧边栏主题", ConfigKey: "sys_index_sideTheme", ConfigValue: "theme-dark", ConfigType: "Y", Remark: "深色主题theme-dark,浅色主题theme-light"},
{Model: common.Model{ID: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now(), DeletedAt: nil}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "主框架页-默认皮肤样式名称", ConfigKey: "sys_index_skinName", ConfigValue: "skin-blue", ConfigType: "Y", Remark: "蓝色 skin-blue、绿色 skin-green、紫色 skin-purple、红色 skin-red、黄色 skin-yellow"},
{Model: common.Model{ID: 2}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now(), DeletedAt: nil}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "用户管理-账号初始密码", ConfigKey: "sys.user.initPassword", ConfigValue: "123456", ConfigType: "Y", Remark: "初始化密码 123456"},
{Model: common.Model{ID: 3}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now(), DeletedAt: nil}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "主框架页-侧边栏主题", ConfigKey: "sys_index_sideTheme", ConfigValue: "theme-dark", ConfigType: "Y", Remark: "深色主题theme-dark,浅色主题theme-light"},
}
list5 := []models.Post{
+1 -1
View File
@@ -35,7 +35,7 @@ func CreateAction(control dto.Control) gin.HandlerFunc {
app.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
object.SetCreateBy(tools.GetUserIdUint(c))
object.SetCreateBy(tools.GetUserId(c))
err = db.WithContext(c).Create(object).Error
if err != nil {
log.Errorf("MsgID[%s] Create error: %s", msgID, err)
+1 -1
View File
@@ -37,7 +37,7 @@ func DeleteAction(control dto.Control) gin.HandlerFunc {
return
}
object.SetUpdateBy(tools.GetUserIdUint(c))
object.SetUpdateBy(tools.GetUserId(c))
//数据权限检查
p := GetPermissionFromContext(c)
+1 -1
View File
@@ -35,7 +35,7 @@ func UpdateAction(control dto.Control) gin.HandlerFunc {
app.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
object.SetUpdateBy(tools.GetUserIdUint(c))
object.SetUpdateBy(tools.GetUserId(c))
//数据权限检查
p := GetPermissionFromContext(c)
+5 -5
View File
@@ -8,12 +8,12 @@ import (
)
type GeneralDelDto struct {
Id uint `uri:"id" json:"id" validate:"required"`
Ids []uint `json:"ids"`
Id int `uri:"id" json:"id" validate:"required"`
Ids []int `json:"ids"`
}
func (g GeneralDelDto) GetIds() []uint {
ids := make([]uint, 0)
func (g GeneralDelDto) GetIds() []int {
ids := make([]int, 0)
if g.Id != 0 {
ids = append(ids, g.Id)
}
@@ -36,7 +36,7 @@ func (g GeneralDelDto) GetIds() []uint {
}
type GeneralGetDto struct {
Id uint `uri:"id" json:"id" validate:"required"`
Id int `uri:"id" json:"id" validate:"required"`
}
func MakeCondition(q interface{}) func(db *gorm.DB) *gorm.DB {
+17 -4
View File
@@ -1,14 +1,27 @@
package models
import "time"
type ControlBy struct {
CreateBy uint `gorm:"index;comment:'创建者'"`
UpdateBy uint `gorm:"index;comment:'更新者'"`
CreateBy int `gorm:"index;comment:'创建者'"`
UpdateBy int `gorm:"index;comment:'更新者'"`
}
func (e *ControlBy) SetCreateBy(createBy uint) {
func (e *ControlBy) SetCreateBy(createBy int) {
e.CreateBy = createBy
}
func (e *ControlBy) SetUpdateBy(updateBy uint) {
func (e *ControlBy) SetUpdateBy(updateBy int) {
e.UpdateBy = updateBy
}
type Model struct {
ID int `gorm:"primaryKey;autoIncrement;comment:'主键编码'"`
}
type ModelTime struct {
CreatedAt time.Time `gorm:"comment:'创建时间'"`
UpdatedAt time.Time `gorm:"comment:'最后更新时间'"`
DeletedAt *time.Time `gorm:"index;comment:'删除时间'"`
}
+2 -2
View File
@@ -4,8 +4,8 @@ import "gorm.io/gorm/schema"
type ActiveRecord interface {
schema.Tabler
SetCreateBy(createBy uint)
SetUpdateBy(updateBy uint)
SetCreateBy(createBy int)
SetUpdateBy(updateBy int)
Generate() ActiveRecord
GetId() interface{}
}
-8
View File
@@ -17,14 +17,6 @@ func ExtractClaims(c *gin.Context) jwt.MapClaims {
return claims.(jwt.MapClaims)
}
func GetUserIdUint(c *gin.Context) uint {
data := ExtractClaims(c)
if data["identity"] != nil {
return uint((data["identity"]).(float64))
}
fmt.Println(GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserId 缺少identity")
return 0
}
func GetUserId(c *gin.Context) int {
data := ExtractClaims(c)