Skip to content

Commit 3aaece2

Browse files
committed
fix: add concurrency for database
1 parent 0f93f6e commit 3aaece2

File tree

4 files changed

+75
-28
lines changed

4 files changed

+75
-28
lines changed

src/service/DeleteMediaById.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ func DeleteMediaById(w http.ResponseWriter, r *http.Request) {
2323
}
2424

2525
for i := 0; i < len(ids); i++ {
26-
err := storage.DeleteMediaById(ids[i])
27-
if err != nil {
26+
ch := make(chan storage.DeleteResult)
27+
go storage.DeleteMediaById(ids[i], ch)
28+
result := <-ch
29+
30+
if result.Err != nil {
2831
http.Error(w, "Could not find "+ids[i], http.StatusBadRequest)
2932
return
3033
}

src/service/GetMediaById.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ func GetMediaById(w http.ResponseWriter, r *http.Request) {
2525
}
2626

2727
for i := 0; i < len(ids); i++ {
28-
media, err := storage.GetMediaById(ids[i])
29-
if err != nil {
28+
ch := make(chan storage.GetResult)
29+
go storage.GetMediaByIdDb(ids[i], ch)
30+
31+
result := <-ch // Properly receive from the channel
32+
if result.Err != nil {
3033
http.Error(w, "Could not find "+ids[i], http.StatusBadRequest)
3134
return
3235
}
33-
mediaArray = append(mediaArray, media)
36+
37+
mediaArray = append(mediaArray, result.Media)
3438
}
3539

3640
response, _ := json.Marshal(mediaArray)

src/service/PostMedia.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,20 @@ func SaveMedia(handler *multipart.FileHeader, w http.ResponseWriter, mime string
9090
return "", err
9191
}
9292
width, height := models.GetWidthAndHeigthOfImage(file)
93-
93+
ch := make(chan storage.AddResult)
9494
media := models.Media{
9595
FileType: mime,
9696
Width: width,
9797
Height: height,
9898
OriginalFileName: handler.Filename,
9999
}
100100

101-
fileId, filepath, err := storage.AddNewMedia(media)
101+
go storage.AddNewMedia(media, ch)
102+
result := <-ch
103+
filepath, fileId, err := result.FileUrl, result.MediaId, result.Err
102104
if err != nil {
103105
return "", err
104106
}
105-
106107
dst, err := os.Create(filepath)
107108
if err != nil {
108109
http.Error(w, "Failed to create file", http.StatusInternalServerError)

src/storage/storage.go

+59-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import (
99
"os"
1010
)
1111

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+
}
1224
type User struct {
1325
ID uint `gorm:"primaryKey"`
1426
Username string `gorm:"unique"`
@@ -48,38 +60,65 @@ func SetupStorage() error {
4860
fmt.Println("Tables created successfully!")
4961
return nil
5062
}
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
5465
result := database.Debug().Create(&media)
5566
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
5874
}
59-
return media.MediaId, media.FileUrl, nil
60-
}
6175

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) {
6384
var media models.Media
6485

6586
result := database.Where("media_id = ?", fileId).First(&media)
6687
if result.Error != nil {
6788
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
6994
}
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
71100
}
72101

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
78113
}
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
82118
}
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}
85124
}

0 commit comments

Comments
 (0)