Changeset 0.29.0 (#195)
This commit is contained in:
82
server/store/sqlite/folder.go
Normal file
82
server/store/sqlite/folder.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/store"
|
||||
)
|
||||
|
||||
type FolderStore struct{}
|
||||
|
||||
func NewFolderStore() *FolderStore { return &FolderStore{} }
|
||||
|
||||
func (s *FolderStore) List(ctx context.Context, userID string) ([]models.Folder, error) {
|
||||
rows, err := DB.QueryContext(ctx, `
|
||||
SELECT id, name, parent_id, sort_order, created_at, updated_at
|
||||
FROM folders WHERE user_id = ?
|
||||
ORDER BY sort_order, name
|
||||
`, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []models.Folder
|
||||
for rows.Next() {
|
||||
var f models.Folder
|
||||
if err := rows.Scan(&f.ID, &f.Name, &f.ParentID, &f.SortOrder,
|
||||
st(&f.CreatedAt), st(&f.UpdatedAt)); err != nil {
|
||||
continue
|
||||
}
|
||||
f.UserID = userID
|
||||
result = append(result, f)
|
||||
}
|
||||
if result == nil {
|
||||
result = []models.Folder{}
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (s *FolderStore) Create(ctx context.Context, f *models.Folder) error {
|
||||
f.ID = store.NewID()
|
||||
_, err := DB.ExecContext(ctx, `
|
||||
INSERT INTO folders (id, user_id, name, sort_order)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`, f.ID, f.UserID, f.Name, f.SortOrder)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return DB.QueryRowContext(ctx, `
|
||||
SELECT name, parent_id, sort_order, created_at, updated_at
|
||||
FROM folders WHERE id = ?
|
||||
`, f.ID).Scan(&f.Name, &f.ParentID, &f.SortOrder, st(&f.CreatedAt), st(&f.UpdatedAt))
|
||||
}
|
||||
|
||||
func (s *FolderStore) Update(ctx context.Context, folderID, userID string, name string, sortOrder *int) (int64, error) {
|
||||
res, err := DB.ExecContext(ctx, `
|
||||
UPDATE folders
|
||||
SET name = COALESCE(NULLIF(?, ''), name),
|
||||
sort_order = COALESCE(?, sort_order)
|
||||
WHERE id = ? AND user_id = ?
|
||||
`, name, sortOrder, folderID, userID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
func (s *FolderStore) Delete(ctx context.Context, folderID, userID string) (int64, error) {
|
||||
res, err := DB.ExecContext(ctx,
|
||||
`DELETE FROM folders WHERE id = ? AND user_id = ?`, folderID, userID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
func (s *FolderStore) UnassignChannels(ctx context.Context, folderID, userID string) error {
|
||||
_, err := DB.ExecContext(ctx,
|
||||
`UPDATE channels SET folder_id = NULL WHERE folder_id = ? AND user_id = ?`, folderID, userID)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user