-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
84 lines (68 loc) · 1.99 KB
/
transactions.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package base
import (
stdliberrors "errors"
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/errors"
"github.com/google/uuid"
"gorm.io/gorm"
)
func CreateFile(db *gorm.DB, ownerID uuid.UUID, ownerType string, fileInfo models.FileInfo) (*models.File, *errors.Error) {
file := &models.File{
OwnerID: ownerID,
OwnerType: ownerType,
FileName: fileInfo.FileName,
FileType: fileInfo.FileType,
FileSize: fileInfo.FileSize,
FileURL: fileInfo.FileURL,
ObjectKey: fileInfo.ObjectKey,
}
if err := db.Create(file).Error; err != nil {
return nil, &errors.FailedToCreateFileInDB
}
return file, nil
}
func DeleteFile(db *gorm.DB, fileID uuid.UUID) *errors.Error {
if err := db.Delete(&models.File{}, fileID).Error; err != nil {
if stdliberrors.Is(err, gorm.ErrRecordNotFound) {
return &errors.FileNotFound
} else {
return &errors.FailedToDeleteFile
}
}
return nil
}
func UpdateFile(db *gorm.DB, fileID uuid.UUID, fileInfo models.FileInfo) (*models.File, *errors.Error) {
existingFile, err := GetFile(db, fileID)
if err != nil {
return nil, err
}
if err := db.Model(&existingFile).Updates(models.File{
FileName: fileInfo.FileName,
FileType: fileInfo.FileType,
FileSize: fileInfo.FileSize,
FileURL: fileInfo.FileURL,
ObjectKey: fileInfo.ObjectKey,
}).Error; err != nil {
return nil, &errors.FailedToUpdateFile
}
return existingFile, nil
}
func GetFiles(db *gorm.DB, limit, page int) ([]models.File, *errors.Error) {
var files []models.File
offset := (page - 1) * limit
if err := db.Limit(limit).Offset(offset).Find(&files).Error; err != nil {
return nil, &errors.FailedToGetFiles
}
return files, nil
}
func GetFile(db *gorm.DB, fileID uuid.UUID) (*models.File, *errors.Error) {
var file models.File
if err := db.First(&file, fileID).Error; err != nil {
if stdliberrors.Is(err, gorm.ErrRecordNotFound) {
return nil, &errors.FileNotFound
} else {
return nil, &errors.FailedToGetFile
}
}
return &file, nil
}