memos/store/memo.go

428 lines
9.0 KiB
Go
Raw Normal View History

2021-12-08 18:43:14 +03:00
package store
2021-12-09 17:02:57 +03:00
import (
2022-08-07 05:17:12 +03:00
"context"
2022-05-21 19:59:22 +03:00
"database/sql"
2022-02-03 10:32:03 +03:00
"fmt"
2023-05-25 16:50:37 +03:00
"strconv"
2021-12-09 17:02:57 +03:00
"strings"
"time"
2021-12-09 17:02:57 +03:00
)
2021-12-08 18:43:14 +03:00
2023-05-25 16:50:37 +03:00
// Visibility is the type of a visibility.
type Visibility string
const (
// Public is the PUBLIC visibility.
Public Visibility = "PUBLIC"
// Protected is the PROTECTED visibility.
Protected Visibility = "PROTECTED"
// Private is the PRIVATE visibility.
Private Visibility = "PRIVATE"
)
func (v Visibility) String() string {
switch v {
case Public:
return "PUBLIC"
case Protected:
return "PROTECTED"
case Private:
return "PRIVATE"
}
return "PRIVATE"
}
type Memo struct {
2022-05-19 13:32:04 +03:00
ID int
// Standard fields
2023-05-25 16:50:37 +03:00
RowStatus RowStatus
2022-05-19 13:32:04 +03:00
CreatorID int
CreatedTs int64
UpdatedTs int64
// Domain specific fields
Content string
2023-05-25 16:50:37 +03:00
Visibility Visibility
2022-05-19 13:32:04 +03:00
2023-05-25 16:50:37 +03:00
// Composed fields
Pinned bool
ResourceIDList []int
RelationList []*MemoRelation
2022-05-19 13:32:04 +03:00
}
type FindMemo struct {
2023-05-25 16:50:37 +03:00
ID *int
2022-08-07 05:17:12 +03:00
2023-05-25 16:50:37 +03:00
// Standard fields
RowStatus *RowStatus
CreatorID *int
2022-08-07 05:17:12 +03:00
2023-05-25 16:50:37 +03:00
// Domain specific fields
Pinned *bool
ContentSearch []string
2023-05-25 16:50:37 +03:00
VisibilityList []Visibility
2022-05-19 13:32:04 +03:00
2023-05-25 16:50:37 +03:00
// Pagination
Limit *int
Offset *int
OrderByUpdatedTs bool
2023-05-25 16:50:37 +03:00
}
2022-08-07 05:17:12 +03:00
type UpdateMemo struct {
2023-05-25 16:50:37 +03:00
ID int
CreatedTs *int64
UpdatedTs *int64
RowStatus *RowStatus
Content *string
Visibility *Visibility
}
2022-08-07 03:09:43 +03:00
type DeleteMemo struct {
2023-05-25 16:50:37 +03:00
ID int
2021-12-08 18:43:14 +03:00
}
func (s *Store) CreateMemo(ctx context.Context, create *Memo) (*Memo, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
2022-08-07 05:17:12 +03:00
}
defer tx.Rollback()
if create.CreatedTs == 0 {
create.CreatedTs = time.Now().Unix()
}
2023-05-25 16:50:37 +03:00
query := `
INSERT INTO memo (
creator_id,
created_ts,
content,
visibility
)
VALUES (?, ?, ?, ?)
RETURNING id, created_ts, updated_ts, row_status
`
if err := tx.QueryRowContext(
ctx,
query,
create.CreatorID,
create.CreatedTs,
create.Content,
create.Visibility,
).Scan(
&create.ID,
&create.CreatedTs,
&create.UpdatedTs,
&create.RowStatus,
); 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
}
memo := create
return memo, nil
2021-12-09 17:02:57 +03:00
}
func (s *Store) ListMemos(ctx context.Context, find *FindMemo) ([]*Memo, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
2022-08-07 05:17:12 +03:00
}
defer tx.Rollback()
2023-05-25 16:50:37 +03:00
list, err := listMemos(ctx, tx, find)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
2022-02-03 10:32:03 +03:00
return list, nil
}
2021-12-08 18:43:14 +03:00
func (s *Store) GetMemo(ctx context.Context, find *FindMemo) (*Memo, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
2022-08-07 05:17:12 +03:00
}
defer tx.Rollback()
2023-05-25 16:50:37 +03:00
list, err := listMemos(ctx, tx, find)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
2021-12-08 18:43:14 +03:00
}
2022-02-03 10:32:03 +03:00
if len(list) == 0 {
return nil, nil
}
if err := tx.Commit(); err != nil {
return nil, err
2021-12-08 18:43:14 +03:00
}
memo := list[0]
return memo, nil
2022-02-03 10:32:03 +03:00
}
2021-12-08 18:43:14 +03:00
func (s *Store) UpdateMemo(ctx context.Context, update *UpdateMemo) error {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
2022-02-03 10:32:03 +03:00
if err != nil {
2022-11-06 07:21:58 +03:00
return err
}
2023-05-25 16:50:37 +03:00
defer tx.Rollback()
set, args := []string{}, []any{}
2023-05-25 16:50:37 +03:00
if v := update.CreatedTs; v != nil {
2022-08-25 17:02:32 +03:00
set, args = append(set, "created_ts = ?"), append(args, *v)
2022-02-03 10:32:03 +03:00
}
2023-05-25 16:50:37 +03:00
if v := update.UpdatedTs; v != nil {
set, args = append(set, "updated_ts = ?"), append(args, *v)
}
2023-05-27 06:14:23 +03:00
if v := update.RowStatus; v != nil {
set, args = append(set, "row_status = ?"), append(args, *v)
}
2023-05-25 16:50:37 +03:00
if v := update.Content; v != nil {
2022-08-25 17:02:32 +03:00
set, args = append(set, "content = ?"), append(args, *v)
}
2023-05-25 16:50:37 +03:00
if v := update.Visibility; v != nil {
set, args = append(set, "visibility = ?"), append(args, *v)
}
2023-05-25 16:50:37 +03:00
args = append(args, update.ID)
2022-02-03 10:32:03 +03:00
2022-08-07 05:17:12 +03:00
query := `
2022-02-03 10:32:03 +03:00
UPDATE memo
2022-08-07 05:17:12 +03:00
SET ` + strings.Join(set, ", ") + `
2022-02-03 10:32:03 +03:00
WHERE id = ?
2022-08-07 05:17:12 +03:00
`
2023-05-25 16:50:37 +03:00
if _, err := tx.ExecContext(ctx, query, args...); err != nil {
return err
2022-02-03 10:32:03 +03:00
}
2023-05-25 16:50:37 +03:00
err = tx.Commit()
return err
}
2022-02-03 10:32:03 +03:00
func (s *Store) DeleteMemo(ctx context.Context, delete *DeleteMemo) error {
2023-05-25 16:50:37 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
2023-05-25 16:50:37 +03:00
defer tx.Rollback()
2023-05-25 16:50:37 +03:00
where, args := []string{"id = ?"}, []any{delete.ID}
stmt := `DELETE FROM memo WHERE ` + strings.Join(where, " AND ")
_, err = tx.ExecContext(ctx, stmt, args...)
2023-05-25 16:50:37 +03:00
if err != nil {
return err
}
2023-05-25 16:50:37 +03:00
if err := s.vacuumImpl(ctx, tx); err != nil {
return err
}
err = tx.Commit()
return err
2021-12-08 18:43:14 +03:00
}
func (s *Store) FindMemosVisibilityList(ctx context.Context, memoIDs []int) ([]Visibility, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
args := make([]any, 0, len(memoIDs))
list := make([]string, 0, len(memoIDs))
for _, memoID := range memoIDs {
args = append(args, memoID)
list = append(list, "?")
}
where := fmt.Sprintf("id in (%s)", strings.Join(list, ","))
query := `SELECT DISTINCT(visibility) FROM memo WHERE ` + where
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
visibilityList := make([]Visibility, 0)
for rows.Next() {
var visibility Visibility
if err := rows.Scan(&visibility); err != nil {
return nil, err
}
visibilityList = append(visibilityList, visibility)
}
if err := rows.Err(); err != nil {
return nil, err
}
return visibilityList, nil
}
func listMemos(ctx context.Context, tx *sql.Tx, find *FindMemo) ([]*Memo, error) {
where, args := []string{"1 = 1"}, []any{}
2021-12-08 18:43:14 +03:00
2022-05-02 21:05:43 +03:00
if v := find.ID; v != nil {
where, args = append(where, "memo.id = ?"), append(args, *v)
2022-02-03 10:32:03 +03:00
}
2022-05-02 21:05:43 +03:00
if v := find.CreatorID; v != nil {
where, args = append(where, "memo.creator_id = ?"), append(args, *v)
2021-12-09 17:02:57 +03:00
}
2022-02-04 16:24:21 +03:00
if v := find.RowStatus; v != nil {
where, args = append(where, "memo.row_status = ?"), append(args, *v)
2022-02-04 16:24:21 +03:00
}
2022-05-19 13:32:04 +03:00
if v := find.Pinned; v != nil {
where = append(where, "memo_organizer.pinned = 1")
2022-05-19 13:32:04 +03:00
}
if v := find.ContentSearch; len(v) != 0 {
for _, s := range v {
where, args = append(where, "memo.content LIKE ?"), append(args, "%"+s+"%")
}
2022-06-14 18:09:03 +03:00
}
if v := find.VisibilityList; len(v) != 0 {
list := []string{}
for _, visibility := range v {
list = append(list, fmt.Sprintf("$%d", len(args)+1))
args = append(args, visibility)
}
where = append(where, fmt.Sprintf("memo.visibility in (%s)", strings.Join(list, ",")))
}
orders := []string{"pinned DESC"}
if find.OrderByUpdatedTs {
orders = append(orders, "updated_ts DESC")
} else {
orders = append(orders, "created_ts DESC")
}
orders = append(orders, "id DESC")
2021-12-08 18:43:14 +03:00
2022-08-07 05:17:12 +03:00
query := `
2023-05-25 16:50:37 +03:00
SELECT
memo.id AS id,
memo.creator_id AS creator_id,
memo.created_ts AS created_ts,
memo.updated_ts AS updated_ts,
memo.row_status AS row_status,
memo.content AS content,
memo.visibility AS visibility,
CASE WHEN memo_organizer.pinned = 1 THEN 1 ELSE 0 END AS pinned,
GROUP_CONCAT(memo_resource.resource_id) AS resource_id_list,
(
SELECT
GROUP_CONCAT(related_memo_id || ':' || type)
FROM
memo_relation
WHERE
memo_relation.memo_id = memo.id
GROUP BY
memo_relation.memo_id
) AS relation_list
FROM
memo
LEFT JOIN
memo_organizer ON memo.id = memo_organizer.memo_id
LEFT JOIN
memo_resource ON memo.id = memo_resource.memo_id
WHERE ` + strings.Join(where, " AND ") + `
GROUP BY memo.id
ORDER BY ` + strings.Join(orders, ", ") + `
2022-10-27 17:02:42 +03:00
`
if find.Limit != nil {
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
if find.Offset != nil {
query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
}
}
2022-08-07 05:17:12 +03:00
rows, err := tx.QueryContext(ctx, query, args...)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
2022-02-03 10:32:03 +03:00
}
2021-12-09 17:02:57 +03:00
defer rows.Close()
list := make([]*Memo, 0)
2021-12-08 18:43:14 +03:00
for rows.Next() {
var memo Memo
2023-05-25 16:50:37 +03:00
var memoResourceIDList sql.NullString
var memoRelationList sql.NullString
2022-02-03 10:32:03 +03:00
if err := rows.Scan(
&memo.ID,
&memo.CreatorID,
&memo.CreatedTs,
&memo.UpdatedTs,
&memo.RowStatus,
&memo.Content,
&memo.Visibility,
&memo.Pinned,
2023-05-25 16:50:37 +03:00
&memoResourceIDList,
&memoRelationList,
2022-02-03 10:32:03 +03:00
); err != nil {
return nil, err
2022-02-03 10:32:03 +03:00
}
2023-05-25 16:50:37 +03:00
if memoResourceIDList.Valid {
idStringList := strings.Split(memoResourceIDList.String, ",")
memo.ResourceIDList = make([]int, 0, len(idStringList))
2023-05-25 16:50:37 +03:00
for _, idString := range idStringList {
id, err := strconv.Atoi(idString)
if err != nil {
return nil, err
2023-05-25 16:50:37 +03:00
}
memo.ResourceIDList = append(memo.ResourceIDList, id)
2023-05-25 16:50:37 +03:00
}
}
if memoRelationList.Valid {
memo.RelationList = make([]*MemoRelation, 0)
2023-05-25 16:50:37 +03:00
relatedMemoTypeList := strings.Split(memoRelationList.String, ",")
for _, relatedMemoType := range relatedMemoTypeList {
relatedMemoTypeList := strings.Split(relatedMemoType, ":")
if len(relatedMemoTypeList) != 2 {
return nil, fmt.Errorf("invalid relation format")
2023-05-25 16:50:37 +03:00
}
relatedMemoID, err := strconv.Atoi(relatedMemoTypeList[0])
if err != nil {
return nil, err
2023-05-25 16:50:37 +03:00
}
memo.RelationList = append(memo.RelationList, &MemoRelation{
MemoID: memo.ID,
2023-05-25 16:50:37 +03:00
RelatedMemoID: relatedMemoID,
Type: MemoRelationType(relatedMemoTypeList[1]),
})
}
}
list = append(list, &memo)
2021-12-08 18:43:14 +03:00
}
2021-12-10 08:41:17 +03:00
if err := rows.Err(); err != nil {
return nil, err
2022-02-03 10:32:03 +03:00
}
return list, nil
2022-11-06 07:21:58 +03:00
}
func vacuumMemo(ctx context.Context, tx *sql.Tx) error {
stmt := `
DELETE FROM
memo
WHERE
creator_id NOT IN (
SELECT
id
FROM
user
)`
_, err := tx.ExecContext(ctx, stmt)
if err != nil {
return err
}
2022-02-03 10:32:03 +03:00
return nil
2021-12-08 18:43:14 +03:00
}