/
updownload_srvc.go
56 lines (43 loc) · 1.55 KB
/
updownload_srvc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package service_file
import (
"context"
)
type TUpDownload interface {
Upload(_ context.Context, file ModUpload) error
FindFile(_ context.Context, id uint) (ModUpload, error)
DeleteFile(ctx context.Context, file ModUpload) (err error)
EditFileName(_ context.Context, file ModUpload) (err error)
GetFileRecordInfoList(_ context.Context, info PageInfo) (list []ModUpload, total int64, err error)
}
func (imp *impl) Upload(_ context.Context, file ModUpload) error {
return imp.gormDB.Create(&file).Error
}
func (imp *impl) FindFile(_ context.Context, id uint) (ModUpload, error) {
var file ModUpload
err := imp.gormDB.Where("id = ?", id).First(&file).Error
return file, err
}
func (imp *impl) DeleteFile(_ context.Context, file ModUpload) (err error) {
err = imp.gormDB.Where("id = ?", file.ID).Unscoped().Delete(&file).Error
return err
}
func (imp *impl) EditFileName(_ context.Context, file ModUpload) (err error) {
var fileFromDb ModUpload
return imp.gormDB.Where("id = ?", file.ID).First(&fileFromDb).Update("name", file.Name).Error
}
func (imp *impl) GetFileRecordInfoList(_ context.Context, info PageInfo) (list []ModUpload, total int64, err error) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
keyword := info.Keyword
db := imp.gormDB.Model(&ModUpload{})
var fileLists []ModUpload
if len(keyword) > 0 {
db = db.Where("name LIKE ?", "%"+keyword+"%")
}
err = db.Count(&total).Error
if err != nil {
return
}
err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&fileLists).Error
return fileLists, total, err
}