2021-05-09 15:07:23 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2021-05-10 20:25:33 +00:00
|
|
|
"strings"
|
2021-05-09 15:07:23 +00:00
|
|
|
|
2021-05-10 20:25:33 +00:00
|
|
|
"github.com/Damillora/Shioriko/pkg/database"
|
2021-05-09 15:07:23 +00:00
|
|
|
"github.com/Damillora/Shioriko/pkg/middleware"
|
|
|
|
"github.com/Damillora/Shioriko/pkg/models"
|
|
|
|
"github.com/Damillora/Shioriko/pkg/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitializePostRoutes(g *gin.Engine) {
|
|
|
|
unprotected := g.Group("/api/post")
|
|
|
|
{
|
|
|
|
unprotected.GET("/", postGet)
|
|
|
|
unprotected.GET("/:id", postGetOne)
|
|
|
|
}
|
|
|
|
protected := g.Group("/api/post").Use(middleware.AuthMiddleware())
|
|
|
|
{
|
|
|
|
protected.POST("/create", postCreate)
|
|
|
|
protected.POST("/:id", postUpdate)
|
|
|
|
protected.DELETE("/:id", postDelete)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func postGet(c *gin.Context) {
|
|
|
|
pageParam := c.Query("page")
|
|
|
|
page, _ := strconv.Atoi(pageParam)
|
|
|
|
|
2022-04-14 14:27:27 +00:00
|
|
|
if page < 1 {
|
|
|
|
page = 1
|
|
|
|
}
|
2021-05-10 20:25:33 +00:00
|
|
|
tag := c.Query("tags")
|
2021-05-09 15:07:23 +00:00
|
|
|
|
2021-05-10 20:25:33 +00:00
|
|
|
tags := strings.Split(tag, " ")
|
2021-05-09 15:07:23 +00:00
|
|
|
|
2021-05-10 20:25:33 +00:00
|
|
|
var posts []database.Post
|
|
|
|
var postPages int
|
2022-04-14 14:27:27 +00:00
|
|
|
var perPage = 20
|
2021-05-10 20:25:33 +00:00
|
|
|
|
|
|
|
if tag != "" {
|
|
|
|
posts = services.GetPostTags(page, tags)
|
|
|
|
postPages = services.CountPostPagesTag(tags)
|
|
|
|
} else {
|
|
|
|
posts = services.GetPostAll(page)
|
|
|
|
postPages = services.CountPostPages()
|
|
|
|
}
|
2021-05-09 15:07:23 +00:00
|
|
|
|
2022-04-14 14:27:27 +00:00
|
|
|
var totalPage = postPages / perPage
|
|
|
|
if postPages%perPage > 0 {
|
|
|
|
totalPage++
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
var postResult []models.PostListItem
|
2022-04-14 17:12:07 +00:00
|
|
|
var tagStrings []string
|
2021-05-09 15:07:23 +00:00
|
|
|
for _, post := range posts {
|
2022-04-14 17:12:07 +00:00
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
for _, tag := range post.Tags {
|
|
|
|
tagStrings = append(tagStrings, tag.TagType.Name+":"+tag.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
postResult = append(postResult, models.PostListItem{
|
2021-05-11 12:21:11 +00:00
|
|
|
ID: post.ID,
|
|
|
|
ImageThumbnailPath: "/data/" + post.Blob.ThumbnailFilePath,
|
|
|
|
ImagePath: "/data/" + post.Blob.FilePath,
|
2021-05-09 15:07:23 +00:00
|
|
|
})
|
|
|
|
}
|
2022-04-14 17:37:03 +00:00
|
|
|
tagObjs := services.GetTagFilterString(tagStrings)
|
2022-04-14 17:26:11 +00:00
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
c.JSON(http.StatusOK, models.PostPaginationResponse{
|
|
|
|
CurrentPage: page,
|
2022-04-14 14:27:27 +00:00
|
|
|
TotalPage: totalPage,
|
2021-05-11 10:06:25 +00:00
|
|
|
PostCount: postPages,
|
2021-05-09 15:07:23 +00:00
|
|
|
Posts: postResult,
|
2022-04-14 17:26:11 +00:00
|
|
|
Tags: tagObjs,
|
2021-05-09 15:07:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func postGetOne(c *gin.Context) {
|
|
|
|
id := c.Param("id")
|
|
|
|
post, err := services.GetPost(id)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
2022-04-14 17:37:03 +00:00
|
|
|
|
|
|
|
tagObjs := services.GetTagFilter(post.Tags)
|
2021-05-09 15:07:23 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, models.PostReadModel{
|
2021-05-11 12:21:11 +00:00
|
|
|
ID: post.ID,
|
|
|
|
ImagePreviewPath: "/data/" + post.Blob.PreviewFilePath,
|
|
|
|
ImagePath: "/data/" + post.Blob.FilePath,
|
|
|
|
SourceURL: post.SourceURL,
|
2022-04-14 17:37:03 +00:00
|
|
|
Tags: tagObjs,
|
2021-05-11 12:21:11 +00:00
|
|
|
Width: post.Blob.Width,
|
|
|
|
Height: post.Blob.Height,
|
|
|
|
Uploader: post.User.Username,
|
2021-05-09 15:07:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func postCreate(c *gin.Context) {
|
|
|
|
var model models.PostCreateModel
|
2021-05-11 10:50:34 +00:00
|
|
|
err := c.BindJSON(&model)
|
2021-05-09 15:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
validate := validator.New()
|
|
|
|
err = validate.Struct(model)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
2021-05-11 10:50:34 +00:00
|
|
|
user, ok := c.Get("user")
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusForbidden, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: "User don't exist",
|
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
userObj := user.(*database.User)
|
2021-05-09 15:07:23 +00:00
|
|
|
|
2021-05-11 10:50:34 +00:00
|
|
|
post, err := services.CreatePost(userObj.ID, model)
|
2021-05-09 15:07:23 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.PostReadModel{
|
|
|
|
ID: post.ID,
|
|
|
|
ImagePath: "/data/" + post.Blob.FilePath,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func postUpdate(c *gin.Context) {
|
2022-04-15 19:20:14 +00:00
|
|
|
_, ok := c.Get("user")
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusForbidden, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: "User don't exist",
|
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
id := c.Param("id")
|
|
|
|
|
|
|
|
var model models.PostUpdateModel
|
|
|
|
err := c.ShouldBindJSON(&model)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
validate := validator.New()
|
|
|
|
err = validate.Struct(model)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
post, err := services.UpdatePost(id, model)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
2021-05-11 11:06:48 +00:00
|
|
|
return
|
2021-05-09 15:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, models.PostListItem{
|
|
|
|
ID: post.ID,
|
|
|
|
ImagePath: "/data/" + post.Blob.FilePath,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func postDelete(c *gin.Context) {
|
2022-04-15 19:20:14 +00:00
|
|
|
_, ok := c.Get("user")
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusForbidden, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: "User don't exist",
|
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
id := c.Param("id")
|
|
|
|
|
|
|
|
err := services.DeletePost(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, models.ErrorResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, models.ErrorResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
Message: "Success",
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|