2024-01-16 00:36:49 +01:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"azote-backend/initializers"
|
|
|
|
"azote-backend/models"
|
|
|
|
"errors"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/google/uuid"
|
2024-03-07 04:35:55 +01:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2024-01-16 00:36:49 +01:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"net/http"
|
2024-02-28 17:03:42 +01:00
|
|
|
"strconv"
|
2024-01-16 00:36:49 +01:00
|
|
|
)
|
|
|
|
|
2024-03-04 17:07:01 +01:00
|
|
|
type UserData struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
Avatar string `json:"profile_picture"`
|
|
|
|
}
|
|
|
|
|
2024-01-16 00:36:49 +01:00
|
|
|
// GetUsers Returns all users
|
|
|
|
func GetUsers(c *gin.Context) {
|
2024-02-28 17:03:42 +01:00
|
|
|
page, err := strconv.Atoi(c.Query("page"))
|
|
|
|
if err != nil || page <= 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
2024-03-04 17:07:01 +01:00
|
|
|
username := c.Query("username")
|
2024-02-28 17:03:42 +01:00
|
|
|
|
2024-01-16 00:36:49 +01:00
|
|
|
var users []models.User
|
|
|
|
|
2024-03-04 17:07:01 +01:00
|
|
|
offset := (page - 1) * 20
|
|
|
|
|
|
|
|
var result *gorm.DB
|
|
|
|
|
|
|
|
if username != "" {
|
2024-03-07 05:24:34 +01:00
|
|
|
result = initializers.DB.Preload("Avatar").Where("username LIKE ?", username+"%").Offset(offset).Limit(20).Find(&users)
|
2024-03-04 17:07:01 +01:00
|
|
|
} else {
|
2024-03-07 05:24:34 +01:00
|
|
|
result = initializers.DB.Preload("Avatar").Offset(offset).Limit(20).Find(&users)
|
2024-03-04 17:07:01 +01:00
|
|
|
}
|
2024-02-28 17:03:42 +01:00
|
|
|
|
2024-01-16 00:36:49 +01:00
|
|
|
if result.Error != nil {
|
|
|
|
c.Status(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-04 17:07:01 +01:00
|
|
|
var response []UserData
|
2024-02-28 17:03:42 +01:00
|
|
|
for _, user := range users {
|
2024-03-04 17:07:01 +01:00
|
|
|
respUser := UserData{
|
2024-02-28 17:03:42 +01:00
|
|
|
ID: user.ID,
|
|
|
|
Username: user.Username,
|
|
|
|
DisplayName: user.DisplayName,
|
|
|
|
Avatar: user.Avatar.FileName,
|
|
|
|
}
|
|
|
|
response = append(response, respUser)
|
|
|
|
}
|
|
|
|
|
2024-01-16 00:36:49 +01:00
|
|
|
c.JSON(http.StatusAccepted, gin.H{
|
2024-02-28 17:03:42 +01:00
|
|
|
"users": response,
|
2024-01-16 00:36:49 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserById(c *gin.Context) {
|
|
|
|
userId := c.Param("id")
|
|
|
|
|
|
|
|
uniqueId, err := uuid.Parse(userId)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id format"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var user models.User
|
2024-03-07 04:35:55 +01:00
|
|
|
result := initializers.DB.Preload("Avatar").Preload("Posts.Files").First(&user, "id = ?", uniqueId)
|
2024-01-16 00:36:49 +01:00
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, user)
|
|
|
|
}
|
2024-03-07 04:35:55 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-04-08 22:47:55 +02:00
|
|
|
if len(uploadedFiles) == 1 {
|
|
|
|
initializers.DB.Model(&files[0]).Update("user_id", id.String())
|
|
|
|
}
|
2024-03-07 04:35:55 +01:00
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|