init
This commit is contained in:
commit
b38c39030b
22 changed files with 1925 additions and 0 deletions
51
internal/app/workspace_service.go
Normal file
51
internal/app/workspace_service.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (a *App) listWorkspaces(ctx context.Context, userID string) ([]Workspace, error) {
|
||||
rows, err := a.db.QueryContext(ctx, `SELECT id, user_id, name, unix_user FROM workspaces WHERE user_id=$1 ORDER BY name`, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Workspace
|
||||
for rows.Next() {
|
||||
var w Workspace
|
||||
if err := rows.Scan(&w.ID, &w.UserID, &w.Name, &w.UnixUser); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, w)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (a *App) createWorkspace(ctx context.Context, userID, name, unixUser string) (Workspace, error) {
|
||||
var w Workspace
|
||||
err := a.db.QueryRowContext(ctx, `INSERT INTO workspaces (user_id, name, unix_user) VALUES ($1,$2,$3) RETURNING id, user_id, name, unix_user`, userID, name, unixUser).
|
||||
Scan(&w.ID, &w.UserID, &w.Name, &w.UnixUser)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "duplicate key") {
|
||||
return Workspace{}, errors.New("workspace name or unix_user already exists")
|
||||
}
|
||||
return Workspace{}, err
|
||||
}
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func (a *App) getWorkspace(ctx context.Context, userID string, workspaceID int64) (Workspace, error) {
|
||||
var w Workspace
|
||||
err := a.db.QueryRowContext(ctx, `SELECT id, user_id, name, unix_user FROM workspaces WHERE id=$1 AND user_id=$2`, workspaceID, userID).
|
||||
Scan(&w.ID, &w.UserID, &w.Name, &w.UnixUser)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Workspace{}, errors.New("workspace not found")
|
||||
}
|
||||
return Workspace{}, err
|
||||
}
|
||||
return w, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue