2021-05-09 15:07:23 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2021-05-10 20:25:33 +00:00
|
|
|
"log"
|
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
"github.com/Damillora/Shioriko/pkg/database"
|
|
|
|
"github.com/Damillora/Shioriko/pkg/models"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
const perPage = 20
|
|
|
|
|
|
|
|
func GetPostAll(page int) []database.Post {
|
|
|
|
var posts []database.Post
|
2021-05-10 20:25:33 +00:00
|
|
|
database.DB.Joins("Blob").Preload("Tags").Preload("Tags.TagType").Order("created_at desc").Offset((page - 1) * perPage).Limit(20).Find(&posts)
|
2021-05-09 15:07:23 +00:00
|
|
|
return posts
|
|
|
|
}
|
|
|
|
|
2021-05-10 20:25:33 +00:00
|
|
|
func GetPostTags(page int, tagSyntax []string) []database.Post {
|
|
|
|
tags, err := ParseReadTags(tagSyntax)
|
|
|
|
log.Println(tags)
|
2021-05-09 15:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return []database.Post{}
|
|
|
|
}
|
|
|
|
var posts []database.Post
|
2021-05-10 20:25:33 +00:00
|
|
|
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)
|
2021-05-09 15:07:23 +00:00
|
|
|
return posts
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPost(id string) (*database.Post, error) {
|
|
|
|
var post database.Post
|
2021-05-11 10:50:34 +00:00
|
|
|
result := database.DB.Joins("User").Joins("Blob").Preload("Tags").Preload("Tags.TagType").Where("posts.id = ?", id).First(&post)
|
2021-05-09 15:07:23 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return &post, nil
|
|
|
|
}
|
2021-05-11 10:50:34 +00:00
|
|
|
func CreatePost(userID string, model models.PostCreateModel) (*database.Post, error) {
|
2021-05-09 15:07:23 +00:00
|
|
|
tags, err := ParseTags(model.Tags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-11 11:00:05 +00:00
|
|
|
if len(tags) == 0 {
|
|
|
|
database.DB.Where("name = ?", "tagme").Find(&tags)
|
|
|
|
}
|
2021-05-09 15:07:23 +00:00
|
|
|
post := database.Post{
|
|
|
|
ID: uuid.NewString(),
|
2021-05-11 10:50:34 +00:00
|
|
|
UserID: userID,
|
2021-05-09 15:07:23 +00:00
|
|
|
BlobID: model.BlobID,
|
|
|
|
SourceURL: model.SourceURL,
|
|
|
|
Tags: tags,
|
|
|
|
}
|
|
|
|
|
|
|
|
result := database.DB.Create(&post)
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
return &post, nil
|
|
|
|
}
|
|
|
|
func UpdatePost(id string, model models.PostUpdateModel) (*database.Post, error) {
|
|
|
|
tags, err := ParseTags(model.Tags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-11 11:00:05 +00:00
|
|
|
if len(tags) == 0 {
|
|
|
|
database.DB.Where("name = ?", "tagme").Find(&tags)
|
|
|
|
}
|
2021-05-09 15:07:23 +00:00
|
|
|
|
|
|
|
var post database.Post
|
2021-05-10 20:25:33 +00:00
|
|
|
result := database.DB.Preload("Tags").Where("id = ?", id).First(&post)
|
2021-05-09 15:07:23 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
2021-05-10 20:25:33 +00:00
|
|
|
|
|
|
|
database.DB.Model(&post).Association("Tags").Replace(tags)
|
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
post.SourceURL = model.SourceURL
|
|
|
|
|
|
|
|
result = database.DB.Save(&post)
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
2021-05-10 20:25:33 +00:00
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
return &post, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
func CountPostPages() int {
|
|
|
|
var count int64
|
|
|
|
database.DB.Model(&database.Post{}).Count(&count)
|
2021-05-11 10:06:25 +00:00
|
|
|
return int(count)
|
2021-05-09 15:07:23 +00:00
|
|
|
}
|
2021-05-10 20:25:33 +00:00
|
|
|
func CountPostPagesTag(tagSyntax []string) int {
|
|
|
|
tags, err := ParseReadTags(tagSyntax)
|
2021-05-10 15:47:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
2021-05-10 20:25:33 +00:00
|
|
|
count = database.DB.Model(&tags).Distinct().Joins("Blob").Preload("Tags").Preload("Tags.TagType").Association("Posts").Count()
|
2021-05-11 10:06:25 +00:00
|
|
|
return int(count)
|
2021-05-10 15:47:35 +00:00
|
|
|
}
|
2021-05-09 15:07:23 +00:00
|
|
|
|
|
|
|
func DeletePost(id string) error {
|
|
|
|
|
|
|
|
var post database.Post
|
|
|
|
result := database.DB.Where("id = ?", id).First(&post)
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
result = database.DB.Where("id = ?", post.BlobID).Delete(&database.Blob{})
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
result = database.DB.Delete(&post)
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|