Ajout de la route pour update l'utilisateur
This commit is contained in:
parent
3f5eeda369
commit
22e5371b30
3 changed files with 77 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
|
||||
}
|
||||
|
|
1
main.go
1
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" {
|
||||
|
|
Loading…
Add table
Reference in a new issue