@@ -9,6 +9,18 @@ import (
9
9
"os"
10
10
)
11
11
12
+ type GetResult struct {
13
+ Media models.Media
14
+ Err error
15
+ }
16
+ type DeleteResult struct {
17
+ Err error
18
+ }
19
+ type AddResult struct {
20
+ MediaId string
21
+ FileUrl string
22
+ Err error
23
+ }
12
24
type User struct {
13
25
ID uint `gorm:"primaryKey"`
14
26
Username string `gorm:"unique"`
@@ -48,38 +60,65 @@ func SetupStorage() error {
48
60
fmt .Println ("Tables created successfully!" )
49
61
return nil
50
62
}
51
-
52
- func AddNewMedia (media models.Media ) (string , string , error ) {
53
-
63
+ func AddNewMedia (media models.Media , ch chan AddResult ) {
64
+ // Perform the database insertion
54
65
result := database .Debug ().Create (& media )
55
66
if result .Error != nil {
56
- fmt .Println ("Could not create entry:" )
57
- return "" , "" , result .Error
67
+ fmt .Println ("Could not create entry:" , result .Error )
68
+ ch <- struct {
69
+ MediaId string
70
+ FileUrl string
71
+ Err error
72
+ }{"" , "" , result .Error } // Send error through the channel
73
+ return
58
74
}
59
- return media .MediaId , media .FileUrl , nil
60
- }
61
75
62
- func GetMediaById (fileId string ) (models.Media , error ) {
76
+ // Send the result (mediaId, fileUrl, and nil error) through the channel
77
+ ch <- struct {
78
+ MediaId string
79
+ FileUrl string
80
+ Err error
81
+ }{media .MediaId , media .FileUrl , nil } // Send mediaId, fileUrl, and nil error
82
+ }
83
+ func GetMediaByIdDb (fileId string , ch chan GetResult ) {
63
84
var media models.Media
64
85
65
86
result := database .Where ("media_id = ?" , fileId ).First (& media )
66
87
if result .Error != nil {
67
88
log .Printf ("Error fetching media by ID: %v" , result .Error )
68
- return models.Media {}, result .Error
89
+ ch <- struct {
90
+ Media models.Media
91
+ Err error
92
+ }{models.Media {}, result .Error } // Send the error through the channel
93
+ return
69
94
}
70
- return media , nil
95
+
96
+ ch <- struct {
97
+ Media models.Media
98
+ Err error
99
+ }{media , nil } // Send the media and nil error through the channel
71
100
}
72
101
73
- func DeleteMediaById (fileId string ) error {
74
- media , err := GetMediaById (fileId )
75
- if err != nil {
76
- fmt .Printf ("Could not Get Media %v" , err )
77
- return err
102
+ func DeleteMediaById (fileId string , deleteCh chan DeleteResult ) {
103
+ ch := make (chan GetResult )
104
+ go GetMediaByIdDb (fileId , ch )
105
+ result := <- ch
106
+
107
+ if result .Err != nil {
108
+ fmt .Printf ("Could not Get Media %v" , result .Err )
109
+ deleteCh <- struct {
110
+ Err error
111
+ }{result .Err }
112
+ return
78
113
}
79
- err = os .Remove (media .FileUrl )
80
- if err != nil {
81
- fmt .Printf ("Could not find Media / Delete it %v" , err )
114
+ result .Err = os .Remove (result .Media .FileUrl )
115
+ if result .Err != nil {
116
+ fmt .Printf ("Could not find Media / Delete it %v" , result .Err )
117
+ return
82
118
}
83
- database .Where ("media_id = ?" , fileId ).First (& media ).Delete (& models.Media {})
84
- return nil
119
+ database .Where ("media_id = ?" , fileId ).First (& result .Media ).Delete (& models.Media {})
120
+
121
+ deleteCh <- struct {
122
+ Err error
123
+ }{nil }
85
124
}
0 commit comments