57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package postsRoutes
|
|
|
|
import (
|
|
"context"
|
|
rest_api_stuff "enshi/REST_API_stuff"
|
|
db_repo "enshi/db/go_queries"
|
|
"enshi/db_connection"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func GetRandomPost(c *gin.Context) {
|
|
limit, err := strconv.Atoi(c.DefaultQuery("limit", "10"))
|
|
|
|
if err != nil {
|
|
rest_api_stuff.InternalErrorAnswer(c, err)
|
|
return
|
|
}
|
|
|
|
offset, err := strconv.Atoi(c.DefaultQuery("offset", "0"))
|
|
|
|
if err != nil {
|
|
rest_api_stuff.InternalErrorAnswer(c, err)
|
|
return
|
|
}
|
|
|
|
params := db_repo.GetRandomPostsParams{
|
|
Column1: int32(limit),
|
|
Column2: int32(offset),
|
|
Offset: int32(offset * limit),
|
|
}
|
|
|
|
postsData, err :=
|
|
db_repo.New(db_connection.Dbx).
|
|
GetRandomPosts(context.Background(), params)
|
|
|
|
if err != nil {
|
|
rest_api_stuff.InternalErrorAnswer(c, err)
|
|
return
|
|
}
|
|
|
|
// result := make([]any, 0)
|
|
|
|
// for _, post := range postsData {
|
|
// result = append(result, gin.H{
|
|
// "post_id": strconv.Itoa(int(post.PostID)),
|
|
// "title": post.Title,
|
|
// "user_id": strconv.Itoa(int(post.UserID)),
|
|
// })
|
|
// }
|
|
|
|
c.IndentedJSON(http.StatusOK, postsData)
|
|
|
|
}
|