mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-21 02:04:09 +00:00
refactor🎨 : 修改文件操作函数
This commit is contained in:
@@ -1,104 +1,52 @@
|
||||
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:"标识"`
|
||||
|
||||
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:"来源"`
|
||||
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:"来源"`
|
||||
}
|
||||
|
||||
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:"标识"` // 标识
|
||||
Type string `json:"type" comment:"类型"`
|
||||
Name string `json:"name" comment:"名称"`
|
||||
Size string `json:"size" comment:"大小"`
|
||||
PId uint `json:"pId" comment:"目录"`
|
||||
Source string `json:"source" comment:"来源"`
|
||||
Url string `json:"url" comment:"地址"`
|
||||
FullUrl string `json:"fullUrl" comment:"全地址"`
|
||||
ID int `uri:"id" comment:"标识"` // 标识
|
||||
Type string `json:"type" comment:"类型"`
|
||||
Name string `json:"name" comment:"名称"`
|
||||
Size string `json:"size" 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},
|
||||
Type: s.Type,
|
||||
Name: s.Name,
|
||||
Size: s.Size,
|
||||
PId: s.PId,
|
||||
Source: s.Source,
|
||||
Url: s.Url,
|
||||
FullUrl: s.FullUrl,
|
||||
Model: common.Model{ID: s.ID},
|
||||
Type: s.Type,
|
||||
Name: s.Name,
|
||||
Size: s.Size,
|
||||
PId: s.PId,
|
||||
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:"-"`
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -116,4 +127,4 @@ func (e *SysFileInfo) RemoveSysFileInfo(d cDto.Control, c common.ActiveRecord, p
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user