2023-04-25 18:27:38 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MemoRelationType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
MemoRelationReference MemoRelationType = "REFERENCE"
|
|
|
|
MemoRelationAdditional MemoRelationType = "ADDITIONAL"
|
|
|
|
)
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
type MemoRelation struct {
|
2023-04-25 18:27:38 +03:00
|
|
|
MemoID int
|
|
|
|
RelatedMemoID int
|
|
|
|
Type MemoRelationType
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
type FindMemoRelation struct {
|
2023-04-25 18:27:38 +03:00
|
|
|
MemoID *int
|
|
|
|
RelatedMemoID *int
|
|
|
|
Type *MemoRelationType
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
type DeleteMemoRelation struct {
|
2023-04-25 18:27:38 +03:00
|
|
|
MemoID *int
|
|
|
|
RelatedMemoID *int
|
|
|
|
Type *MemoRelationType
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func (s *Store) UpsertMemoRelation(ctx context.Context, create *MemoRelation) (*MemoRelation, error) {
|
2023-04-25 18:27:38 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
query := `
|
|
|
|
INSERT INTO memo_relation (
|
|
|
|
memo_id,
|
|
|
|
related_memo_id,
|
|
|
|
type
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
ON CONFLICT (memo_id, related_memo_id, type) DO UPDATE SET
|
|
|
|
type = EXCLUDED.type
|
|
|
|
RETURNING memo_id, related_memo_id, type
|
|
|
|
`
|
2023-07-06 17:53:38 +03:00
|
|
|
memoRelation := &MemoRelation{}
|
2023-04-25 18:27:38 +03:00
|
|
|
if err := tx.QueryRowContext(
|
|
|
|
ctx,
|
|
|
|
query,
|
|
|
|
create.MemoID,
|
|
|
|
create.RelatedMemoID,
|
|
|
|
create.Type,
|
|
|
|
).Scan(
|
2023-07-06 17:53:38 +03:00
|
|
|
&memoRelation.MemoID,
|
|
|
|
&memoRelation.RelatedMemoID,
|
|
|
|
&memoRelation.Type,
|
2023-04-25 18:27:38 +03:00
|
|
|
); err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
2023-07-06 17:53:38 +03:00
|
|
|
|
2023-04-25 18:27:38 +03:00
|
|
|
if err := tx.Commit(); err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
2023-07-06 17:53:38 +03:00
|
|
|
|
|
|
|
return memoRelation, nil
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func (s *Store) ListMemoRelations(ctx context.Context, find *FindMemoRelation) ([]*MemoRelation, error) {
|
2023-04-25 18:27:38 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := listMemoRelations(ctx, tx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-25 18:27:38 +03:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func (s *Store) GetMemoRelation(ctx context.Context, find *FindMemoRelation) (*MemoRelation, error) {
|
2023-04-25 18:27:38 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := listMemoRelations(ctx, tx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
2023-04-25 18:27:38 +03:00
|
|
|
return list[0], nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func (s *Store) DeleteMemoRelation(ctx context.Context, delete *DeleteMemoRelation) error {
|
2023-04-25 18:27:38 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
where, args := []string{"TRUE"}, []any{}
|
|
|
|
if delete.MemoID != nil {
|
|
|
|
where, args = append(where, "memo_id = ?"), append(args, delete.MemoID)
|
|
|
|
}
|
|
|
|
if delete.RelatedMemoID != nil {
|
|
|
|
where, args = append(where, "related_memo_id = ?"), append(args, delete.RelatedMemoID)
|
|
|
|
}
|
|
|
|
if delete.Type != nil {
|
|
|
|
where, args = append(where, "type = ?"), append(args, delete.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := `
|
|
|
|
DELETE FROM memo_relation
|
|
|
|
WHERE ` + strings.Join(where, " AND ")
|
|
|
|
if _, err := tx.ExecContext(ctx, query, args...); err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
// Prevent lint warning.
|
|
|
|
return err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func listMemoRelations(ctx context.Context, tx *sql.Tx, find *FindMemoRelation) ([]*MemoRelation, error) {
|
2023-04-25 18:27:38 +03:00
|
|
|
where, args := []string{"TRUE"}, []any{}
|
|
|
|
if find.MemoID != nil {
|
|
|
|
where, args = append(where, "memo_id = ?"), append(args, find.MemoID)
|
|
|
|
}
|
|
|
|
if find.RelatedMemoID != nil {
|
|
|
|
where, args = append(where, "related_memo_id = ?"), append(args, find.RelatedMemoID)
|
|
|
|
}
|
|
|
|
if find.Type != nil {
|
|
|
|
where, args = append(where, "type = ?"), append(args, find.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := tx.QueryContext(ctx, `
|
|
|
|
SELECT
|
|
|
|
memo_id,
|
|
|
|
related_memo_id,
|
|
|
|
type
|
|
|
|
FROM memo_relation
|
|
|
|
WHERE `+strings.Join(where, " AND "), args...)
|
|
|
|
if err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
memoRelationMessages := []*MemoRelation{}
|
2023-04-25 18:27:38 +03:00
|
|
|
for rows.Next() {
|
2023-07-06 16:56:42 +03:00
|
|
|
memoRelationMessage := &MemoRelation{}
|
2023-04-25 18:27:38 +03:00
|
|
|
if err := rows.Scan(
|
|
|
|
&memoRelationMessage.MemoID,
|
|
|
|
&memoRelationMessage.RelatedMemoID,
|
|
|
|
&memoRelationMessage.Type,
|
|
|
|
); err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
memoRelationMessages = append(memoRelationMessages, memoRelationMessage)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
return memoRelationMessages, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vacuumMemoRelations(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
if _, err := tx.ExecContext(ctx, `
|
|
|
|
DELETE FROM memo_relation
|
|
|
|
WHERE memo_id NOT IN (SELECT id FROM memo) OR related_memo_id NOT IN (SELECT id FROM memo)
|
|
|
|
`); err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|