CodeBench: kompaktoitu Go handlers.go golden — error handling yhdelle riville

This commit is contained in:
2026-04-14 21:15:57 +03:00
parent 5b8919ef89
commit 7d49d62f81

View File

@@ -71,7 +71,7 @@ type UpdateTodo struct {
## handlers.go ## handlers.go
CRUD handlers as closures taking *sql.DB. INSERT/UPDATE use RETURNING, sql.ErrNoRows for 404. CRUD handlers as closures taking *sql.DB. Key patterns: INSERT RETURNING, sql.ErrNoRows for 404, RowsAffected for delete.
```go ```go
package main package main
@@ -81,67 +81,51 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"strconv" "strconv"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
// POST — decode JSON, defaults with nil-check, INSERT RETURNING, StatusCreated.
func createTodo(db *sql.DB) http.HandlerFunc { func createTodo(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
var input CreateTodo var input CreateTodo
if err := json.NewDecoder(r.Body).Decode(&input); err != nil { if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest); return
return
} }
priority := int64(1) priority := int64(1)
if input.Priority != nil { if input.Priority != nil { priority = *input.Priority }
priority = *input.Priority
}
status := "pending" status := "pending"
if input.Status != nil { if input.Status != nil { status = *input.Status }
status = *input.Status
}
var todo Todo var todo Todo
err := db.QueryRow( err := db.QueryRow(
`INSERT INTO todos (title, description, due_date, priority, status) `INSERT INTO todos (title, description, due_date, priority, status)
VALUES (?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?) RETURNING id, title, description, due_date, priority, status`,
RETURNING id, title, description, due_date, priority, status`,
input.Title, input.Description, input.DueDate, priority, status, input.Title, input.Description, input.DueDate, priority, status,
).Scan(&todo.ID, &todo.Title, &todo.Description, &todo.DueDate, &todo.Priority, &todo.Status) ).Scan(&todo.ID, &todo.Title, &todo.Description, &todo.DueDate, &todo.Priority, &todo.Status)
if err != nil { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError); return }
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(todo) json.NewEncoder(w).Encode(todo)
} }
} }
// GET list — db.Query + rows.Scan loop, empty slice not nil.
func listTodos(db *sql.DB) http.HandlerFunc { func listTodos(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("SELECT id, title, description, due_date, priority, status FROM todos") rows, err := db.Query("SELECT id, title, description, due_date, priority, status FROM todos")
if err != nil { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError); return }
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close() defer rows.Close()
var todos []Todo todos := []Todo{}
for rows.Next() { for rows.Next() {
var t Todo var t Todo
if err := rows.Scan(&t.ID, &t.Title, &t.Description, &t.DueDate, &t.Priority, &t.Status); err != nil { rows.Scan(&t.ID, &t.Title, &t.Description, &t.DueDate, &t.Priority, &t.Status)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
todos = append(todos, t) todos = append(todos, t)
} }
if todos == nil {
todos = []Todo{}
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(todos) json.NewEncoder(w).Encode(todos)
} }
} }
// GET by id — QueryRow + sql.ErrNoRows → 404.
func getTodo(db *sql.DB) http.HandlerFunc { func getTodo(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
@@ -149,83 +133,51 @@ func getTodo(db *sql.DB) http.HandlerFunc {
err := db.QueryRow( err := db.QueryRow(
"SELECT id, title, description, due_date, priority, status FROM todos WHERE id = ?", id, "SELECT id, title, description, due_date, priority, status FROM todos WHERE id = ?", id,
).Scan(&todo.ID, &todo.Title, &todo.Description, &todo.DueDate, &todo.Priority, &todo.Status) ).Scan(&todo.ID, &todo.Title, &todo.Description, &todo.DueDate, &todo.Priority, &todo.Status)
if err == sql.ErrNoRows { if err == sql.ErrNoRows { http.Error(w, "not found", http.StatusNotFound); return }
http.Error(w, "not found", http.StatusNotFound) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError); return }
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(todo) json.NewEncoder(w).Encode(todo)
} }
} }
// PUT — fetch existing, merge with input nil-checks, UPDATE RETURNING.
func updateTodo(db *sql.DB) http.HandlerFunc { func updateTodo(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
var existing Todo var existing Todo
err := db.QueryRow( err := db.QueryRow("SELECT id, title, description, due_date, priority, status FROM todos WHERE id = ?", id,
"SELECT id, title, description, due_date, priority, status FROM todos WHERE id = ?", id,
).Scan(&existing.ID, &existing.Title, &existing.Description, &existing.DueDate, &existing.Priority, &existing.Status) ).Scan(&existing.ID, &existing.Title, &existing.Description, &existing.DueDate, &existing.Priority, &existing.Status)
if err == sql.ErrNoRows { if err == sql.ErrNoRows { http.Error(w, "not found", http.StatusNotFound); return }
http.Error(w, "not found", http.StatusNotFound) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError); return }
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var input UpdateTodo var input UpdateTodo
if err := json.NewDecoder(r.Body).Decode(&input); err != nil { if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest); return
return
}
if input.Title != nil {
existing.Title = *input.Title
}
if input.Description != nil {
existing.Description = input.Description
}
if input.DueDate != nil {
existing.DueDate = input.DueDate
}
if input.Priority != nil {
existing.Priority = *input.Priority
}
if input.Status != nil {
existing.Status = *input.Status
} }
if input.Title != nil { existing.Title = *input.Title }
if input.Description != nil { existing.Description = input.Description }
if input.DueDate != nil { existing.DueDate = input.DueDate }
if input.Priority != nil { existing.Priority = *input.Priority }
if input.Status != nil { existing.Status = *input.Status }
var updated Todo var updated Todo
err = db.QueryRow( err = db.QueryRow(
`UPDATE todos SET title = ?, description = ?, due_date = ?, priority = ?, status = ? `UPDATE todos SET title=?, description=?, due_date=?, priority=?, status=? WHERE id=?
WHERE id = ?
RETURNING id, title, description, due_date, priority, status`, RETURNING id, title, description, due_date, priority, status`,
existing.Title, existing.Description, existing.DueDate, existing.Priority, existing.Status, id, existing.Title, existing.Description, existing.DueDate, existing.Priority, existing.Status, id,
).Scan(&updated.ID, &updated.Title, &updated.Description, &updated.DueDate, &updated.Priority, &updated.Status) ).Scan(&updated.ID, &updated.Title, &updated.Description, &updated.DueDate, &updated.Priority, &updated.Status)
if err != nil { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError); return }
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(updated) json.NewEncoder(w).Encode(updated)
} }
} }
// DELETE — Exec + RowsAffected == 0 → 404, else 204.
func deleteTodo(db *sql.DB) http.HandlerFunc { func deleteTodo(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
result, err := db.Exec("DELETE FROM todos WHERE id = ?", id) result, err := db.Exec("DELETE FROM todos WHERE id = ?", id)
if err != nil { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError); return }
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rows, _ := result.RowsAffected() rows, _ := result.RowsAffected()
if rows == 0 { if rows == 0 { http.Error(w, "not found", http.StatusNotFound); return }
http.Error(w, "not found", http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} }
} }