Skip to content

Commit

Permalink
add go routines
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnsj4 committed Jun 23, 2023
1 parent a098395 commit 840949f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 59 deletions.
90 changes: 63 additions & 27 deletions controllers/profile/UpdatePhotoProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/models"
"net/http"
"os"
"sync"
)

func UpdatePhotoProfile(c *gin.Context, router *gin.RouterGroup) {
var (
user models.UserDescDB
photoProfileUser models.UserPhotoProfileDB
photoProfileUpload models.PhotoProfile
wg *sync.WaitGroup
)

db, err := database.ConnectDB()
Expand All @@ -33,15 +35,36 @@ func UpdatePhotoProfile(c *gin.Context, router *gin.RouterGroup) {
return
}

if err := db.Table("user").Where("id = ?", userId).First(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err,
})
return
}
if err := db.Table("photo_profile").Where("user_id = ?", userId).First(&photoProfileUser).Error; err != nil {
wg.Add(2)
errChan := make(chan error)

go func() {
defer wg.Done()
if err := db.Table("user").Where("id = ?", userId).First(&user).Error; err != nil {
errChan <- err
}
}()

go func() {
defer wg.Done()
if err := db.Table("photo_profile").Where("user_id = ?", userId).First(&photoProfileUser).Error; err != nil {
errChan <- err
}
}()

go func() {
wg.Wait()
close(errChan)
}()

if len(errChan) != 0 {
errors := make([]string, len(errChan))
for value := range errChan {
errors = append(errors, value.Error())
}

c.JSON(http.StatusInternalServerError, gin.H{
"status": err,
"status": errors,
})
return
}
Expand All @@ -66,27 +89,40 @@ func UpdatePhotoProfile(c *gin.Context, router *gin.RouterGroup) {
imageName := photoProfileUpload.Image.Filename
path := "././assets/" + userId + "/profile/" + uid + "_" + imageName

if err := c.SaveUploadedFile(photoProfileUpload.Image, path); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": err.Error(),
})
return
}

photoProfileUser.Path = path
user.PhotoProfile = "https://wallpapercollectapi-production-c728.up.railway.app/photo_profile/" + uid
if err := db.Table("photo_profile").Save(&photoProfileUser).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err,
})
return
}
if err := db.Table("user").Save(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err,
})
return
}

wg.Add(3)

go func() {
defer wg.Done()
if err := c.SaveUploadedFile(photoProfileUpload.Image, path); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": err.Error(),
})
return
}
}()

go func() {
defer wg.Done()
if err := db.Table("photo_profile").Save(&photoProfileUser).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err,
})
return
}
}()

go func() {
defer wg.Done()
if err := db.Table("user").Save(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err,
})
return
}
}()

file, err := os.Open(path)
if err != nil {
Expand Down
79 changes: 47 additions & 32 deletions controllers/profile/UploadPhotoProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
models2 "github.com/models"
"net/http"
"os"
"sync"
)

func PhotoProfileUpload(c *gin.Context, router *gin.RouterGroup) {
Expand All @@ -16,6 +17,7 @@ func PhotoProfileUpload(c *gin.Context, router *gin.RouterGroup) {
ppUpload models2.PhotoProfile
user models2.UserDescDB
photoProfileDB models2.UserPhotoProfileDB
wg *sync.WaitGroup
)

db, err := database.ConnectDB()
Expand Down Expand Up @@ -44,42 +46,55 @@ func PhotoProfileUpload(c *gin.Context, router *gin.RouterGroup) {
imageName := ppUpload.Image.Filename
path := "././assets/" + userId + "/profile/" + uid + "_" + imageName

if err := c.SaveUploadedFile(ppUpload.Image, path); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": err.Error(),
})
return
}
wg.Add(3)

if err := db.Table("user").Where("id = ?", userId).First(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
go func() {
defer wg.Done()
if err := c.SaveUploadedFile(ppUpload.Image, path); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": err.Error(),
})
return
}
}()

user.PhotoProfile = "https://wallpapercollectapi-production-c728.up.railway.app/photo_profile/" + uid
if err := db.Table("user").Save(user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
go func() {
defer wg.Done()
if err := db.Table("user").Where("id = ?", userId).First(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}

if err := db.Table("photo_profile").Where("user_id = ?", userId).First(&photoProfileDB).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
user.PhotoProfile = "https://wallpapercollectapi-production-c728.up.railway.app/photo_profile/" + uid
if err := db.Table("user").Save(user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
}()

photoProfileDB.Path = path
if err := db.Table("photo_profile").Save(&photoProfileDB).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
go func() {
defer wg.Done()
if err := db.Table("photo_profile").Where("user_id = ?", userId).First(&photoProfileDB).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}

photoProfileDB.Path = path
if err := db.Table("photo_profile").Save(&photoProfileDB).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
}()

wg.Wait()

file, err := os.Open(path)
if err != nil {
Expand Down

3 comments on commit 840949f

@vercel
Copy link

@vercel vercel bot commented on 840949f Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 840949f Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 840949f Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.