mirror of
https://github.com/aykhans/slash-e.git
synced 2025-06-14 03:47:50 +00:00
chore: code clean
This commit is contained in:
@ -1,173 +0,0 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
storepb "github.com/yourselfhosted/slash/proto/gen/store"
|
||||
"github.com/yourselfhosted/slash/store"
|
||||
)
|
||||
|
||||
func (d *DB) CreateMemo(ctx context.Context, create *storepb.Memo) (*storepb.Memo, error) {
|
||||
set := []string{"creator_id", "name", "title", "content", "visibility", "tag"}
|
||||
args := []any{create.CreatorId, create.Name, create.Title, create.Content, create.Visibility.String(), strings.Join(create.Tags, " ")}
|
||||
|
||||
stmt := `
|
||||
INSERT INTO memo (` + strings.Join(set, ", ") + `)
|
||||
VALUES (` + placeholders(len(args)) + `)
|
||||
RETURNING id, created_ts, updated_ts, row_status
|
||||
`
|
||||
|
||||
var rowStatus string
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&create.Id,
|
||||
&create.CreatedTs,
|
||||
&create.UpdatedTs,
|
||||
&rowStatus,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
create.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
memo := create
|
||||
return memo, nil
|
||||
}
|
||||
|
||||
func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) (*storepb.Memo, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if update.RowStatus != nil {
|
||||
set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, update.RowStatus.String())
|
||||
}
|
||||
if update.Name != nil {
|
||||
set, args = append(set, "name = "+placeholder(len(args)+1)), append(args, *update.Name)
|
||||
}
|
||||
if update.Title != nil {
|
||||
set, args = append(set, "title = "+placeholder(len(args)+1)), append(args, *update.Title)
|
||||
}
|
||||
if update.Content != nil {
|
||||
set, args = append(set, "content = "+placeholder(len(args)+1)), append(args, *update.Content)
|
||||
}
|
||||
if update.Visibility != nil {
|
||||
set, args = append(set, "visibility = "+placeholder(len(args)+1)), append(args, update.Visibility.String())
|
||||
}
|
||||
if update.Tag != nil {
|
||||
set, args = append(set, "tag = "+placeholder(len(args)+1)), append(args, *update.Tag)
|
||||
}
|
||||
if len(set) == 0 {
|
||||
return nil, errors.New("no update specified")
|
||||
}
|
||||
|
||||
stmt := `
|
||||
UPDATE memo
|
||||
SET ` + strings.Join(set, ", ") + `
|
||||
WHERE id = ` + placeholder(len(args)+1) + `
|
||||
RETURNING id, creator_id, created_ts, updated_ts, row_status, name, title, content, visibility, tag
|
||||
`
|
||||
args = append(args, update.ID)
|
||||
memo := &storepb.Memo{}
|
||||
var rowStatus, visibility, tags string
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&memo.Id,
|
||||
&memo.CreatorId,
|
||||
&memo.CreatedTs,
|
||||
&memo.UpdatedTs,
|
||||
&rowStatus,
|
||||
&memo.Name,
|
||||
&memo.Title,
|
||||
&memo.Content,
|
||||
&visibility,
|
||||
&tags,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
memo.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
memo.Visibility = convertVisibilityStringToStorepb(visibility)
|
||||
memo.Tags = filterTags(strings.Split(tags, " "))
|
||||
return memo, nil
|
||||
}
|
||||
|
||||
func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*storepb.Memo, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
if v := find.ID; v != nil {
|
||||
where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *v)
|
||||
}
|
||||
if v := find.CreatorID; v != nil {
|
||||
where, args = append(where, "creator_id = "+placeholder(len(args)+1)), append(args, *v)
|
||||
}
|
||||
if v := find.RowStatus; v != nil {
|
||||
where, args = append(where, "row_status = "+placeholder(len(args)+1)), append(args, *v)
|
||||
}
|
||||
if v := find.Name; v != nil {
|
||||
where, args = append(where, "name = "+placeholder(len(args)+1)), append(args, *v)
|
||||
}
|
||||
if v := find.VisibilityList; len(v) != 0 {
|
||||
list := []string{}
|
||||
for _, visibility := range v {
|
||||
list, args = append(list, placeholder(len(args)+1)), append(args, visibility)
|
||||
}
|
||||
where = append(where, fmt.Sprintf("visibility IN (%s)", strings.Join(list, ",")))
|
||||
}
|
||||
if v := find.Tag; v != nil {
|
||||
where, args = append(where, "tag LIKE "+placeholder(len(args)+1)), append(args, "%"+*v+"%")
|
||||
}
|
||||
|
||||
rows, err := d.db.QueryContext(ctx, `
|
||||
SELECT
|
||||
id,
|
||||
creator_id,
|
||||
created_ts,
|
||||
updated_ts,
|
||||
row_status,
|
||||
name,
|
||||
title,
|
||||
content,
|
||||
visibility,
|
||||
tag
|
||||
FROM memo
|
||||
WHERE `+strings.Join(where, " AND ")+`
|
||||
ORDER BY created_ts DESC`,
|
||||
args...,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
list := make([]*storepb.Memo, 0)
|
||||
for rows.Next() {
|
||||
memo := &storepb.Memo{}
|
||||
var rowStatus, visibility, tags string
|
||||
if err := rows.Scan(
|
||||
&memo.Id,
|
||||
&memo.CreatorId,
|
||||
&memo.CreatedTs,
|
||||
&memo.UpdatedTs,
|
||||
&rowStatus,
|
||||
&memo.Name,
|
||||
&memo.Title,
|
||||
&memo.Content,
|
||||
&visibility,
|
||||
&tags,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
memo.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
memo.Visibility = storepb.Visibility(storepb.Visibility_value[visibility])
|
||||
memo.Tags = filterTags(strings.Split(tags, " "))
|
||||
list = append(list, memo)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
|
||||
if _, err := d.db.ExecContext(ctx, `DELETE FROM memo WHERE id = $1`, delete.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@ -74,19 +74,3 @@ CREATE TABLE collection (
|
||||
);
|
||||
|
||||
CREATE INDEX idx_collection_name ON collection(name);
|
||||
|
||||
-- memo
|
||||
CREATE TABLE memo (
|
||||
id SERIAL PRIMARY KEY,
|
||||
creator_id INTEGER REFERENCES "user"(id) NOT NULL,
|
||||
created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
|
||||
updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
|
||||
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
content TEXT NOT NULL DEFAULT '',
|
||||
visibility TEXT NOT NULL CHECK (visibility IN ('PRIVATE', 'WORKSPACE', 'PUBLIC')) DEFAULT 'PRIVATE',
|
||||
tag TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
CREATE INDEX idx_memo_name ON memo(name);
|
||||
|
@ -1,182 +0,0 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
storepb "github.com/yourselfhosted/slash/proto/gen/store"
|
||||
"github.com/yourselfhosted/slash/store"
|
||||
)
|
||||
|
||||
func (d *DB) CreateMemo(ctx context.Context, create *storepb.Memo) (*storepb.Memo, error) {
|
||||
set := []string{"creator_id", "name", "title", "content", "visibility", "tag"}
|
||||
args := []any{create.CreatorId, create.Name, create.Title, create.Content, create.Visibility.String(), strings.Join(create.Tags, " ")}
|
||||
|
||||
stmt := `
|
||||
INSERT INTO memo (
|
||||
` + strings.Join(set, ", ") + `
|
||||
)
|
||||
VALUES (` + placeholders(len(args)) + `)
|
||||
RETURNING id, created_ts, updated_ts, row_status
|
||||
`
|
||||
|
||||
var rowStatus string
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&create.Id,
|
||||
&create.CreatedTs,
|
||||
&create.UpdatedTs,
|
||||
&rowStatus,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
create.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
memo := create
|
||||
return memo, nil
|
||||
}
|
||||
|
||||
func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) (*storepb.Memo, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if update.RowStatus != nil {
|
||||
set, args = append(set, "row_status = ?"), append(args, update.RowStatus.String())
|
||||
}
|
||||
if update.Name != nil {
|
||||
set, args = append(set, "name = ?"), append(args, *update.Name)
|
||||
}
|
||||
if update.Title != nil {
|
||||
set, args = append(set, "title = ?"), append(args, *update.Title)
|
||||
}
|
||||
if update.Content != nil {
|
||||
set, args = append(set, "content = ?"), append(args, *update.Content)
|
||||
}
|
||||
if update.Visibility != nil {
|
||||
set, args = append(set, "visibility = ?"), append(args, update.Visibility.String())
|
||||
}
|
||||
if update.Tag != nil {
|
||||
set, args = append(set, "tag = ?"), append(args, *update.Tag)
|
||||
}
|
||||
if len(set) == 0 {
|
||||
return nil, errors.New("no update specified")
|
||||
}
|
||||
args = append(args, update.ID)
|
||||
|
||||
stmt := `
|
||||
UPDATE memo
|
||||
SET
|
||||
` + strings.Join(set, ", ") + `
|
||||
WHERE
|
||||
id = ?
|
||||
RETURNING id, creator_id, created_ts, updated_ts, row_status, name, title, content, visibility, tag
|
||||
`
|
||||
memo := &storepb.Memo{}
|
||||
var rowStatus, visibility, tags string
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&memo.Id,
|
||||
&memo.CreatorId,
|
||||
&memo.CreatedTs,
|
||||
&memo.UpdatedTs,
|
||||
&rowStatus,
|
||||
&memo.Name,
|
||||
&memo.Title,
|
||||
&memo.Content,
|
||||
&visibility,
|
||||
&tags,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
memo.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
memo.Visibility = convertVisibilityStringToStorepb(visibility)
|
||||
memo.Tags = filterTags(strings.Split(tags, " "))
|
||||
return memo, nil
|
||||
}
|
||||
|
||||
func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*storepb.Memo, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
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)
|
||||
}
|
||||
if v := find.RowStatus; v != nil {
|
||||
where, args = append(where, "row_status = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.Name; v != nil {
|
||||
where, args = append(where, "name = ?"), append(args, *v)
|
||||
}
|
||||
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("visibility in (%s)", strings.Join(list, ",")))
|
||||
}
|
||||
if v := find.Tag; v != nil {
|
||||
where, args = append(where, "tag LIKE ?"), append(args, "%"+*v+"%")
|
||||
}
|
||||
|
||||
rows, err := d.db.QueryContext(ctx, `
|
||||
SELECT
|
||||
id,
|
||||
creator_id,
|
||||
created_ts,
|
||||
updated_ts,
|
||||
row_status,
|
||||
name,
|
||||
title,
|
||||
content,
|
||||
visibility,
|
||||
tag
|
||||
FROM memo
|
||||
WHERE `+strings.Join(where, " AND ")+`
|
||||
ORDER BY created_ts DESC`,
|
||||
args...,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
list := make([]*storepb.Memo, 0)
|
||||
for rows.Next() {
|
||||
memo := &storepb.Memo{}
|
||||
var rowStatus, visibility, tags string
|
||||
if err := rows.Scan(
|
||||
&memo.Id,
|
||||
&memo.CreatorId,
|
||||
&memo.CreatedTs,
|
||||
&memo.UpdatedTs,
|
||||
&rowStatus,
|
||||
&memo.Name,
|
||||
&memo.Title,
|
||||
&memo.Content,
|
||||
&visibility,
|
||||
&tags,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
memo.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
memo.Visibility = storepb.Visibility(storepb.Visibility_value[visibility])
|
||||
memo.Tags = filterTags(strings.Split(tags, " "))
|
||||
list = append(list, memo)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
|
||||
if _, err := d.db.ExecContext(ctx, `DELETE FROM memo WHERE id = ?`, delete.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func placeholders(n int) string {
|
||||
return strings.Repeat("?,", n-1) + "?"
|
||||
}
|
@ -74,19 +74,3 @@ CREATE TABLE collection (
|
||||
);
|
||||
|
||||
CREATE INDEX idx_collection_name ON collection(name);
|
||||
|
||||
-- memo
|
||||
CREATE TABLE memo (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
creator_id INTEGER NOT NULL,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
content TEXT NOT NULL DEFAULT '',
|
||||
visibility TEXT NOT NULL CHECK (visibility IN ('PRIVATE', 'WORKSPACE', 'PUBLIC')) DEFAULT 'PRIVATE',
|
||||
tag TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
CREATE INDEX idx_memo_name ON memo(name);
|
||||
|
@ -77,7 +77,7 @@ func (d *DB) Migrate(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to read raw database file")
|
||||
}
|
||||
backupDBFilePath := fmt.Sprintf("%s/memos_%s_%d_backup.db", d.profile.Data, d.profile.Version, time.Now().Unix())
|
||||
backupDBFilePath := fmt.Sprintf("%s/slash_%s_%d_backup.db", d.profile.Data, d.profile.Version, time.Now().Unix())
|
||||
if err := os.WriteFile(backupDBFilePath, rawBytes, 0644); err != nil {
|
||||
return errors.Wrap(err, "failed to write raw database file")
|
||||
}
|
||||
|
@ -29,12 +29,6 @@ type Driver interface {
|
||||
ListCollections(ctx context.Context, find *FindCollection) ([]*storepb.Collection, error)
|
||||
DeleteCollection(ctx context.Context, delete *DeleteCollection) error
|
||||
|
||||
// Memo model related methods.
|
||||
CreateMemo(ctx context.Context, create *storepb.Memo) (*storepb.Memo, error)
|
||||
UpdateMemo(ctx context.Context, update *UpdateMemo) (*storepb.Memo, error)
|
||||
ListMemos(ctx context.Context, find *FindMemo) ([]*storepb.Memo, error)
|
||||
DeleteMemo(ctx context.Context, delete *DeleteMemo) error
|
||||
|
||||
// Shortcut model related methods.
|
||||
CreateShortcut(ctx context.Context, create *storepb.Shortcut) (*storepb.Shortcut, error)
|
||||
UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*storepb.Shortcut, error)
|
||||
|
@ -1,60 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
storepb "github.com/yourselfhosted/slash/proto/gen/store"
|
||||
)
|
||||
|
||||
type UpdateMemo struct {
|
||||
ID int32
|
||||
RowStatus *RowStatus
|
||||
Name *string
|
||||
Title *string
|
||||
Content *string
|
||||
Visibility *Visibility
|
||||
Tag *string
|
||||
}
|
||||
|
||||
type FindMemo struct {
|
||||
ID *int32
|
||||
CreatorID *int32
|
||||
RowStatus *RowStatus
|
||||
Name *string
|
||||
VisibilityList []Visibility
|
||||
Tag *string
|
||||
}
|
||||
|
||||
type DeleteMemo struct {
|
||||
ID int32
|
||||
}
|
||||
|
||||
func (s *Store) CreateMemo(ctx context.Context, create *storepb.Memo) (*storepb.Memo, error) {
|
||||
return s.driver.CreateMemo(ctx, create)
|
||||
}
|
||||
|
||||
func (s *Store) UpdateMemo(ctx context.Context, update *UpdateMemo) (*storepb.Memo, error) {
|
||||
return s.driver.UpdateMemo(ctx, update)
|
||||
}
|
||||
|
||||
func (s *Store) ListMemos(ctx context.Context, find *FindMemo) ([]*storepb.Memo, error) {
|
||||
return s.driver.ListMemos(ctx, find)
|
||||
}
|
||||
|
||||
func (s *Store) GetMemo(ctx context.Context, find *FindMemo) (*storepb.Memo, error) {
|
||||
memos, err := s.ListMemos(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(memos) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
memo := memos[0]
|
||||
return memo, nil
|
||||
}
|
||||
|
||||
func (s *Store) DeleteMemo(ctx context.Context, delete *DeleteMemo) error {
|
||||
return s.driver.DeleteMemo(ctx, delete)
|
||||
}
|
Reference in New Issue
Block a user