Gisle Aune
4 years ago
11 changed files with 264 additions and 43 deletions
-
16cmd/rpdata-restore/main.go
-
21database/postgres/chapters.go
-
113database/postgres/comments.go
-
2database/postgres/db.go
-
20database/postgres/migrations/20210327101320_create_table_comment.sql
-
9database/postgres/migrations/20210327101854_create_index_comment_chapter_id.sql
-
24database/postgres/queries/chapters.sql
-
45database/postgres/queries/characters.sql
-
34database/postgres/queries/comments.sql
-
5database/postgres/stories.go
-
18models/comment.go
@ -0,0 +1,113 @@ |
|||||
|
package postgres |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"database/sql" |
||||
|
"git.aiterp.net/rpdata/api/database/postgres/psqlcore" |
||||
|
"git.aiterp.net/rpdata/api/internal/generate" |
||||
|
"git.aiterp.net/rpdata/api/models" |
||||
|
) |
||||
|
|
||||
|
type commentRepository struct { |
||||
|
insertWithIDs bool |
||||
|
db *sql.DB |
||||
|
} |
||||
|
|
||||
|
func (r *commentRepository) Find(ctx context.Context, id string) (*models.Comment, error) { |
||||
|
comment, err := psqlcore.New(r.db).SelectComment(ctx, id) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
return r.comment(comment), nil |
||||
|
} |
||||
|
|
||||
|
func (r *commentRepository) List(ctx context.Context, filter models.CommentFilter) ([]*models.Comment, error) { |
||||
|
params := psqlcore.SelectCommentsParams{ |
||||
|
ChapterID: "", |
||||
|
LimitSize: 10000, |
||||
|
} |
||||
|
if filter.ChapterID != nil { |
||||
|
params.ChapterID = *filter.ChapterID |
||||
|
} |
||||
|
if filter.Limit > 0 { |
||||
|
params.LimitSize = int32(filter.Limit) |
||||
|
} |
||||
|
|
||||
|
comments, err := psqlcore.New(r.db).SelectComments(ctx, params) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
return r.comments(comments), nil |
||||
|
} |
||||
|
|
||||
|
func (r *commentRepository) Insert(ctx context.Context, comment models.Comment) (*models.Comment, error) { |
||||
|
if !r.insertWithIDs || len(comment.ID) < 8 { |
||||
|
comment.ID = generate.CommentID() |
||||
|
} |
||||
|
|
||||
|
err := psqlcore.New(r.db).InsertComment(ctx, psqlcore.InsertCommentParams{ |
||||
|
ID: comment.ID, |
||||
|
ChapterID: comment.ChapterID, |
||||
|
CharacterID: comment.CharacterID, |
||||
|
Subject: comment.Subject, |
||||
|
Author: comment.Author, |
||||
|
CharacterName: comment.CharacterID, |
||||
|
Source: comment.Source, |
||||
|
CreatedDate: comment.CreatedDate.UTC(), |
||||
|
FictionalDate: comment.FictionalDate.UTC(), |
||||
|
EditedDate: comment.EditedDate.UTC(), |
||||
|
}) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
return &comment, nil |
||||
|
} |
||||
|
|
||||
|
func (r *commentRepository) Update(ctx context.Context, comment models.Comment, update models.CommentUpdate) (*models.Comment, error) { |
||||
|
comment.ApplyUpdate(update) |
||||
|
|
||||
|
err := psqlcore.New(r.db).UpdateComment(ctx, psqlcore.UpdateCommentParams{ |
||||
|
Subject: comment.Subject, |
||||
|
Source: comment.Source, |
||||
|
FictionalDate: comment.FictionalDate.UTC(), |
||||
|
CharacterName: comment.ChapterID, |
||||
|
CharacterID: comment.CharacterID, |
||||
|
ID: comment.ID, |
||||
|
}) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
return &comment, nil |
||||
|
} |
||||
|
|
||||
|
func (r *commentRepository) Delete(ctx context.Context, comment models.Comment) error { |
||||
|
return psqlcore.New(r.db).DeleteComment(ctx, comment.ID) |
||||
|
} |
||||
|
|
||||
|
func (r *commentRepository) comment(comment psqlcore.StoryComment) *models.Comment { |
||||
|
return &models.Comment{ |
||||
|
ID: comment.ID, |
||||
|
ChapterID: comment.ChapterID, |
||||
|
Subject: comment.Subject, |
||||
|
Author: comment.Author, |
||||
|
CharacterName: comment.CharacterName, |
||||
|
CharacterID: comment.CharacterID, |
||||
|
FictionalDate: comment.FictionalDate, |
||||
|
CreatedDate: comment.CreatedDate, |
||||
|
EditedDate: comment.EditedDate, |
||||
|
Source: comment.Source, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func (r *commentRepository) comments(comments []psqlcore.StoryComment) []*models.Comment { |
||||
|
results := make([]*models.Comment, 0, len(comments)) |
||||
|
for _, comment := range comments { |
||||
|
results = append(results, r.comment(comment)) |
||||
|
} |
||||
|
|
||||
|
return results |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
-- +goose Up |
||||
|
-- +goose StatementBegin |
||||
|
CREATE TABLE story_comment ( |
||||
|
id TEXT NOT NULL PRIMARY KEY, |
||||
|
chapter_id TEXT NOT NULL, |
||||
|
character_id TEXT NOT NULL, |
||||
|
subject TEXT NOT NULL, |
||||
|
author TEXT NOT NULL, |
||||
|
character_name TEXT NOT NULL, |
||||
|
source TEXT NOT NULL, |
||||
|
created_date TIMESTAMP NOT NULL, |
||||
|
fictional_date TIMESTAMP NOT NULL, |
||||
|
edited_date TIMESTAMP NOT NULL |
||||
|
); |
||||
|
-- +goose StatementEnd |
||||
|
|
||||
|
-- +goose Down |
||||
|
-- +goose StatementBegin |
||||
|
DROP TABLE story_comment; |
||||
|
-- +goose StatementEnd |
@ -0,0 +1,9 @@ |
|||||
|
-- +goose Up |
||||
|
-- +goose StatementBegin |
||||
|
CREATE INDEX story_comment_index_chapter_id ON story_comment (chapter_id); |
||||
|
-- +goose StatementEnd |
||||
|
|
||||
|
-- +goose Down |
||||
|
-- +goose StatementBegin |
||||
|
DROP INDEX IF EXISTS story_comment_index_chapter_id; |
||||
|
-- +goose StatementEnd |
@ -1,50 +1,47 @@ |
|||||
-- name: SelectCharacterByID :one |
-- name: SelectCharacterByID :one |
||||
SELECT * FROM data_character WHERE id = sqlc.arg(id)::text; |
|
||||
|
SELECT * FROM data_character WHERE id = @id::text; |
||||
|
|
||||
-- name: SelectCharacterByNick :one |
-- name: SelectCharacterByNick :one |
||||
SELECT * FROM data_character WHERE nicks <@ ARRAY[sqlc.arg(nick)::text]; |
|
||||
|
SELECT * FROM data_character WHERE nicks <@ ARRAY[@nick::text]; |
||||
|
|
||||
-- name: SelectCharacterByName :one |
-- name: SelectCharacterByName :one |
||||
SELECT * FROM data_character WHERE name = sqlc.arg(name)::text; |
|
||||
|
SELECT * FROM data_character WHERE name = @name::text; |
||||
|
|
||||
-- name: SelectCharacters :many |
-- name: SelectCharacters :many |
||||
SELECT * FROM data_character |
SELECT * FROM data_character |
||||
WHERE (sqlc.arg(filter_id)::bool = false OR id = ANY(sqlc.arg(ids)::text[])) |
|
||||
AND (sqlc.arg(filter_name)::bool = false OR name = ANY(sqlc.arg(names)::text[])) |
|
||||
AND (sqlc.arg(filter_nick)::bool = false OR nicks && (sqlc.arg(nicks)::text[])) |
|
||||
AND (sqlc.arg(filter_author)::bool = false OR author = sqlc.arg(author)::text) |
|
||||
AND (sqlc.arg(filter_search)::bool = false OR "ts_vector" @@ to_tsquery(sqlc.arg(search)::text)) |
|
||||
LIMIT sqlc.arg(limit_size)::int; |
|
||||
|
WHERE (@filter_id::bool = false OR id = ANY(@ids::text[])) |
||||
|
AND (@filter_name::bool = false OR name = ANY(@names::text[])) |
||||
|
AND (@filter_nick::bool = false OR nicks && (@nicks::text[])) |
||||
|
AND (@filter_author::bool = false OR author = @author::text) |
||||
|
AND (@filter_search::bool = false OR "ts_vector" @@ to_tsquery(@search::text)) |
||||
|
LIMIT @limit_size::int; |
||||
|
|
||||
-- name: InsertCharacter :exec |
-- name: InsertCharacter :exec |
||||
INSERT INTO data_character (id, nicks, name, short_name, author, description, ts_vector) VALUES ( |
INSERT INTO data_character (id, nicks, name, short_name, author, description, ts_vector) VALUES ( |
||||
sqlc.arg(id)::text, sqlc.arg(nicks)::text[], sqlc.arg(name)::text, |
|
||||
sqlc.arg(short_name)::text, sqlc.arg(author)::text, sqlc.arg(description)::text, |
|
||||
|
@id::text, @nicks::text[], @name::text, |
||||
|
@short_name::text, @author::text, @description::text, |
||||
to_tsvector( |
to_tsvector( |
||||
'english', |
|
||||
sqlc.arg(name)::text || ' ' || sqlc.arg(description)::text || ' ' || sqlc.arg(author)::text || ' ' |
|
||||
|| immutable_array_to_string( |
|
||||
sqlc.arg(nicks)::text[], ' ' |
|
||||
) |
|
||||
|
'english', @name::text || ' ' || @description::text || ' ' || @author::text || ' ' |
||||
|
|| immutable_array_to_string(@nicks::text[], ' ') |
||||
) |
) |
||||
); |
); |
||||
|
|
||||
-- name: UpdateCharacter :exec |
-- name: UpdateCharacter :exec |
||||
UPDATE data_character |
UPDATE data_character |
||||
SET name=sqlc.arg(name)::text, |
|
||||
short_name=sqlc.arg(short_name)::text, |
|
||||
description=sqlc.arg(description)::text |
|
||||
WHERE id=sqlc.arg(id)::text; |
|
||||
|
SET name = @name::text, |
||||
|
short_name = @short_name::text, |
||||
|
description = @description::text |
||||
|
WHERE id = @id::text; |
||||
|
|
||||
-- name: AddCharacterNick :exec |
-- name: AddCharacterNick :exec |
||||
UPDATE data_character |
UPDATE data_character |
||||
SET nicks=append(nicks, sqlc.arg(nick)::text) |
|
||||
WHERE id=sqlc.arg(id)::text; |
|
||||
|
SET nicks=array_append(nicks, @nick::text) |
||||
|
WHERE id = @id::text; |
||||
|
|
||||
-- name: RemoveCharacterNick :exec |
-- name: RemoveCharacterNick :exec |
||||
UPDATE data_character |
UPDATE data_character |
||||
SET nicks=array_remove(nicks, sqlc.arg(nick)::text) |
|
||||
WHERE id=sqlc.arg(id)::text; |
|
||||
|
SET nicks=array_remove(nicks, @nick::text) |
||||
|
WHERE id = @id::text; |
||||
|
|
||||
-- name: DeleteCharacter :exec |
-- name: DeleteCharacter :exec |
||||
DELETE FROM data_character WHERE id=$1; |
DELETE FROM data_character WHERE id=$1; |
@ -0,0 +1,34 @@ |
|||||
|
-- name: SelectComment :one |
||||
|
SELECT * FROM story_comment WHERE id=$1; |
||||
|
|
||||
|
-- name: InsertComment :exec |
||||
|
INSERT INTO story_comment (id, chapter_id, character_id, subject, author, character_name, source, created_date, fictional_date, edited_date) |
||||
|
VALUES ( |
||||
|
@id, @chapter_id, @character_id, |
||||
|
@subject, @author, @character_name, @source, |
||||
|
@created_date, @fictional_date, @edited_date |
||||
|
); |
||||
|
|
||||
|
-- name: UpdateComment :exec |
||||
|
UPDATE story_comment |
||||
|
SET subject = @subject, |
||||
|
source = @source, |
||||
|
fictional_date = @fictional_date, |
||||
|
character_name = @character_name, |
||||
|
character_id = @character_id |
||||
|
WHERE id = @id; |
||||
|
|
||||
|
-- name: SelectComments :many |
||||
|
SELECT * FROM story_comment WHERE (@chapter_id = '' OR chapter_id = @chapter_id) LIMIT @limit_size; |
||||
|
|
||||
|
-- name: DeleteComment :exec |
||||
|
DELETE FROM story_comment WHERE id = $1; |
||||
|
|
||||
|
-- name: DeleteCommentsByChapterID :exec |
||||
|
DELETE FROM story_comment WHERE chapter_id = $1; |
||||
|
|
||||
|
-- name: DeleteCommentsByStoryID :exec |
||||
|
DELETE FROM story_comment |
||||
|
WHERE chapter_id IN ( |
||||
|
SELECT id FROM story_chapter WHERE story_id = $1 |
||||
|
); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue