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 0ab629f commit 588a065
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions controllers/profile/UpdateProfileDescription.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"github.com/libs/utils/validation"
"github.com/models"
"net/http"
"sync"
"time"
)

func UpdateProfileDescription(c *gin.Context) {

var user models.UserDescDB
var userUpdate models.UserDescDB
var (
user models.UserDescDB
userUpdate models.UserDescDB
wg sync.WaitGroup
)

db, err := database.ConnectDB()
if err != nil {
Expand Down Expand Up @@ -45,31 +49,44 @@ func UpdateProfileDescription(c *gin.Context) {
return
}

if !validation.ValidateEmail(userUpdate.Email) {
c.JSON(http.StatusNotAcceptable, gin.H{
"status": "invalid email",
})
return
}
wg.Add(3)

if !validation.ValidationNumberPhone(userUpdate.PhoneNumber) {
c.JSON(http.StatusNotAcceptable, gin.H{
"status": "invalid phone number",
})
return
}
go func() {
defer wg.Done()
if !validation.ValidateEmail(userUpdate.Email) {
c.JSON(http.StatusNotAcceptable, gin.H{
"status": "invalid email",
})
return
}
}()

t := time.Now().Local()
user.UserName = userUpdate.UserName
user.Email = userUpdate.Email
user.PhoneNumber = userUpdate.PhoneNumber
user.UpdatedAt = &t
if err := db.Table("user").Save(user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
go func() {
wg.Done()
if !validation.ValidationNumberPhone(userUpdate.PhoneNumber) {
c.JSON(http.StatusNotAcceptable, gin.H{
"status": "invalid phone number",
})
return
}
}()

go func() {
wg.Done()
t := time.Now().Local()
user.UserName = userUpdate.UserName
user.Email = userUpdate.Email
user.PhoneNumber = userUpdate.PhoneNumber
user.UpdatedAt = &t
if err := db.Table("user").Save(user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
}()

wg.Wait()

c.JSON(http.StatusOK, gin.H{
"status": "ok",
Expand Down

3 comments on commit 588a065

@vercel
Copy link

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