memos/store/memo_organizer.go

147 lines
2.7 KiB
Go
Raw Normal View History

2022-05-19 13:32:04 +03:00
package store
import (
2022-08-07 05:17:12 +03:00
"context"
2022-05-21 19:59:22 +03:00
"database/sql"
2022-05-19 13:32:04 +03:00
"fmt"
2022-11-06 07:21:58 +03:00
"strings"
2022-05-19 13:32:04 +03:00
)
type MemoOrganizer struct {
2022-05-19 13:32:04 +03:00
MemoID int
UserID int
Pinned bool
}
type FindMemoOrganizer struct {
MemoID int
UserID int
2022-05-19 13:32:04 +03:00
}
type DeleteMemoOrganizer struct {
MemoID *int
UserID *int
2022-05-19 13:32:04 +03:00
}
func (s *Store) UpsertMemoOrganizerV1(ctx context.Context, upsert *MemoOrganizer) (*MemoOrganizer, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
2022-05-19 13:32:04 +03:00
if err != nil {
return nil, err
2022-08-07 05:17:12 +03:00
}
defer tx.Rollback()
query := `
INSERT INTO memo_organizer (
memo_id,
user_id,
pinned
)
VALUES (?, ?, ?)
ON CONFLICT(memo_id, user_id) DO UPDATE
SET
pinned = EXCLUDED.pinned
`
if _, err := tx.ExecContext(ctx, query, upsert.MemoID, upsert.UserID, upsert.Pinned); err != nil {
return nil, err
2022-05-19 13:32:04 +03:00
}
2022-08-07 05:17:12 +03:00
if err := tx.Commit(); err != nil {
return nil, err
2022-08-07 05:17:12 +03:00
}
memoOrganizer := upsert
return memoOrganizer, nil
2022-05-19 13:32:04 +03:00
}
func (s *Store) GetMemoOrganizerV1(ctx context.Context, find *FindMemoOrganizer) (*MemoOrganizer, error) {
2022-11-06 07:21:58 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
2022-11-06 07:21:58 +03:00
}
defer tx.Rollback()
where, args := []string{}, []any{}
if find.MemoID != 0 {
where = append(where, "memo_id = ?")
args = append(args, find.MemoID)
2022-11-06 07:21:58 +03:00
}
if find.UserID != 0 {
where = append(where, "user_id = ?")
args = append(args, find.UserID)
2022-11-06 07:21:58 +03:00
}
query := fmt.Sprintf(`
2022-05-19 13:32:04 +03:00
SELECT
memo_id,
user_id,
pinned
FROM memo_organizer
WHERE %s
`, strings.Join(where, " AND "))
row := tx.QueryRowContext(ctx, query, args...)
2022-05-19 13:32:04 +03:00
memoOrganizer := &MemoOrganizer{}
2022-05-19 13:32:04 +03:00
if err := row.Scan(
&memoOrganizer.MemoID,
&memoOrganizer.UserID,
&memoOrganizer.Pinned,
2022-05-19 13:32:04 +03:00
); err != nil {
2022-08-24 16:53:12 +03:00
return nil, err
}
return memoOrganizer, nil
2022-05-19 13:32:04 +03:00
}
func (s *Store) DeleteMemoOrganizerV1(ctx context.Context, delete *DeleteMemoOrganizer) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
2022-05-19 13:32:04 +03:00
return FormatError(err)
}
defer tx.Rollback()
2022-05-19 13:32:04 +03:00
where, args := []string{}, []any{}
2022-11-06 07:21:58 +03:00
if v := delete.MemoID; v != nil {
where, args = append(where, "memo_id = ?"), append(args, *v)
}
if v := delete.UserID; v != nil {
where, args = append(where, "user_id = ?"), append(args, *v)
}
stmt := `DELETE FROM memo_organizer WHERE ` + strings.Join(where, " AND ")
_, err = tx.ExecContext(ctx, stmt, args...)
2022-11-06 07:21:58 +03:00
if err != nil {
return FormatError(err)
}
if err := tx.Commit(); err != nil {
return FormatError(err)
2022-11-06 07:21:58 +03:00
}
return nil
}
func vacuumMemoOrganizer(ctx context.Context, tx *sql.Tx) error {
stmt := `
DELETE FROM
memo_organizer
WHERE
memo_id NOT IN (
SELECT
id
FROM
memo
)
OR user_id NOT IN (
SELECT
id
FROM
user
)`
_, err := tx.ExecContext(ctx, stmt)
if err != nil {
return FormatError(err)
}
return nil
}