Skip to content

Commit

Permalink
feat(file):删除文件使用id作为参数
Browse files Browse the repository at this point in the history
feat(file):删除文件使用id作为参数
  • Loading branch information
li1553770945 committed Dec 8, 2023
2 parents 40ed26f + c91da2d commit 115ba29
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
9 changes: 9 additions & 0 deletions biz/internal/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ func (Repo *Repository) FindFileByFileKey(fileKey string) (*domain.FileEntity, e
}
return &file, nil
}

func (Repo *Repository) FindFileByID(fileID uint) (*domain.FileEntity, error) {
var file domain.FileEntity
err := Repo.DB.Where("id = ?", fileID).Limit(1).Find(&file).Error
if err != nil {
return nil, err
}
return &file, nil
}
func (Repo *Repository) RemoveFile(fileID uint) error {
err := Repo.DB.Delete(&domain.FileEntity{}, fileID).Error
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions biz/internal/repo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type IRepository interface {
FindUser(username string) (*domain.UserEntity, error)
SaveUser(user *domain.UserEntity) error

FindFileByID(fileID uint) (*domain.FileEntity, error)
FindFileByFileKey(fileKey string) (*domain.FileEntity, error)
FindFileBySaveName(saveName string) (*domain.FileEntity, error)
SaveFile(user *domain.FileEntity) error
Expand Down
64 changes: 42 additions & 22 deletions biz/internal/service/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,56 @@ func (s *FileService) DownloadFile(ctx context.Context, c *app.RequestContext) {
}

func (s *FileService) DeleteFile(ctx context.Context, c *app.RequestContext) {
code, msg, data := s.getFileInfo(c.DefaultQuery("file-key", ""))
fileID := c.Param("id")
fileIDInt, err := strconv.ParseUint(fileID, 10, 64)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 2001,
"msg": "参数错误",
})
return
}

if code == 0 {
if data.UserID != s.getUserId(ctx) {
c.JSON(consts.StatusOK, utils.H{
"code": 4003,
"msg": "无权执行操作",
})
return
}
file, err := s.Repo.FindFileByID(uint(fileIDInt))
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 5001,
"msg": err.Error(),
})
}

err := s.Repo.RemoveFile(data.ID)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 5001,
"msg": "删除失败:" + err.Error(),
})
return
}
username := ctx.Value("username")
user, err := s.Repo.FindUser(username.(string))
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 0,
"msg": "删除成功",
"code": 5001,
"msg": err.Error(),
})
return
} else {
}

if uint(file.UserID) != user.ID && user.Role != "admin" {
c.JSON(consts.StatusOK, utils.H{
"code": code,
"msg": msg,
"code": 4003,
"msg": "无权执行操作",
})
return
}

err = s.Repo.RemoveFile(file.ID)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 5001,
"msg": "删除失败:" + err.Error(),
})
return
}
c.JSON(consts.StatusOK, utils.H{
"code": 0,
"msg": "删除成功",
})
return

}

func (s *FileService) getUserId(ctx context.Context) (uid int) {
Expand Down
2 changes: 1 addition & 1 deletion build.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set CGO_ENABLED=0
set GOOS=linux
set GOARCH=amd64
go build -o main
go build -o personal-page-be

0 comments on commit 115ba29

Please sign in to comment.