memos/store/memo_organizer.go

198 lines
4 KiB
Go
Raw Normal View History

2022-05-19 18:32:04 +08:00
package store
import (
2022-08-07 10:17:12 +08:00
"context"
2022-05-22 00:59:22 +08:00
"database/sql"
2022-05-19 18:32:04 +08:00
"fmt"
2022-11-06 12:21:58 +08:00
"strings"
2022-06-27 22:09:06 +08:00
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
2022-05-19 18:32:04 +08:00
)
// memoOrganizerRaw is the store model for an MemoOrganizer.
// Fields have exactly the same meanings as MemoOrganizer.
type memoOrganizerRaw struct {
ID int
// Domain specific fields
MemoID int
UserID int
Pinned bool
}
func (raw *memoOrganizerRaw) toMemoOrganizer() *api.MemoOrganizer {
return &api.MemoOrganizer{
ID: raw.ID,
MemoID: raw.MemoID,
UserID: raw.UserID,
Pinned: raw.Pinned,
}
}
2022-08-07 10:17:12 +08:00
func (s *Store) FindMemoOrganizer(ctx context.Context, find *api.MemoOrganizerFind) (*api.MemoOrganizer, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.Rollback()
memoOrganizerRaw, err := findMemoOrganizer(ctx, tx, find)
2022-05-19 18:32:04 +08:00
if err != nil {
return nil, err
}
memoOrganizer := memoOrganizerRaw.toMemoOrganizer()
return memoOrganizer, nil
}
2022-08-07 10:17:12 +08:00
func (s *Store) UpsertMemoOrganizer(ctx context.Context, upsert *api.MemoOrganizerUpsert) error {
tx, err := s.db.BeginTx(ctx, nil)
2022-05-19 18:32:04 +08:00
if err != nil {
2022-08-07 10:17:12 +08:00
return FormatError(err)
}
defer tx.Rollback()
if err := upsertMemoOrganizer(ctx, tx, upsert); err != nil {
2022-05-19 18:32:04 +08:00
return err
}
2022-08-07 10:17:12 +08:00
if err := tx.Commit(); err != nil {
return FormatError(err)
}
2022-05-19 18:32:04 +08:00
return nil
}
2022-11-06 12:21:58 +08:00
func (s *Store) DeleteMemoOrganizer(ctx context.Context, delete *api.MemoOrganizerDelete) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return FormatError(err)
}
defer tx.Rollback()
if err := deleteMemoOrganizer(ctx, tx, delete); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return FormatError(err)
}
return nil
}
2022-08-07 10:17:12 +08:00
func findMemoOrganizer(ctx context.Context, tx *sql.Tx, find *api.MemoOrganizerFind) (*memoOrganizerRaw, error) {
query := `
2022-05-19 18:32:04 +08:00
SELECT
id,
memo_id,
user_id,
pinned
FROM memo_organizer
WHERE memo_id = ? AND user_id = ?
2022-08-07 10:17:12 +08:00
`
row, err := tx.QueryContext(ctx, query, find.MemoID, find.UserID)
2022-05-19 18:32:04 +08:00
if err != nil {
return nil, FormatError(err)
}
defer row.Close()
if !row.Next() {
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
}
var memoOrganizerRaw memoOrganizerRaw
if err := row.Scan(
&memoOrganizerRaw.ID,
&memoOrganizerRaw.MemoID,
&memoOrganizerRaw.UserID,
&memoOrganizerRaw.Pinned,
); err != nil {
return nil, FormatError(err)
}
2022-08-24 21:53:12 +08:00
if err := row.Err(); err != nil {
return nil, err
}
2022-05-19 18:32:04 +08:00
return &memoOrganizerRaw, nil
}
2022-08-07 10:17:12 +08:00
func upsertMemoOrganizer(ctx context.Context, tx *sql.Tx, upsert *api.MemoOrganizerUpsert) error {
query := `
2022-05-19 18:32:04 +08:00
INSERT INTO memo_organizer (
memo_id,
user_id,
pinned
)
VALUES (?, ?, ?)
ON CONFLICT(memo_id, user_id) DO UPDATE
SET
pinned = EXCLUDED.pinned
RETURNING id, memo_id, user_id, pinned
2022-08-07 10:17:12 +08:00
`
2022-05-19 18:32:04 +08:00
var memoOrganizer api.MemoOrganizer
2022-08-07 10:17:12 +08:00
if err := tx.QueryRowContext(ctx, query, upsert.MemoID, upsert.UserID, upsert.Pinned).Scan(
2022-05-19 18:32:04 +08:00
&memoOrganizer.ID,
&memoOrganizer.MemoID,
&memoOrganizer.UserID,
&memoOrganizer.Pinned,
); err != nil {
return FormatError(err)
}
return nil
}
2022-11-06 12:21:58 +08:00
func deleteMemoOrganizer(ctx context.Context, tx *sql.Tx, delete *api.MemoOrganizerDelete) error {
where, args := []string{}, []interface{}{}
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 ")
result, err := tx.ExecContext(ctx, stmt, args...)
if err != nil {
return FormatError(err)
}
rows, _ := result.RowsAffected()
if rows == 0 {
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("memo organizer not found")}
}
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
}