This commit is contained in:
@@ -149,3 +149,37 @@ type DeployResult struct {
|
||||
Success bool `json:"success" example:"true" description:"Whether deployment succeeded"`
|
||||
Error string `json:"error,omitempty" example:"" description:"Error message if deployment failed"`
|
||||
}
|
||||
|
||||
// Script represents a stored script with path and interpreter binding.
|
||||
type Script struct {
|
||||
ID int64 `json:"id"`
|
||||
Path string `json:"path"`
|
||||
Content string `json:"content"`
|
||||
InterpreterID int64 `json:"interpreter_id"`
|
||||
CreatedAt *string `json:"created_at"`
|
||||
UpdatedAt *string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// ScriptCreate is the request body for creating a script.
|
||||
type ScriptCreate struct {
|
||||
Path string `json:"path" binding:"required"`
|
||||
Content string `json:"content"`
|
||||
InterpreterID int64 `json:"interpreter_id" binding:"required"`
|
||||
}
|
||||
|
||||
// ScriptUpdate is the request body for updating a script.
|
||||
type ScriptUpdate struct {
|
||||
Path *string `json:"path"`
|
||||
Content *string `json:"content"`
|
||||
InterpreterID *int64 `json:"interpreter_id"`
|
||||
}
|
||||
|
||||
// ScriptTreeNode represents a node in the script directory tree.
|
||||
type ScriptTreeNode struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"` // "folder" or "file"
|
||||
Children []ScriptTreeNode `json:"children,omitempty"`
|
||||
ID *int64 `json:"id,omitempty"`
|
||||
Content *string `json:"content,omitempty"`
|
||||
InterpreterID *int64 `json:"interpreter_id,omitempty"`
|
||||
}
|
||||
|
||||
@@ -507,3 +507,134 @@ func (r *Repository) UpdatePassword(login string, newPassword string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateScript inserts a new script into the database.
|
||||
func (r *Repository) CreateScript(sc ScriptCreate) (*Script, error) {
|
||||
result, err := r.DB.Exec(
|
||||
`INSERT INTO scripts (path, content, interpreter_id) VALUES (?, ?, ?)`,
|
||||
sc.Path, sc.Content, sc.InterpreterID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("insert script: %w", err)
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get last insert id: %w", err)
|
||||
}
|
||||
|
||||
return &Script{
|
||||
ID: id,
|
||||
Path: sc.Path,
|
||||
Content: sc.Content,
|
||||
InterpreterID: sc.InterpreterID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetScript retrieves a script by ID.
|
||||
func (r *Repository) GetScript(id int64) (*Script, error) {
|
||||
var s Script
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT id, path, content, interpreter_id, created_at, updated_at FROM scripts WHERE id = ?`,
|
||||
id,
|
||||
).Scan(&s.ID, &s.Path, &s.Content, &s.InterpreterID, &s.CreatedAt, &s.UpdatedAt)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// GetScriptByPath retrieves a script by its path.
|
||||
func (r *Repository) GetScriptByPath(path string) (*Script, error) {
|
||||
var s Script
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT id, path, content, interpreter_id, created_at, updated_at FROM scripts WHERE path = ?`,
|
||||
path,
|
||||
).Scan(&s.ID, &s.Path, &s.Content, &s.InterpreterID, &s.CreatedAt, &s.UpdatedAt)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// ListScripts returns all scripts.
|
||||
func (r *Repository) ListScripts() ([]Script, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT id, path, content, interpreter_id, created_at, updated_at FROM scripts`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var scripts []Script
|
||||
for rows.Next() {
|
||||
var s Script
|
||||
if err := rows.Scan(&s.ID, &s.Path, &s.Content, &s.InterpreterID, &s.CreatedAt, &s.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scripts = append(scripts, s)
|
||||
}
|
||||
return scripts, rows.Err()
|
||||
}
|
||||
|
||||
// UpdateScript updates a script by ID.
|
||||
func (r *Repository) UpdateScript(id int64, update ScriptUpdate) (*Script, error) {
|
||||
existing, err := r.GetScript(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newPath := existing.Path
|
||||
newContent := existing.Content
|
||||
newInterpreterID := existing.InterpreterID
|
||||
|
||||
if update.Path != nil {
|
||||
newPath = *update.Path
|
||||
}
|
||||
if update.Content != nil {
|
||||
newContent = *update.Content
|
||||
}
|
||||
if update.InterpreterID != nil {
|
||||
newInterpreterID = *update.InterpreterID
|
||||
}
|
||||
|
||||
_, err = r.DB.Exec(
|
||||
`UPDATE scripts SET path = ?, content = ?, interpreter_id = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?`,
|
||||
newPath, newContent, newInterpreterID, id,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("update script: %w", err)
|
||||
}
|
||||
|
||||
return &Script{
|
||||
ID: id,
|
||||
Path: newPath,
|
||||
Content: newContent,
|
||||
InterpreterID: newInterpreterID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DeleteScript deletes a script by ID.
|
||||
func (r *Repository) DeleteScript(id int64) error {
|
||||
result, err := r.DB.Exec(`DELETE FROM scripts WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user