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

View File

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

View File

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

View File

@ -7,4 +7,5 @@ type PostReadModel struct {
Tags []string `json:"tags"`
Width int `json:"width"`
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) {
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 {
return nil, result.Error
}
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)
if err != nil {
return nil, err
@ -44,6 +44,7 @@ func CreatePost(model models.PostCreateModel) (*database.Post, error) {
post := database.Post{
ID: uuid.NewString(),
UserID: userID,
BlobID: model.BlobID,
SourceURL: model.SourceURL,
Tags: tags,

View File

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

View File

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