diff --git a/pkg/app/post_routes.go b/pkg/app/post_routes.go index 52a368b..48a0d9d 100644 --- a/pkg/app/post_routes.go +++ b/pkg/app/post_routes.go @@ -3,7 +3,9 @@ package app import ( "net/http" "strconv" + "strings" + "github.com/Damillora/Shioriko/pkg/database" "github.com/Damillora/Shioriko/pkg/middleware" "github.com/Damillora/Shioriko/pkg/models" "github.com/Damillora/Shioriko/pkg/services" @@ -16,7 +18,6 @@ func InitializePostRoutes(g *gin.Engine) { { unprotected.GET("/", postGet) unprotected.GET("/:id", postGetOne) - unprotected.GET("/tag/:id", postGetTag) } protected := g.Group("/api/post").Use(middleware.AuthMiddleware()) { @@ -30,7 +31,22 @@ func InitializePostRoutes(g *gin.Engine) { func postGet(c *gin.Context) { pageParam := c.Query("page") page, _ := strconv.Atoi(pageParam) - posts := services.GetPostAll(page) + + tag := c.Query("tags") + + tags := strings.Split(tag, " ") + + var posts []database.Post + var postPages int + + if tag != "" { + posts = services.GetPostTags(page, tags) + postPages = services.CountPostPagesTag(tags) + } else { + posts = services.GetPostAll(page) + postPages = services.CountPostPages() + } + var postResult []models.PostListItem for _, post := range posts { var tagStrings []string @@ -44,35 +60,6 @@ func postGet(c *gin.Context) { Tags: tagStrings, }) } - postPages := services.CountPostPages() - c.JSON(http.StatusOK, models.PostPaginationResponse{ - CurrentPage: page, - TotalPage: postPages, - Posts: postResult, - }) -} - -func postGetTag(c *gin.Context) { - pageParam := c.Query("page") - page, _ := strconv.Atoi(pageParam) - - tag := c.Param("id") - - posts := services.GetPostTag(page, tag) - var postResult []models.PostListItem - for _, post := range posts { - var tagStrings []string - for _, tag := range post.Tags { - tagStrings = append(tagStrings, tag.TagType.Name+":"+tag.Name) - } - - postResult = append(postResult, models.PostListItem{ - ID: post.ID, - ImagePath: "/data/" + post.Blob.FilePath, - Tags: tagStrings, - }) - } - postPages := services.CountPostPagesTag(tag) c.JSON(http.StatusOK, models.PostPaginationResponse{ CurrentPage: page, TotalPage: postPages, diff --git a/pkg/services/post.go b/pkg/services/post.go index 653b7e5..f8cc855 100644 --- a/pkg/services/post.go +++ b/pkg/services/post.go @@ -1,6 +1,9 @@ package services import ( + "log" + "math" + "github.com/Damillora/Shioriko/pkg/database" "github.com/Damillora/Shioriko/pkg/models" "github.com/google/uuid" @@ -10,17 +13,18 @@ const perPage = 20 func GetPostAll(page int) []database.Post { var posts []database.Post - database.DB.Joins("Blob").Preload("Tags").Preload("Tags.TagType").Offset((page - 1) * perPage).Limit(20).Find(&posts) + database.DB.Joins("Blob").Preload("Tags").Preload("Tags.TagType").Order("created_at desc").Offset((page - 1) * perPage).Limit(20).Find(&posts) return posts } -func GetPostTag(page int, tagSyntax string) []database.Post { - tag, err := GetTag(tagSyntax) +func GetPostTags(page int, tagSyntax []string) []database.Post { + tags, err := ParseReadTags(tagSyntax) + log.Println(tags) if err != nil { return []database.Post{} } var posts []database.Post - database.DB.Model(&tag).Joins("Blob").Preload("Tags").Preload("Tags.TagType").Offset((page - 1) * perPage).Limit(20).Association("Posts").Find(&posts) + database.DB.Model(&tags).Distinct().Joins("Blob").Preload("Tags").Preload("Tags.TagType").Order("created_at desc").Offset((page - 1) * perPage).Limit(20).Association("Posts").Find(&posts) return posts } @@ -59,34 +63,38 @@ func UpdatePost(id string, model models.PostUpdateModel) (*database.Post, error) } var post database.Post - result := database.DB.Where("id = ?", id).First(&post) + result := database.DB.Preload("Tags").Where("id = ?", id).First(&post) if result.Error != nil { return nil, result.Error } + + database.DB.Model(&post).Association("Tags").Replace(tags) + post.SourceURL = model.SourceURL - post.Tags = tags result = database.DB.Save(&post) if result.Error != nil { return nil, result.Error } + return &post, nil } func CountPostPages() int { var count int64 database.DB.Model(&database.Post{}).Count(&count) - return int(count/perPage) + 1 + return int(math.Abs(float64(count-1))/perPage) + 1 } -func CountPostPagesTag(tagSyntax string) int { - tag, err := GetTag(tagSyntax) +func CountPostPagesTag(tagSyntax []string) int { + tags, err := ParseReadTags(tagSyntax) if err != nil { return 0 } var count int64 - count = database.DB.Model(&tag).Joins("Blob").Preload("Tags").Preload("Tags.TagType").Association("Posts").Count() - return int(count/perPage) + 1 + count = database.DB.Model(&tags).Distinct().Joins("Blob").Preload("Tags").Preload("Tags.TagType").Association("Posts").Count() + + return int(math.Abs(float64(count-1))/perPage) + 1 } func DeletePost(id string) error { diff --git a/pkg/services/tag.go b/pkg/services/tag.go index ff75a0c..7a45171 100644 --- a/pkg/services/tag.go +++ b/pkg/services/tag.go @@ -14,27 +14,15 @@ func GetTagAll() []database.Tag { return tags } -func CreateOrUpdateTagGeneric(tagName string) (*database.Tag, error) { +func FindTagGeneric(tagName string) (*database.Tag, error) { var tag database.Tag result := database.DB.Where("name = ?", tagName).First(&tag) if result.Error != nil { - var tagType database.TagType - database.DB.Where("name = ?", "general").First(&tagType) - - tag = database.Tag{ - ID: uuid.NewString(), - Name: tagName, - TagTypeID: tagType.ID, - } - result = database.DB.Create(&tag) - if result.Error != nil { - return nil, result.Error - } - + return nil, result.Error } return &tag, nil } -func CreateOrUpdateTagComplex(tagName string, tagTypeString string) (*database.Tag, error) { +func FindTagComplex(tagName string, tagTypeString string) (*database.Tag, error) { var tag database.Tag var tagType database.TagType result := database.DB.Where("name = ?", tagTypeString).First(&tagType) @@ -45,7 +33,40 @@ func CreateOrUpdateTagComplex(tagName string, tagTypeString string) (*database.T result = database.DB.Where("name = ? AND tag_type_id = ? ", tagName, tagType.ID).First(&tag) if result.Error != nil { - tag = database.Tag{ + return nil, result.Error + } + return &tag, nil +} + +func CreateOrUpdateTagGeneric(tagName string) (*database.Tag, error) { + tag, err := FindTagGeneric(tagName) + if err != nil { + var tagType database.TagType + database.DB.Where("name = ?", "general").First(&tagType) + + tag = &database.Tag{ + ID: uuid.NewString(), + Name: tagName, + TagTypeID: tagType.ID, + } + result := database.DB.Create(&tag) + if result.Error != nil { + return nil, result.Error + } + + } + return tag, nil +} +func CreateOrUpdateTagComplex(tagName string, tagTypeString string) (*database.Tag, error) { + tag, err := FindTagComplex(tagName, tagTypeString) + + if err != nil { + var tagType database.TagType + result := database.DB.Where("name = ?", tagTypeString).First(&tagType) + if result.Error != nil { + return nil, result.Error + } + tag = &database.Tag{ ID: uuid.NewString(), Name: tagName, TagTypeID: tagType.ID, @@ -56,7 +77,7 @@ func CreateOrUpdateTagComplex(tagName string, tagTypeString string) (*database.T } } - return &tag, nil + return tag, nil } func CreateOrUpdateTag(tagSyntax string) (*database.Tag, error) { tagFields := strings.Split(tagSyntax, ":") @@ -73,30 +94,20 @@ func CreateOrUpdateTag(tagSyntax string) (*database.Tag, error) { return nil, errors.New("Malformed tag syntax") } } - -func GetTag(tagSyntax string) (*database.Tag, error) { +func FindTag(tagSyntax string) (*database.Tag, error) { tagFields := strings.Split(tagSyntax, ":") var tagName string - var tagType database.TagType + var tagType string if len(tagFields) == 1 { tagName = tagFields[0] - database.DB.Where("name = ?", "general").First(&tagType) + return FindTagGeneric(tagName) } else if len(tagFields) == 2 { + tagType = tagFields[0] tagName = tagFields[1] - result := database.DB.Where("name = ?", tagFields[0]).First(&tagType) - if result.Error != nil { - return nil, result.Error - } + return FindTagComplex(tagName, tagType) } else { return nil, errors.New("Malformed tag syntax") } - var tag database.Tag - result := database.DB.Preload("Posts").Where("name = ? AND tag_type_id = ? ", tagName, tagType.ID).First(&tag) - - if result.Error != nil { - return nil, result.Error - } - return &tag, nil } func ParseTags(tags []string) ([]database.Tag, error) { @@ -110,3 +121,15 @@ func ParseTags(tags []string) ([]database.Tag, error) { } return result, nil } + +func ParseReadTags(tags []string) ([]database.Tag, error) { + var result []database.Tag + for _, tagSyntax := range tags { + tag, err := FindTag(tagSyntax) + if err != nil { + return nil, err + } + result = append(result, *tag) + } + return result, nil +} diff --git a/web/app/src/App.svelte b/web/app/src/App.svelte index dbd3ce9..0ad7f0b 100644 --- a/web/app/src/App.svelte +++ b/web/app/src/App.svelte @@ -8,8 +8,8 @@ import Post from "./routes/Post.svelte"; import Login from "./routes/Login.svelte"; import Logout from "./routes/Logout.svelte"; - import Tag from "./routes/Tag.svelte"; import Upload from "./routes/Upload.svelte"; + import Edit from "./routes/Edit.svelte"; export let url = ""; let baseURL = window.BASE_URL; @@ -20,8 +20,8 @@