This commit is contained in:
@@ -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