You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.7 KiB

4 years ago
package api
import (
"net/http"
"github.com/gin-gonic/gin"
4 years ago
"github.com/imdotdev/im.dev/server/internal/interaction"
4 years ago
"github.com/imdotdev/im.dev/server/internal/story"
4 years ago
"github.com/imdotdev/im.dev/server/internal/user"
4 years ago
"github.com/imdotdev/im.dev/server/pkg/common"
"github.com/imdotdev/im.dev/server/pkg/e"
)
func SubmitPost(c *gin.Context) {
res, err := story.SubmitPost(c)
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(res))
}
func DeletePost(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid))
return
}
4 years ago
user := user.CurrentUser(c)
4 years ago
creator, err := story.GetPostCreator(id)
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
if user.ID != creator {
c.JSON(http.StatusForbidden, common.RespError(e.NoPermission))
return
}
err = story.DeletePost(id)
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(nil))
}
4 years ago
func GetStoryPost(c *gin.Context) {
4 years ago
id := c.Param("id")
4 years ago
user := user.CurrentUser(c)
4 years ago
ar, err := story.GetPost(id, "")
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
4 years ago
if user != nil {
4 years ago
ar.Liked = interaction.GetLiked(ar.ID, user.ID)
4 years ago
ar.Bookmarked, _ = story.Bookmarked(user.ID, ar.ID)
4 years ago
}
c.JSON(http.StatusOK, common.RespSuccess(ar))
}
4 years ago
func Bookmark(c *gin.Context) {
storyID := c.Param("storyID")
4 years ago
4 years ago
user := user.CurrentUser(c)
err := story.Bookmark(user.ID, storyID)
4 years ago
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
4 years ago
c.JSON(http.StatusOK, common.RespSuccess(nil))
4 years ago
}