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 588a065 commit 7c7667e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 60 deletions.
86 changes: 50 additions & 36 deletions controllers/profile/UserDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/models"
"net/http"
"os"
"sync"
)

func UserDelete(c *gin.Context) {
Expand All @@ -15,6 +16,7 @@ func UserDelete(c *gin.Context) {
userDelete models.UserDescDB
wallpaperCollections []models.WallpaperCollectionDB
photoProfileDelete models.UserPhotoProfileDB
wg sync.WaitGroup
)

userId, err := data.GetUserIdFromCookies(c)
Expand All @@ -32,61 +34,73 @@ func UserDelete(c *gin.Context) {
})
return
}
wg.Add(3)

if _ = db.Table("user").Where("id = ?", userId).First(&userDelete); userDelete.Id == "" {
c.JSON(http.StatusBadRequest, gin.H{
"status": "user not found",
})
return
}

if _ = db.Table("photo_profile").Where("user_id = ?", userId).First(&photoProfileDelete); photoProfileDelete.UserId == "" {
c.JSON(http.StatusBadRequest, gin.H{
"status": "photo profile not found",
})
return
}

if photoProfileDelete.Path != "" {
if err := os.Remove(photoProfileDelete.Path); err != nil {
go func() {
defer wg.Done()
if _ = db.Table("user").Where("id = ?", userId).First(&userDelete); userDelete.Id == "" {
c.JSON(http.StatusBadRequest, gin.H{
"status": "path not found",
"status": "user not found",
})
return
}
}
}()

if err := db.Table("photo_profile").Delete(photoProfileDelete).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}

if err := db.Table("wallpaper_collect").Where("user_id = ?", userId).Find(&wallpaperCollections).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "wallpapers not found",
})
return
}
go func() {
defer wg.Done()
if _ = db.Table("photo_profile").Where("user_id = ?", userId).First(&photoProfileDelete); photoProfileDelete.UserId == "" {
c.JSON(http.StatusBadRequest, gin.H{
"status": "photo profile not found",
})
return
}

if len(wallpaperCollections) != 0 {
for _, value := range wallpaperCollections {
if err := os.Remove(value.Path); err != nil {
if photoProfileDelete.Path != "" {
if err := os.Remove(photoProfileDelete.Path); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "path not found",
})
return
}
}

if err := db.Table("wallpaper_collect").Delete(wallpaperCollections).Error; err != nil {
if err := db.Table("photo_profile").Delete(photoProfileDelete).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
}
}()

go func() {
defer wg.Done()
if err := db.Table("wallpaper_collect").Where("user_id = ?", userId).Find(&wallpaperCollections).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "wallpapers not found",
})
return
}

if len(wallpaperCollections) != 0 {
for _, value := range wallpaperCollections {
if err := os.Remove(value.Path); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "path not found",
})
return
}
}

if err := db.Table("wallpaper_collect").Delete(wallpaperCollections).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
}
}()

wg.Wait()

if err := db.Table("user").Delete(userDelete).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
Expand Down
67 changes: 43 additions & 24 deletions controllers/register/AuthGoogle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/http"
"os"
"sync"
"time"
)

Expand All @@ -20,6 +21,7 @@ func CreateUserAuthGoogle(c *gin.Context) {
justCheck models.UserDescDB
userPhotoProfile models.UserPhotoProfileDB
googleToken models.GoogleToken
wg sync.WaitGroup
)

db, err := database.ConnectDB()
Expand Down Expand Up @@ -94,32 +96,49 @@ func CreateUserAuthGoogle(c *gin.Context) {
Path: "",
}

if err := os.MkdirAll("././assets/"+userDesc.Id+"/wallpaper_collection", os.ModePerm); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": err.Error(),
})
return
}
wg.Add(4)

if err := os.MkdirAll(pathProfile, os.ModePerm); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": err.Error(),
})
return
}
go func() {
defer wg.Done()
if err := os.MkdirAll("././assets/"+userDesc.Id+"/wallpaper_collection", os.ModePerm); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": err.Error(),
})
return
}
}()

if err := db.Table("user").Create(&userDesc).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
if err := db.Table("photo_profile").Create(&userPhotoProfile).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}
go func() {
defer wg.Done()
if err := os.MkdirAll(pathProfile, os.ModePerm); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": err.Error(),
})
return
}
}()

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

go func() {
defer wg.Done()
if err := db.Table("photo_profile").Create(&userPhotoProfile).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 7c7667e

@vercel
Copy link

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