Initial commit
This commit is contained in:
commit
b6014c6ffc
47 files changed
+4329
No files matched your search
@@ -0,0 +1,149 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"vlog/internal/auth"
|
||||
"vlog/internal/models"
|
||||
)
|
||||
|
||||
// ListFuelEntries returns one page of a car's fuel-up history, newest
|
||||
// first. Paginated for the same reason as service entries — no unbounded
|
||||
// fetch regardless of how long the car's been tracked.
|
||||
func (h *Handlers) ListFuelEntries(w http.ResponseWriter, r *http.Request) {
|
||||
car, ok := h.loadOwnedCar(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
limit, offset := parsePagination(r)
|
||||
|
||||
entries := []models.FuelEntry{} // not "var" — empty result must encode as [], not null
|
||||
err := h.dbSelect(r.Context(), &entries, `
|
||||
SELECT * FROM fuel_entries WHERE car_id = ?
|
||||
ORDER BY entry_date DESC, created_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`, car.ID, limit+1, offset)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to list fuel entries")
|
||||
return
|
||||
}
|
||||
|
||||
entries, hasMore := paginate(limit, entries)
|
||||
writeJSON(w, http.StatusOK, page[models.FuelEntry]{Items: entries, HasMore: hasMore})
|
||||
}
|
||||
|
||||
// CreateFuelEntry adds a fuel-up to a car.
|
||||
func (h *Handlers) CreateFuelEntry(w http.ResponseWriter, r *http.Request) {
|
||||
car, ok := h.loadOwnedCar(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
in, entryDate, ok := decodeFuelEntryInput(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var entry models.FuelEntry
|
||||
err := h.dbGet(r.Context(), &entry, `
|
||||
INSERT INTO fuel_entries (id, car_id, entry_date, litres, cost, odometer, location, notes)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
RETURNING *
|
||||
`, uuid.New().String(), car.ID, entryDate, in.Litres, in.Cost, in.Odometer, in.Location, in.Notes)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to create fuel entry")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, entry)
|
||||
}
|
||||
|
||||
// UpdateFuelEntry edits a fuel entry's fields.
|
||||
func (h *Handlers) UpdateFuelEntry(w http.ResponseWriter, r *http.Request) {
|
||||
entry, ok := h.loadOwnedFuelEntry(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
in, entryDate, ok := decodeFuelEntryInput(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var updated models.FuelEntry
|
||||
err := h.dbGet(r.Context(), &updated, `
|
||||
UPDATE fuel_entries
|
||||
SET entry_date = ?, litres = ?, cost = ?, odometer = ?, location = ?, notes = ?
|
||||
WHERE id = ?
|
||||
RETURNING *
|
||||
`, entryDate, in.Litres, in.Cost, in.Odometer, in.Location, in.Notes, entry.ID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to update fuel entry")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, updated)
|
||||
}
|
||||
|
||||
// DeleteFuelEntry removes a fuel entry.
|
||||
func (h *Handlers) DeleteFuelEntry(w http.ResponseWriter, r *http.Request) {
|
||||
entry, ok := h.loadOwnedFuelEntry(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if _, err := h.dbExec(r.Context(), `DELETE FROM fuel_entries WHERE id = ?`, entry.ID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to delete fuel entry")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]bool{"ok": true})
|
||||
}
|
||||
|
||||
func decodeFuelEntryInput(w http.ResponseWriter, r *http.Request) (models.FuelEntryInput, time.Time, bool) {
|
||||
var in models.FuelEntryInput
|
||||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
return in, time.Time{}, false
|
||||
}
|
||||
if in.Litres <= 0 {
|
||||
writeError(w, http.StatusBadRequest, "litres must be greater than 0")
|
||||
return in, time.Time{}, false
|
||||
}
|
||||
if in.Cost <= 0 {
|
||||
writeError(w, http.StatusBadRequest, "cost must be greater than 0")
|
||||
return in, time.Time{}, false
|
||||
}
|
||||
entryDate, err := time.Parse(dateLayout, in.EntryDate)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid entryDate")
|
||||
return in, time.Time{}, false
|
||||
}
|
||||
return in, entryDate, true
|
||||
}
|
||||
|
||||
func (h *Handlers) loadOwnedFuelEntry(w http.ResponseWriter, r *http.Request) (*models.FuelEntry, bool) {
|
||||
userID, _ := auth.UserIDFromContext(r.Context())
|
||||
entryID := r.PathValue("entryId")
|
||||
|
||||
var entry models.FuelEntry
|
||||
err := h.dbGet(r.Context(), &entry, `
|
||||
SELECT fe.*
|
||||
FROM fuel_entries fe
|
||||
JOIN cars c ON c.id = fe.car_id
|
||||
WHERE fe.id = ? AND c.user_id = ?
|
||||
`, entryID, userID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
writeError(w, http.StatusNotFound, "fuel entry not found")
|
||||
return nil, false
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to load fuel entry")
|
||||
return nil, false
|
||||
}
|
||||
return &entry, true
|
||||
}
|
||||
Reference in new issue
Block a user