feat: new endpoints for posts and change in the get users to get by usernames.
This commit is contained in:
parent
2f99c09bfe
commit
3f5eeda369
4 changed files with 124 additions and 15 deletions
|
@ -7,13 +7,112 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/h2non/filetype"
|
||||
"gorm.io/gorm"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PostData struct {
|
||||
PostId uuid.UUID `json:"id"`
|
||||
Text string `json:"text"`
|
||||
Files []models.File `json:"files"`
|
||||
User UserData `json:"user"`
|
||||
}
|
||||
|
||||
func GetPosts(c *gin.Context) {
|
||||
page, err := strconv.Atoi(c.Query("page"))
|
||||
if err != nil || page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
var posts []models.Post
|
||||
|
||||
offset := (page - 1) * 20
|
||||
|
||||
result := initializers.DB.Offset(offset).Limit(20).Order("created_at desc").Find(&posts)
|
||||
if result.Error != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var response []PostData
|
||||
for _, post := range posts {
|
||||
var user models.User
|
||||
result = initializers.DB.Preload("Avatar").First(&user, "id = ?", post.Author)
|
||||
|
||||
if result.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
respUser := PostData{
|
||||
PostId: post.ID,
|
||||
Text: post.Text,
|
||||
Files: post.Files,
|
||||
User: UserData{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
DisplayName: user.DisplayName,
|
||||
Avatar: user.Avatar.FileName,
|
||||
},
|
||||
}
|
||||
response = append(response, respUser)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusAccepted, gin.H{
|
||||
"posts": response,
|
||||
})
|
||||
}
|
||||
|
||||
func GetPostById(c *gin.Context) {
|
||||
postId := c.Param("id")
|
||||
|
||||
uniqueId, err := uuid.Parse(postId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id format"})
|
||||
return
|
||||
}
|
||||
|
||||
var postData PostData
|
||||
|
||||
var post models.Post
|
||||
result := initializers.DB.Preload("Files").First(&post, "id = ?", uniqueId)
|
||||
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Post not found"})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var user models.User
|
||||
result = initializers.DB.Preload("Avatar").First(&user, "id = ?", post.Author)
|
||||
|
||||
if result.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
|
||||
postData = PostData{
|
||||
PostId: uniqueId,
|
||||
Text: post.Text,
|
||||
Files: post.Files,
|
||||
User: UserData{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
DisplayName: user.DisplayName,
|
||||
Avatar: user.Avatar.FileName,
|
||||
},
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, postData)
|
||||
}
|
||||
|
||||
func CreatePost(c *gin.Context) {
|
||||
form, _ := c.MultipartForm()
|
||||
uploadedFiles := form.File["files"]
|
||||
|
|
|
@ -11,33 +11,41 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
type UserData struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Avatar string `json:"profile_picture"`
|
||||
}
|
||||
|
||||
// GetUsers Returns all users
|
||||
func GetUsers(c *gin.Context) {
|
||||
page, err := strconv.Atoi(c.Query("page"))
|
||||
if err != nil || page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
type userResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Avatar string `json:"profile_picture"`
|
||||
}
|
||||
username := c.Query("username")
|
||||
|
||||
var users []models.User
|
||||
|
||||
offset := (page - 1) * 50
|
||||
offset := (page - 1) * 20
|
||||
|
||||
var result *gorm.DB
|
||||
|
||||
if username != "" {
|
||||
result = initializers.DB.Where("username LIKE ?", username+"%").Offset(offset).Limit(20).Find(&users)
|
||||
} else {
|
||||
result = initializers.DB.Offset(offset).Limit(20).Find(&users)
|
||||
}
|
||||
|
||||
result := initializers.DB.Offset(offset).Limit(50).Find(&users)
|
||||
if result.Error != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var response []userResponse
|
||||
var response []UserData
|
||||
for _, user := range users {
|
||||
respUser := userResponse{
|
||||
respUser := UserData{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
DisplayName: user.DisplayName,
|
||||
|
|
6
main.go
6
main.go
|
@ -34,8 +34,8 @@ func main() {
|
|||
api.Api.GET("/validate", middleware.RequireAuth, controllers.ValidateToken)
|
||||
|
||||
// Users
|
||||
api.Api.GET("/users", controllers.GetUsers)
|
||||
api.Api.GET("/users/:id", controllers.GetUserById)
|
||||
api.Api.GET("/users", middleware.RequireAuth, controllers.GetUsers)
|
||||
api.Api.GET("/users/:id", middleware.RequireAuth, controllers.GetUserById)
|
||||
|
||||
// Files
|
||||
if currentMode == "debug" {
|
||||
|
@ -46,6 +46,8 @@ func main() {
|
|||
|
||||
// Posts
|
||||
api.Api.POST("/posts", middleware.RequireAuth, controllers.CreatePost)
|
||||
api.Api.GET("/posts", middleware.RequireAuth, controllers.GetPosts)
|
||||
api.Api.GET("/posts/:id", middleware.RequireAuth, controllers.GetPostById)
|
||||
|
||||
// Starting
|
||||
err := api.Router.Run()
|
||||
|
|
|
@ -5,6 +5,6 @@ import "github.com/google/uuid"
|
|||
type File struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement;"`
|
||||
FileName string
|
||||
PostID *uuid.UUID
|
||||
UserID *uuid.UUID
|
||||
PostID *uuid.UUID `json:"-"`
|
||||
UserID *uuid.UUID `json:"-"`
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue