You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.3 KiB
129 lines
3.3 KiB
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"git.aiterp.net/rpdata/api/database/postgres/psqlcore"
|
|
"git.aiterp.net/rpdata/api/models"
|
|
)
|
|
|
|
type storyRepository struct {
|
|
insertWithIDs bool
|
|
db *sql.DB
|
|
}
|
|
|
|
func (r *storyRepository) Find(ctx context.Context, id string) (*models.Story, error) {
|
|
q := psqlcore.New(r.db)
|
|
story, err := q.SelectStory(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tags, err := q.SelectTagsByTarget(ctx, psqlcore.SelectTagsByTargetParams{
|
|
TargetKind: "Story",
|
|
TargetID: story.ID,
|
|
})
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return nil, err
|
|
}
|
|
|
|
return r.story(story, tags), nil
|
|
}
|
|
|
|
func (r *storyRepository) List(ctx context.Context, filter models.StoryFilter) ([]*models.Story, error) {
|
|
q := psqlcore.New(r.db)
|
|
params := psqlcore.SelectStoriesParams{LimitSize: 100}
|
|
|
|
if len(filter.Tags) > 0 {
|
|
params.FilterID = true
|
|
if len(filter.Tags) == 1 {
|
|
tags, err := q.SelectTagsByKindName(ctx, psqlcore.SelectTagsByKindNameParams{
|
|
Kind: string(filter.Tags[0].Kind),
|
|
Name: filter.Tags[0].Name,
|
|
})
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return nil, err
|
|
}
|
|
|
|
for _, tag := range tags {
|
|
params.Ids = append(params.Ids, tag.TargetID)
|
|
}
|
|
} else {
|
|
|
|
}
|
|
|
|
if len(params.Ids) == 0 {
|
|
return []*models.Story{}, nil
|
|
}
|
|
}
|
|
if filter.Limit > 0 {
|
|
params.LimitSize = 1000000
|
|
}
|
|
|
|
panic("implement me")
|
|
}
|
|
|
|
func (r *storyRepository) Insert(ctx context.Context, story models.Story) (*models.Story, error) {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (r *storyRepository) Update(ctx context.Context, story models.Story, update models.StoryUpdate) (*models.Story, error) {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (r *storyRepository) AddTag(ctx context.Context, story models.Story, tag models.Tag) error {
|
|
return psqlcore.New(r.db).SetTag(ctx, psqlcore.SetTagParams{
|
|
Kind: string(tag.Kind),
|
|
Name: tag.Name,
|
|
TargetKind: "Story",
|
|
TargetID: story.ID,
|
|
})
|
|
}
|
|
|
|
func (r *storyRepository) RemoveTag(ctx context.Context, story models.Story, tag models.Tag) error {
|
|
return psqlcore.New(r.db).ClearTag(ctx, psqlcore.ClearTagParams{
|
|
Kind: string(tag.Kind),
|
|
Name: tag.Name,
|
|
TargetKind: "Story",
|
|
TargetID: story.ID,
|
|
})
|
|
}
|
|
|
|
func (r *storyRepository) Delete(ctx context.Context, story models.Story) error {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (r *storyRepository) story(story psqlcore.Story, tags []psqlcore.CommonTag) *models.Story {
|
|
tags2 := make([]models.Tag, 0, 8)
|
|
for _, tag := range tags {
|
|
if tag.TargetKind == "Story" && tag.TargetID == story.ID {
|
|
tags2 = append(tags2, models.Tag{
|
|
Kind: models.TagKind(tag.Kind),
|
|
Name: tag.Name,
|
|
})
|
|
}
|
|
}
|
|
|
|
return &models.Story{
|
|
ID: story.ID,
|
|
Author: story.Author,
|
|
Name: story.Name,
|
|
Category: models.StoryCategory(story.Category),
|
|
Open: story.Open,
|
|
Listed: story.Listed,
|
|
Tags: tags2,
|
|
CreatedDate: story.CreatedDate,
|
|
FictionalDate: story.FictionalDate,
|
|
UpdatedDate: story.UpdatedDate,
|
|
SortByFictionalDate: story.SortByFictionalDate,
|
|
}
|
|
}
|
|
|
|
func (r *storyRepository) stories(stories []psqlcore.Story, tags []psqlcore.CommonTag) []*models.Story {
|
|
results := make([]*models.Story, 0, len(stories))
|
|
for _, story := range stories {
|
|
results = append(results, r.story(story, tags))
|
|
}
|
|
|
|
return results
|
|
}
|