Files
where_woof/frontend/internal/handlers/location.go

60 lines
1.4 KiB
Go

package handlers
import (
"errors"
"net/http"
"github.com/jackc/pgx/v5"
"wherewoof/frontend/internal/db"
)
// locationData is passed to the owner's location page template.
type locationData struct {
Lat float64
Lng float64
FinderPhone string
TagCode string
ScannedAt string
}
// ServeShort resolves a short code to its scan and renders the owner's
// location page (map embed + finder details).
func (a *App) ServeShort(w http.ResponseWriter, r *http.Request) {
code := r.PathValue("code")
sc, err := a.Queries.GetShortCode(r.Context(), code)
if err != nil {
http.NotFound(w, r)
return
}
scan, err := a.Queries.GetScanByID(r.Context(), sc.ScanID)
if err != nil || !scan.Lat.Valid || !scan.Lng.Valid {
http.NotFound(w, r)
return
}
tag, err := a.Queries.GetTagByID(r.Context(), scan.TagID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
http.NotFound(w, r)
return
}
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
data := locationData{
Lat: scan.Lat.Float64,
Lng: scan.Lng.Float64,
FinderPhone: scan.ScannerPhone.String,
TagCode: tag.TagCode,
ScannedAt: scan.ScannedAt.Time.Local().Format("Mon 2 Jan, 3:04 pm"),
}
a.render(w, r, "location", "Location found", data, "")
}
// keep the db import referenced (Tag lookup above uses a.Queries only; this
// guards against accidental removal while the package evolves).
var _ = db.Tag{}