Add uploader

This commit is contained in:
Damillora 2021-05-11 17:50:34 +07:00
parent 2c20730aa5
commit ee9d2c80bf
7 changed files with 36 additions and 13 deletions

View File

@ -88,12 +88,13 @@ func postGetOne(c *gin.Context) {
Tags: tagStrings, Tags: tagStrings,
Width: post.Blob.Width, Width: post.Blob.Width,
Height: post.Blob.Height, Height: post.Blob.Height,
Uploader: post.User.Username,
}) })
} }
func postCreate(c *gin.Context) { func postCreate(c *gin.Context) {
var model models.PostCreateModel var model models.PostCreateModel
err := c.ShouldBindJSON(&model) err := c.BindJSON(&model)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse{ c.JSON(http.StatusBadRequest, models.ErrorResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -113,8 +114,17 @@ func postCreate(c *gin.Context) {
c.Abort() c.Abort()
return return
} }
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)
post, err := services.CreatePost(model) post, err := services.CreatePost(userObj.ID, model)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse{ c.JSON(http.StatusBadRequest, models.ErrorResponse{

View File

@ -6,6 +6,8 @@ import (
type Post struct { type Post struct {
ID string `gorm:"size:36"` ID string `gorm:"size:36"`
UserID string
User User
BlobID string BlobID string
Blob Blob `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Blob Blob `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
SourceURL string SourceURL string

View File

@ -43,6 +43,7 @@ func AuthMiddleware() gin.HandlerFunc {
return return
} }
user := services.GetUser(claims["sub"].(string)) user := services.GetUser(claims["sub"].(string))
c.Set("user", user) c.Set("user", user)
c.Next() c.Next()

View File

@ -7,4 +7,5 @@ type PostReadModel struct {
Tags []string `json:"tags"` Tags []string `json:"tags"`
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`
Uploader string `json:"uploader"`
} }

View File

@ -29,14 +29,14 @@ func GetPostTags(page int, tagSyntax []string) []database.Post {
func GetPost(id string) (*database.Post, error) { func GetPost(id string) (*database.Post, error) {
var post database.Post var post database.Post
result := database.DB.Joins("Blob").Preload("Tags").Preload("Tags.TagType").Where("posts.id = ?", id).First(&post) result := database.DB.Joins("User").Joins("Blob").Preload("Tags").Preload("Tags.TagType").Where("posts.id = ?", id).First(&post)
if result.Error != nil { if result.Error != nil {
return nil, result.Error return nil, result.Error
} }
return &post, nil return &post, nil
} }
func CreatePost(model models.PostCreateModel) (*database.Post, error) { func CreatePost(userID string, model models.PostCreateModel) (*database.Post, error) {
tags, err := ParseTags(model.Tags) tags, err := ParseTags(model.Tags)
if err != nil { if err != nil {
return nil, err return nil, err
@ -44,6 +44,7 @@ func CreatePost(model models.PostCreateModel) (*database.Post, error) {
post := database.Post{ post := database.Post{
ID: uuid.NewString(), ID: uuid.NewString(),
UserID: userID,
BlobID: model.BlobID, BlobID: model.BlobID,
SourceURL: model.SourceURL, SourceURL: model.SourceURL,
Tags: tags, Tags: tags,

View File

@ -43,11 +43,17 @@
to="/post/edit/{post.id}">Edit</Link to="/post/edit/{post.id}">Edit</Link
> >
</p> </p>
<p>
Uploader: {post.uploader}
</p>
<p> <p>
Source URL: <a href={post.source_url} Source URL: <a href={post.source_url}
>{trimUrl(post.source_url)}</a >{trimUrl(post.source_url)}</a
> >
</p> </p>
<p>
Dimensions: {post.width}x{post.height}
</p>
<p> <p>
Tags:<br /> Tags:<br />
</p> </p>

View File

@ -18,8 +18,12 @@
const splitToChunks = (array, parts) => { const splitToChunks = (array, parts) => {
let result = []; let result = [];
for (let i = parts; i > 0; i--) { for (let i = 0; i < parts; i++) {
result.push(array.slice(i * parts, i * parts + parts + 1)); let currentColumn = [];
for (let j = i; j < array.length; j += parts) {
currentColumn.push(array[j]);
}
result.push(currentColumn);
} }
return result; return result;
}; };
@ -27,11 +31,10 @@
let postChunks = []; let postChunks = [];
// split posts into 4 columns // split posts into 4 columns
$: { $: {
postChunks = splitToChunks(posts, 4); postChunks = splitToChunks(posts, 5);
} }
const getData = async () => { const getData = async () => {
console.log("page " + page);
const data = await getPostSearchTag({ page, q: searchTerms.join("+") }); const data = await getPostSearchTag({ page, q: searchTerms.join("+") });
if (data.posts) { if (data.posts) {
newBatch = data.posts; newBatch = data.posts;
@ -98,7 +101,7 @@
<div class="block"> <div class="block">
<div class="columns"> <div class="columns">
{#each postChunks as postChunk} {#each postChunks as postChunk}
<div class="column is-3"> <div class="column is-one-fifth">
{#each postChunk as post, i (post.id)} {#each postChunk as post, i (post.id)}
<div class="block"> <div class="block">
<div class="card"> <div class="card">
@ -130,7 +133,6 @@
<InfiniteScroll <InfiniteScroll
hasMore={newBatch.length} hasMore={newBatch.length}
elementScroll={document} elementScroll={document}
threshold={0}
on:loadMore={() => { on:loadMore={() => {
page++; page++;
getData(); getData();