2021-05-09 15:07:23 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2022-04-16 10:41:50 +00:00
|
|
|
"fmt"
|
2022-04-16 12:23:19 +00:00
|
|
|
"strings"
|
2022-04-16 10:41:50 +00:00
|
|
|
|
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 {
|
2022-04-16 12:23:19 +00:00
|
|
|
positiveTagSyntax := []string{}
|
|
|
|
negativeTagSyntax := []string{}
|
2021-05-11 18:22:46 +00:00
|
|
|
|
2022-04-16 12:23:19 +00:00
|
|
|
for _, tag := range tagSyntax {
|
|
|
|
if strings.HasPrefix(tag, "-") {
|
|
|
|
negativeTagSyntax = append(negativeTagSyntax, strings.TrimPrefix(tag, "-"))
|
|
|
|
} else {
|
|
|
|
positiveTagSyntax = append(positiveTagSyntax, tag)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
positiveTags, err := ParseReadTags(positiveTagSyntax)
|
|
|
|
if err != nil {
|
|
|
|
return []database.Post{}
|
|
|
|
}
|
|
|
|
negativeTags, err := ParseReadTags(negativeTagSyntax)
|
2021-05-09 15:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return []database.Post{}
|
|
|
|
}
|
2022-04-16 10:41:50 +00:00
|
|
|
|
2022-04-16 12:23:19 +00:00
|
|
|
var positiveTagIds []string
|
|
|
|
for _, tag := range positiveTags {
|
|
|
|
positiveTagIds = append(positiveTagIds, tag.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var negativeTagIds []string
|
|
|
|
for _, tag := range negativeTags {
|
|
|
|
negativeTagIds = append(negativeTagIds, tag.ID)
|
2022-04-16 10:41:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 12:23:19 +00:00
|
|
|
var positivePostIds []string
|
2022-04-16 10:41:50 +00:00
|
|
|
database.DB.
|
2022-04-16 12:23:19 +00:00
|
|
|
Model(&positiveTags).
|
2022-04-16 10:41:50 +00:00
|
|
|
Joins("join post_tags on post_tags.tag_id = tags.id").
|
|
|
|
Select("post_tags.post_id").
|
2022-04-16 12:23:19 +00:00
|
|
|
Where("post_tags.tag_id IN ?", positiveTagIds).
|
2022-04-16 10:41:50 +00:00
|
|
|
Group("post_tags.post_id").
|
2022-04-16 12:23:19 +00:00
|
|
|
Having("count(*) = ?", len(positiveTagIds)).
|
2022-04-16 10:41:50 +00:00
|
|
|
Distinct().
|
2022-04-16 12:23:19 +00:00
|
|
|
Find(&positivePostIds)
|
|
|
|
var negativePostIds []string
|
|
|
|
database.DB.
|
|
|
|
Model(&positiveTags).
|
|
|
|
Joins("join post_tags on post_tags.tag_id = tags.id").
|
|
|
|
Select("post_tags.post_id").
|
|
|
|
Where("post_tags.tag_id IN ?", negativeTagIds).
|
|
|
|
Group("post_tags.post_id").
|
|
|
|
Having("count(*) = ?", len(negativeTagIds)).
|
|
|
|
Distinct().
|
|
|
|
Find(&negativePostIds)
|
2022-04-16 10:41:50 +00:00
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
var posts []database.Post
|
2022-04-16 12:23:19 +00:00
|
|
|
query := database.DB.
|
2022-04-16 10:41:50 +00:00
|
|
|
Joins("Blob").
|
|
|
|
Preload("Tags").
|
2022-04-16 12:23:19 +00:00
|
|
|
Preload("Tags.TagType")
|
|
|
|
|
|
|
|
if len(positivePostIds) > 0 && len(negativePostIds) > 0 {
|
|
|
|
query = query.
|
|
|
|
Where("posts.id IN ? AND posts.id NOT IN ?", positivePostIds, negativePostIds)
|
|
|
|
} else if len(positivePostIds) > 0 {
|
|
|
|
query = query.
|
|
|
|
Where("posts.id IN ?", positivePostIds)
|
|
|
|
} else if len(negativePostIds) > 0 {
|
|
|
|
query = query.
|
|
|
|
Where("posts.id NOT IN ?", negativePostIds)
|
|
|
|
}
|
|
|
|
query.Order("created_at desc").
|
2022-04-16 10:41:50 +00:00
|
|
|
Offset((page - 1) * perPage).
|
|
|
|
Limit(20).
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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-11 11:06:48 +00:00
|
|
|
err = database.DB.Model(&post).Association("Tags").Replace(tags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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 {
|
2022-04-16 12:23:19 +00:00
|
|
|
positiveTagSyntax := []string{}
|
|
|
|
negativeTagSyntax := []string{}
|
|
|
|
|
|
|
|
for _, tag := range tagSyntax {
|
|
|
|
if strings.HasPrefix(tag, "-") {
|
|
|
|
negativeTagSyntax = append(negativeTagSyntax, strings.TrimPrefix(tag, "-"))
|
|
|
|
} else {
|
|
|
|
positiveTagSyntax = append(positiveTagSyntax, tag)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
positiveTags, err := ParseReadTags(positiveTagSyntax)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
negativeTags, err := ParseReadTags(negativeTagSyntax)
|
2021-05-10 15:47:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-04-16 12:23:19 +00:00
|
|
|
var positiveTagIds []string
|
|
|
|
for _, tag := range positiveTags {
|
|
|
|
positiveTagIds = append(positiveTagIds, tag.ID)
|
2022-04-16 10:41:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 12:23:19 +00:00
|
|
|
var negativeTagIds []string
|
|
|
|
for _, tag := range negativeTags {
|
|
|
|
negativeTagIds = append(negativeTagIds, tag.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var positivePostIds []string
|
2022-04-16 10:41:50 +00:00
|
|
|
database.DB.
|
2022-04-16 12:23:19 +00:00
|
|
|
Model(&positiveTags).
|
|
|
|
Joins("join post_tags on post_tags.tag_id = tags.id").
|
|
|
|
Select("post_tags.post_id").
|
|
|
|
Where("post_tags.tag_id IN ?", positiveTagIds).
|
|
|
|
Group("post_tags.post_id").
|
|
|
|
Having("count(*) = ?", len(positiveTagIds)).
|
2022-04-16 10:41:50 +00:00
|
|
|
Distinct().
|
2022-04-16 12:23:19 +00:00
|
|
|
Find(&positivePostIds)
|
|
|
|
var negativePostIds []string
|
|
|
|
database.DB.
|
|
|
|
Model(&positiveTags).
|
2022-04-16 10:41:50 +00:00
|
|
|
Joins("join post_tags on post_tags.tag_id = tags.id").
|
|
|
|
Select("post_tags.post_id").
|
2022-04-16 12:23:19 +00:00
|
|
|
Where("post_tags.tag_id IN ?", negativeTagIds).
|
2022-04-16 10:41:50 +00:00
|
|
|
Group("post_tags.post_id").
|
2022-04-16 12:23:19 +00:00
|
|
|
Having("count(*) = ?", len(negativeTagIds)).
|
|
|
|
Distinct().
|
|
|
|
Find(&negativePostIds)
|
2022-04-16 10:41:50 +00:00
|
|
|
|
2022-04-16 12:23:19 +00:00
|
|
|
var count int64
|
|
|
|
query := database.DB.
|
|
|
|
Model(&database.Post{})
|
|
|
|
if len(positivePostIds) > 0 && len(negativePostIds) > 0 {
|
|
|
|
query = query.
|
|
|
|
Where("posts.id IN ? AND posts.id NOT IN ?", positivePostIds, negativePostIds)
|
|
|
|
} else if len(positivePostIds) > 0 {
|
|
|
|
query = query.
|
|
|
|
Where("posts.id IN ?", positivePostIds)
|
|
|
|
} else if len(negativePostIds) > 0 {
|
|
|
|
query = query.
|
|
|
|
Where("posts.id NOT IN ?", negativePostIds)
|
|
|
|
}
|
|
|
|
query.
|
|
|
|
Count(&count)
|
|
|
|
fmt.Println(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
|
|
|
|
|
|
|
|
}
|