From 22e5371b3032eb6d104b2513db6bb1331b04a130 Mon Sep 17 00:00:00 2001 From: Nabil Ould Hamou Date: Thu, 7 Mar 2024 04:35:55 +0100 Subject: [PATCH] Ajout de la route pour update l'utilisateur --- controllers/postController.go | 10 +++++- controllers/userController.go | 68 ++++++++++++++++++++++++++++++++++- main.go | 1 + 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/controllers/postController.go b/controllers/postController.go index 7da8603..9d0e137 100644 --- a/controllers/postController.go +++ b/controllers/postController.go @@ -28,12 +28,20 @@ func GetPosts(c *gin.Context) { if err != nil || page <= 0 { page = 1 } + query := c.Query("query") var posts []models.Post offset := (page - 1) * 20 - result := initializers.DB.Offset(offset).Limit(20).Order("created_at desc").Find(&posts) + var result *gorm.DB + + if query != "" { + result = initializers.DB.Preload("Files").Where("text LIKE ?", "%"+query+"%").Offset(offset).Limit(20).Order("created_at desc").Find(&posts) + } else { + result = initializers.DB.Preload("Files").Offset(offset).Limit(20).Order("created_at desc").Find(&posts) + } + if result.Error != nil { c.Status(http.StatusBadRequest) return diff --git a/controllers/userController.go b/controllers/userController.go index 314505c..7b3dd89 100644 --- a/controllers/userController.go +++ b/controllers/userController.go @@ -6,6 +6,7 @@ import ( "errors" "github.com/gin-gonic/gin" "github.com/google/uuid" + "golang.org/x/crypto/bcrypt" "gorm.io/gorm" "net/http" "strconv" @@ -69,7 +70,7 @@ func GetUserById(c *gin.Context) { } var user models.User - result := initializers.DB.Preload("Posts.Files").First(&user, "id = ?", uniqueId) + result := initializers.DB.Preload("Avatar").Preload("Posts.Files").First(&user, "id = ?", uniqueId) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { @@ -82,3 +83,68 @@ func GetUserById(c *gin.Context) { c.JSON(http.StatusOK, user) } + +func UpdateUser(c *gin.Context) { + form, _ := c.MultipartForm() + uploadedFiles := form.File["Avatar"] + var body struct { + Email string + Password string + DisplayName string + } + + if err := c.Bind(&body); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format"}) + return + } + + if len(uploadedFiles) == 0 { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "No files were uploaded.", + }) + return + } + + files, err := uploadFiles(c, uploadedFiles) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err.Error(), + }) + return + } + + currentUserId := c.GetString("userId") + id, _ := uuid.Parse(currentUserId) + + initializers.DB.Model(&files[0]).Update("user_id", id.String()) + + var user models.User + result := initializers.DB.Preload("Avatar").First(&user, "id = ?", id) + if result.Error != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"}) + return + } + + if body.Password != "" { + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(body.Password), bcrypt.DefaultCost) + if err != nil { + c.Status(http.StatusInternalServerError) + } + + initializers.DB.Model(&user).Updates(models.User{ + Email: body.Email, + Password: string(hashedPassword), + DisplayName: body.DisplayName, + }) + } else { + initializers.DB.Model(&user).Updates(models.User{ + Email: body.Email, + DisplayName: body.DisplayName, + }) + } + + c.JSON(http.StatusAccepted, gin.H{ + "user": user, + }) + +} diff --git a/main.go b/main.go index ab94898..3486302 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ func main() { // Users api.Api.GET("/users", middleware.RequireAuth, controllers.GetUsers) api.Api.GET("/users/:id", middleware.RequireAuth, controllers.GetUserById) + api.Api.PUT("/users", middleware.RequireAuth, controllers.UpdateUser) // Files if currentMode == "debug" {