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 977a12a commit ab46517
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions controllers/profile/UpdatePhotoProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,28 @@ func UpdatePhotoProfile(c *gin.Context, router *gin.RouterGroup) {
}

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
c.JSON(http.StatusInternalServerError, gin.H{
"status": err,
})
return
}
}()

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

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": errors,
})
return
}
wg.Wait()

if photoProfileUser.Path != "" {
if err := os.Remove(photoProfileUser.Path); err != nil {
Expand Down

3 comments on commit ab46517

@vercel
Copy link

@vercel vercel bot commented on ab46517 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 ab46517 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 ab46517 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.