2021-12-08 23:43:14 +08:00
|
|
|
package store
|
|
|
|
|
2021-12-09 22:02:57 +08:00
|
|
|
import (
|
2022-02-03 15:32:03 +08:00
|
|
|
"fmt"
|
|
|
|
"memos/api"
|
|
|
|
"memos/common"
|
2021-12-09 22:02:57 +08:00
|
|
|
"strings"
|
|
|
|
)
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
type MemoService struct {
|
|
|
|
db *DB
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
func NewMemoService(db *DB) *MemoService {
|
|
|
|
return &MemoService{db: db}
|
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
func (s *MemoService) CreateMemo(create *api.MemoCreate) (*api.Memo, error) {
|
|
|
|
memo, err := createMemo(s.db, create)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return memo, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
func (s *MemoService) PatchMemo(patch *api.MemoPatch) (*api.Memo, error) {
|
|
|
|
memo, err := patchMemo(s.db, patch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return memo, nil
|
2021-12-09 22:02:57 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
func (s *MemoService) FindMemoList(find *api.MemoFind) ([]*api.Memo, error) {
|
|
|
|
list, err := findMemoList(s.db, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
func (s *MemoService) FindMemo(find *api.MemoFind) (*api.Memo, error) {
|
|
|
|
list, err := findMemoList(s.db, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return list[0], nil
|
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
func (s *MemoService) DeleteMemo(delete *api.MemoDelete) error {
|
|
|
|
err := deleteMemo(s.db, delete)
|
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
func createMemo(db *DB, create *api.MemoCreate) (*api.Memo, error) {
|
|
|
|
row, err := db.Db.Query(`
|
|
|
|
INSERT INTO memo (
|
|
|
|
creator_id,
|
|
|
|
content
|
|
|
|
)
|
|
|
|
VALUES (?, ?)
|
|
|
|
RETURNING id, creator_id, created_ts, updated_ts, content, row_status
|
|
|
|
`,
|
2022-05-03 02:05:43 +08:00
|
|
|
create.CreatorID,
|
2022-02-03 15:32:03 +08:00
|
|
|
create.Content,
|
|
|
|
)
|
|
|
|
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 memo api.Memo
|
|
|
|
if err := row.Scan(
|
2022-05-03 02:05:43 +08:00
|
|
|
&memo.ID,
|
|
|
|
&memo.CreatorID,
|
2022-02-03 15:32:03 +08:00
|
|
|
&memo.CreatedTs,
|
|
|
|
&memo.UpdatedTs,
|
|
|
|
&memo.Content,
|
|
|
|
&memo.RowStatus,
|
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &memo, nil
|
2021-12-09 22:02:57 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
func patchMemo(db *DB, patch *api.MemoPatch) (*api.Memo, error) {
|
|
|
|
set, args := []string{}, []interface{}{}
|
|
|
|
|
|
|
|
if v := patch.Content; v != nil {
|
2022-02-04 21:24:21 +08:00
|
|
|
set, args = append(set, "content = ?"), append(args, *v)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
if v := patch.RowStatus; v != nil {
|
2022-02-04 21:24:21 +08:00
|
|
|
set, args = append(set, "row_status = ?"), append(args, *v)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-03-29 00:01:34 +08:00
|
|
|
if v := patch.CreatedTs; v != nil {
|
|
|
|
set, args = append(set, "created_ts = ?"), append(args, *v)
|
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
args = append(args, patch.ID)
|
2022-02-03 15:32:03 +08:00
|
|
|
|
|
|
|
row, err := db.Db.Query(`
|
|
|
|
UPDATE memo
|
|
|
|
SET `+strings.Join(set, ", ")+`
|
|
|
|
WHERE id = ?
|
|
|
|
RETURNING id, created_ts, updated_ts, content, row_status
|
|
|
|
`, args...)
|
|
|
|
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 memo api.Memo
|
|
|
|
if err := row.Scan(
|
2022-05-03 02:05:43 +08:00
|
|
|
&memo.ID,
|
2022-02-03 15:32:03 +08:00
|
|
|
&memo.CreatedTs,
|
|
|
|
&memo.UpdatedTs,
|
|
|
|
&memo.Content,
|
|
|
|
&memo.RowStatus,
|
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &memo, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
func findMemoList(db *DB, find *api.MemoFind) ([]*api.Memo, error) {
|
|
|
|
where, args := []string{"1 = 1"}, []interface{}{}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
if v := find.ID; v != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
where, args = append(where, "id = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-03 02:05:43 +08:00
|
|
|
if v := find.CreatorID; v != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
where, args = append(where, "creator_id = ?"), append(args, *v)
|
2021-12-09 22:02:57 +08:00
|
|
|
}
|
2022-02-04 21:24:21 +08:00
|
|
|
if v := find.RowStatus; v != nil {
|
|
|
|
where, args = append(where, "row_status = ?"), append(args, *v)
|
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
rows, err := db.Db.Query(`
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
creator_id,
|
|
|
|
created_ts,
|
|
|
|
updated_ts,
|
|
|
|
content,
|
|
|
|
row_status
|
|
|
|
FROM memo
|
|
|
|
WHERE `+strings.Join(where, " AND "),
|
|
|
|
args...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
2021-12-09 22:02:57 +08:00
|
|
|
defer rows.Close()
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
list := make([]*api.Memo, 0)
|
2021-12-08 23:43:14 +08:00
|
|
|
for rows.Next() {
|
2022-02-03 15:32:03 +08:00
|
|
|
var memo api.Memo
|
|
|
|
if err := rows.Scan(
|
2022-05-03 02:05:43 +08:00
|
|
|
&memo.ID,
|
|
|
|
&memo.CreatorID,
|
2022-02-03 15:32:03 +08:00
|
|
|
&memo.CreatedTs,
|
|
|
|
&memo.UpdatedTs,
|
|
|
|
&memo.Content,
|
|
|
|
&memo.RowStatus,
|
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, &memo)
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2021-12-10 13:41:17 +08:00
|
|
|
if err := rows.Err(); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteMemo(db *DB, delete *api.MemoDelete) error {
|
2022-05-03 02:05:43 +08:00
|
|
|
result, err := db.Db.Exec(`DELETE FROM memo WHERE id = ?`, delete.ID)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, _ := result.RowsAffected()
|
|
|
|
if rows == 0 {
|
2022-05-03 02:05:43 +08:00
|
|
|
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("memo ID not found: %d", delete.ID)}
|
2021-12-10 13:41:17 +08:00
|
|
|
}
|
2021-12-09 22:02:57 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|