2021-12-08 18:43:14 +03:00
|
|
|
package store
|
|
|
|
|
2021-12-09 17:02:57 +03:00
|
|
|
import (
|
2022-02-03 10:32:03 +03:00
|
|
|
"fmt"
|
|
|
|
"memos/api"
|
|
|
|
"memos/common"
|
2021-12-09 17:02:57 +03:00
|
|
|
"strings"
|
|
|
|
)
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
type MemoService struct {
|
|
|
|
db *DB
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
func NewMemoService(db *DB) *MemoService {
|
|
|
|
return &MemoService{db: db}
|
|
|
|
}
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2022-02-03 10:32:03 +03: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 18:43:14 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return memo, nil
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03: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 17:02:57 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03: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 18:43:14 +03:00
|
|
|
|
2022-02-03 10:32:03 +03: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 18:43:14 +03:00
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return list[0], nil
|
|
|
|
}
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2022-02-03 10:32:03 +03: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 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03: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
|
|
|
|
`,
|
|
|
|
create.CreatorId,
|
|
|
|
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(
|
|
|
|
&memo.Id,
|
|
|
|
&memo.CreatorId,
|
|
|
|
&memo.CreatedTs,
|
|
|
|
&memo.UpdatedTs,
|
|
|
|
&memo.Content,
|
|
|
|
&memo.RowStatus,
|
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &memo, nil
|
2021-12-09 17:02:57 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
func patchMemo(db *DB, patch *api.MemoPatch) (*api.Memo, error) {
|
|
|
|
set, args := []string{}, []interface{}{}
|
|
|
|
|
|
|
|
if v := patch.Content; v != nil {
|
2022-02-04 16:24:21 +03:00
|
|
|
set, args = append(set, "content = ?"), append(args, *v)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
if v := patch.RowStatus; v != nil {
|
2022-02-04 16:24:21 +03:00
|
|
|
set, args = append(set, "row_status = ?"), append(args, *v)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, patch.Id)
|
|
|
|
|
|
|
|
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(
|
|
|
|
&memo.Id,
|
|
|
|
&memo.CreatedTs,
|
|
|
|
&memo.UpdatedTs,
|
|
|
|
&memo.Content,
|
|
|
|
&memo.RowStatus,
|
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &memo, nil
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
func findMemoList(db *DB, find *api.MemoFind) ([]*api.Memo, error) {
|
|
|
|
where, args := []string{"1 = 1"}, []interface{}{}
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
if v := find.Id; v != nil {
|
|
|
|
where, args = append(where, "id = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := find.CreatorId; v != nil {
|
|
|
|
where, args = append(where, "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, "row_status = ?"), append(args, *v)
|
|
|
|
}
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2022-02-03 10:32:03 +03: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 17:02:57 +03:00
|
|
|
defer rows.Close()
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
list := make([]*api.Memo, 0)
|
2021-12-08 18:43:14 +03:00
|
|
|
for rows.Next() {
|
2022-02-03 10:32:03 +03:00
|
|
|
var memo api.Memo
|
|
|
|
if err := rows.Scan(
|
|
|
|
&memo.Id,
|
|
|
|
&memo.CreatorId,
|
|
|
|
&memo.CreatedTs,
|
|
|
|
&memo.UpdatedTs,
|
|
|
|
&memo.Content,
|
|
|
|
&memo.RowStatus,
|
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-02-03 10:32:03 +03:00
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteMemo(db *DB, delete *api.MemoDelete) error {
|
|
|
|
result, err := db.Db.Exec(`DELETE FROM memo WHERE id = ?`, delete.Id)
|
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, _ := result.RowsAffected()
|
|
|
|
if rows == 0 {
|
|
|
|
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("memo ID not found: %d", delete.Id)}
|
2021-12-10 08:41:17 +03:00
|
|
|
}
|
2021-12-09 17:02:57 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return nil
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|