Changeset 0.29.0 (#195)
This commit is contained in:
75
server/store/postgres/folder.go
Normal file
75
server/store/postgres/folder.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
)
|
||||
|
||||
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 = $1
|
||||
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,
|
||||
&f.CreatedAt, &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 {
|
||||
return DB.QueryRowContext(ctx, `
|
||||
INSERT INTO folders (user_id, name, sort_order)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, name, parent_id, sort_order, created_at, updated_at
|
||||
`, f.UserID, f.Name, f.SortOrder).Scan(
|
||||
&f.ID, &f.Name, &f.ParentID, &f.SortOrder, &f.CreatedAt, &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($3, ''), name),
|
||||
sort_order = COALESCE($4, sort_order)
|
||||
WHERE id = $1 AND user_id = $2
|
||||
`, folderID, userID, name, sortOrder)
|
||||
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 = $1 AND user_id = $2`, 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 = $1 AND user_id = $2`, folderID, userID)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user