2023-09-28 17:09:52 +03:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrganizer) (*store.MemoOrganizer, error) {
|
2023-10-07 17:56:12 +03:00
|
|
|
stmt := "INSERT INTO `memo_organizer` (`memo_id`, `user_id`, `pinned`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `pinned` = ?"
|
2023-09-28 17:09:52 +03:00
|
|
|
if _, err := d.db.ExecContext(ctx, stmt, upsert.MemoID, upsert.UserID, upsert.Pinned, upsert.Pinned); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return upsert, nil
|
|
|
|
}
|
|
|
|
|
2023-10-09 16:18:47 +03:00
|
|
|
func (d *DB) ListMemoOrganizer(ctx context.Context, find *store.FindMemoOrganizer) ([]*store.MemoOrganizer, error) {
|
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2023-09-28 17:09:52 +03:00
|
|
|
if find.MemoID != 0 {
|
2023-10-07 17:56:12 +03:00
|
|
|
where = append(where, "`memo_id` = ?")
|
2023-09-28 17:09:52 +03:00
|
|
|
args = append(args, find.MemoID)
|
|
|
|
}
|
|
|
|
if find.UserID != 0 {
|
2023-10-07 17:56:12 +03:00
|
|
|
where = append(where, "`user_id` = ?")
|
2023-09-28 17:09:52 +03:00
|
|
|
args = append(args, find.UserID)
|
|
|
|
}
|
|
|
|
|
2023-10-07 17:56:12 +03:00
|
|
|
query := "SELECT `memo_id`, `user_id`, `pinned` FROM `memo_organizer` WHERE " + strings.Join(where, " AND ")
|
2023-10-09 16:18:47 +03:00
|
|
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
2023-09-28 17:09:52 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-09 16:18:47 +03:00
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
list := []*store.MemoOrganizer{}
|
|
|
|
for rows.Next() {
|
|
|
|
memoOrganizer := &store.MemoOrganizer{}
|
|
|
|
if err := rows.Scan(
|
|
|
|
&memoOrganizer.MemoID,
|
|
|
|
&memoOrganizer.UserID,
|
|
|
|
&memoOrganizer.Pinned,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, memoOrganizer)
|
2023-09-28 17:09:52 +03:00
|
|
|
}
|
|
|
|
|
2023-10-09 16:18:47 +03:00
|
|
|
if err := rows.Err(); err != nil {
|
2023-09-28 17:09:52 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-09 16:18:47 +03:00
|
|
|
return list, nil
|
2023-09-28 17:09:52 +03:00
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) DeleteMemoOrganizer(ctx context.Context, delete *store.DeleteMemoOrganizer) error {
|
2023-09-28 17:09:52 +03:00
|
|
|
where, args := []string{}, []any{}
|
|
|
|
if v := delete.MemoID; v != nil {
|
2023-10-07 17:56:12 +03:00
|
|
|
where, args = append(where, "`memo_id` = ?"), append(args, *v)
|
2023-09-28 17:09:52 +03:00
|
|
|
}
|
|
|
|
if v := delete.UserID; v != nil {
|
2023-10-07 17:56:12 +03:00
|
|
|
where, args = append(where, "`user_id` = ?"), append(args, *v)
|
2023-09-28 17:09:52 +03:00
|
|
|
}
|
2023-10-07 17:56:12 +03:00
|
|
|
stmt := "DELETE FROM `memo_organizer` WHERE " + strings.Join(where, " AND ")
|
2023-09-28 17:09:52 +03:00
|
|
|
if _, err := d.db.ExecContext(ctx, stmt, args...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vacuumMemoOrganizer(ctx context.Context, tx *sql.Tx) error {
|
2023-10-07 17:56:12 +03:00
|
|
|
stmt := "DELETE FROM `memo_organizer` WHERE `memo_id` NOT IN (SELECT `id` FROM `memo`) OR `user_id` NOT IN (SELECT `id` FROM `user`)"
|
2023-09-28 17:09:52 +03:00
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|