2023-12-03 13:31:29 +08:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2024-01-05 21:27:16 +08:00
|
|
|
"strings"
|
2023-12-03 13:31:29 +08:00
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *DB) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrganizer) (*store.MemoOrganizer, error) {
|
2023-12-10 10:07:26 +08:00
|
|
|
pinned := 0
|
2023-12-03 13:31:29 +08:00
|
|
|
if upsert.Pinned {
|
2023-12-10 10:07:26 +08:00
|
|
|
pinned = 1
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
stmt := `
|
|
|
|
INSERT INTO memo_organizer (
|
|
|
|
memo_id,
|
|
|
|
user_id,
|
|
|
|
pinned
|
|
|
|
)
|
|
|
|
VALUES (` + placeholders(3) + `)
|
|
|
|
ON CONFLICT(memo_id, user_id) DO UPDATE
|
|
|
|
SET pinned = EXCLUDED.pinned`
|
|
|
|
if _, err := d.db.ExecContext(ctx, stmt, upsert.MemoID, upsert.UserID, pinned); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
|
2023-12-03 13:31:29 +08:00
|
|
|
return upsert, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DB) ListMemoOrganizer(ctx context.Context, find *store.FindMemoOrganizer) ([]*store.MemoOrganizer, error) {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2023-12-03 13:31:29 +08:00
|
|
|
if find.MemoID != 0 {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args = append(where, "memo_id = "+placeholder(len(args)+1)), append(args, find.MemoID)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if find.UserID != 0 {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args = append(where, "user_id = "+placeholder(len(args)+1)), append(args, find.UserID)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
query := fmt.Sprintf(`
|
|
|
|
SELECT
|
|
|
|
memo_id,
|
|
|
|
user_id,
|
|
|
|
pinned
|
|
|
|
FROM memo_organizer
|
|
|
|
WHERE %s
|
|
|
|
`, strings.Join(where, " AND "))
|
2023-12-03 13:31:29 +08:00
|
|
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
list := []*store.MemoOrganizer{}
|
2023-12-03 13:31:29 +08:00
|
|
|
for rows.Next() {
|
|
|
|
memoOrganizer := &store.MemoOrganizer{}
|
2024-01-05 21:27:16 +08:00
|
|
|
pinned := 0
|
|
|
|
if err := rows.Scan(
|
|
|
|
&memoOrganizer.MemoID,
|
|
|
|
&memoOrganizer.UserID,
|
|
|
|
&pinned,
|
|
|
|
); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
|
|
|
|
memoOrganizer.Pinned = pinned == 1
|
2023-12-03 13:31:29 +08:00
|
|
|
list = append(list, memoOrganizer)
|
|
|
|
}
|
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DB) DeleteMemoOrganizer(ctx context.Context, delete *store.DeleteMemoOrganizer) error {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args := []string{}, []any{}
|
2023-12-03 13:31:29 +08:00
|
|
|
if v := delete.MemoID; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args = append(where, "memo_id = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if v := delete.UserID; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args = append(where, "user_id = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
stmt := `DELETE FROM memo_organizer WHERE ` + strings.Join(where, " AND ")
|
|
|
|
if _, err := d.db.ExecContext(ctx, stmt, args...); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vacuumMemoOrganizer(ctx context.Context, tx *sql.Tx) error {
|
2024-01-05 21:27:16 +08:00
|
|
|
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)
|
2023-12-03 13:31:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
return nil
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|