Skip to content

Commit

Permalink
add user delete
Browse files Browse the repository at this point in the history
  • Loading branch information
fchrgrib committed Jun 15, 2023
1 parent 7f84137 commit 1eaeed7
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 19 deletions.
111 changes: 92 additions & 19 deletions controllers/profile/UserDelete.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,102 @@
package profile

import (
"github.com/database"
"github.com/gin-gonic/gin"
"github.com/libs/utils/data"
"github.com/models"
"net/http"
"os"
)

func UserDelete(c *gin.Context) {

//var (
// userDelete models.UserDescDB
//)
//
//userId, err := data.GetUserIdFromCookies(c)
//if err != nil {
// c.JSON(http.StatusUnauthorized, gin.H{
// "status": err.Error(),
// })
// return
//}
//
//db, err := database.ConnectDB()
//if err != nil {
// c.JSON(http.StatusInternalServerError, gin.H{
// "status": err.Error(),
// })
// return
//}
var (
userDelete models.UserDescDB
wallpaperCollections []models.WallpaperCollectionDB
photoProfileDelete models.UserPhotoProfileDB
)

userId, err := data.GetUserIdFromCookies(c)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"status": err.Error(),
})
return
}

db, err := database.ConnectDB()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": err.Error(),
})
return
}

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 {
c.JSON(http.StatusBadRequest, gin.H{
"status": "path 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
}

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
}

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

c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
return
}
1 change: 1 addition & 0 deletions handlers/routers/Profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func Profile(routers *gin.Engine) {
privateRouters := routers.Group("/wallpaper")
privateRouters.Use(middleware.JWT)
profileRouter := privateRouters.Group("/profile")
profileRouter.DELETE("/delete", profile.UserDelete)
profileRouter.GET("", profile.Info)
profileRouter.PUT("/update_profile_desc", profile.UpdateProfileDescription)

Expand Down

3 comments on commit 1eaeed7

@vercel
Copy link

@vercel vercel bot commented on 1eaeed7 Jun 15, 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 1eaeed7 Jun 15, 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 1eaeed7 Jun 15, 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.