|
|
|
|
@ -84,17 +84,11 @@ type DeleteMemo struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) CreateMemo(ctx context.Context, create *Memo) (*Memo, error) {
|
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
if create.CreatedTs == 0 {
|
|
|
|
|
create.CreatedTs = time.Now().Unix()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query := `
|
|
|
|
|
stmt := `
|
|
|
|
|
INSERT INTO memo (
|
|
|
|
|
creator_id,
|
|
|
|
|
created_ts,
|
|
|
|
|
@ -104,9 +98,9 @@ func (s *Store) CreateMemo(ctx context.Context, create *Memo) (*Memo, error) {
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
|
RETURNING id, created_ts, updated_ts, row_status
|
|
|
|
|
`
|
|
|
|
|
if err := tx.QueryRowContext(
|
|
|
|
|
if err := s.db.QueryRowContext(
|
|
|
|
|
ctx,
|
|
|
|
|
query,
|
|
|
|
|
stmt,
|
|
|
|
|
create.CreatorID,
|
|
|
|
|
create.CreatedTs,
|
|
|
|
|
create.Content,
|
|
|
|
|
@ -119,155 +113,12 @@ func (s *Store) CreateMemo(ctx context.Context, create *Memo) (*Memo, error) {
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memo := create
|
|
|
|
|
return memo, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) ListMemos(ctx context.Context, find *FindMemo) ([]*Memo, error) {
|
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
list, err := listMemos(ctx, tx, find)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) GetMemo(ctx context.Context, find *FindMemo) (*Memo, error) {
|
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
list, err := listMemos(ctx, tx, find)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memo := list[0]
|
|
|
|
|
return memo, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) UpdateMemo(ctx context.Context, update *UpdateMemo) error {
|
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
set, args := []string{}, []any{}
|
|
|
|
|
if v := update.CreatedTs; v != nil {
|
|
|
|
|
set, args = append(set, "created_ts = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
if v := update.UpdatedTs; v != nil {
|
|
|
|
|
set, args = append(set, "updated_ts = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
if v := update.RowStatus; v != nil {
|
|
|
|
|
set, args = append(set, "row_status = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
if v := update.Content; v != nil {
|
|
|
|
|
set, args = append(set, "content = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
if v := update.Visibility; v != nil {
|
|
|
|
|
set, args = append(set, "visibility = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
args = append(args, update.ID)
|
|
|
|
|
|
|
|
|
|
query := `
|
|
|
|
|
UPDATE memo
|
|
|
|
|
SET ` + strings.Join(set, ", ") + `
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
if _, err := tx.ExecContext(ctx, query, args...); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = tx.Commit()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) DeleteMemo(ctx context.Context, delete *DeleteMemo) error {
|
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
where, args := []string{"id = ?"}, []any{delete.ID}
|
|
|
|
|
stmt := `DELETE FROM memo WHERE ` + strings.Join(where, " AND ")
|
|
|
|
|
_, err = tx.ExecContext(ctx, stmt, args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := s.vacuumImpl(ctx, tx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = tx.Commit()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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{}
|
|
|
|
|
|
|
|
|
|
if v := find.ID; v != nil {
|
|
|
|
|
@ -341,7 +192,7 @@ func listMemos(ctx context.Context, tx *sql.Tx, find *FindMemo) ([]*Memo, error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
|
|
|
|
rows, err := s.db.QueryContext(ctx, query, args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
@ -407,6 +258,98 @@ func listMemos(ctx context.Context, tx *sql.Tx, find *FindMemo) ([]*Memo, error)
|
|
|
|
|
return list, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) GetMemo(ctx context.Context, find *FindMemo) (*Memo, error) {
|
|
|
|
|
list, err := s.ListMemos(ctx, find)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memo := list[0]
|
|
|
|
|
return memo, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) UpdateMemo(ctx context.Context, update *UpdateMemo) error {
|
|
|
|
|
set, args := []string{}, []any{}
|
|
|
|
|
if v := update.CreatedTs; v != nil {
|
|
|
|
|
set, args = append(set, "created_ts = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
if v := update.UpdatedTs; v != nil {
|
|
|
|
|
set, args = append(set, "updated_ts = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
if v := update.RowStatus; v != nil {
|
|
|
|
|
set, args = append(set, "row_status = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
if v := update.Content; v != nil {
|
|
|
|
|
set, args = append(set, "content = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
if v := update.Visibility; v != nil {
|
|
|
|
|
set, args = append(set, "visibility = ?"), append(args, *v)
|
|
|
|
|
}
|
|
|
|
|
args = append(args, update.ID)
|
|
|
|
|
|
|
|
|
|
stmt := `
|
|
|
|
|
UPDATE memo
|
|
|
|
|
SET ` + strings.Join(set, ", ") + `
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
if _, err := s.db.ExecContext(ctx, stmt, args...); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) DeleteMemo(ctx context.Context, delete *DeleteMemo) error {
|
|
|
|
|
where, args := []string{"id = ?"}, []any{delete.ID}
|
|
|
|
|
stmt := `DELETE FROM memo WHERE ` + strings.Join(where, " AND ")
|
|
|
|
|
result, err := s.db.ExecContext(ctx, stmt, args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if _, err := result.RowsAffected(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := s.Vacuum(ctx); err != nil {
|
|
|
|
|
// Prevent linter warning.
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) FindMemosVisibilityList(ctx context.Context, memoIDs []int) ([]Visibility, error) {
|
|
|
|
|
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 := s.db.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 vacuumMemo(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
|
stmt := `
|
|
|
|
|
DELETE FROM
|
|
|
|
|
|