70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"wherewoof/frontend/internal/db"
|
|
)
|
|
|
|
// locationData is passed to the owner's location page template.
|
|
type locationData struct {
|
|
UpdatedAt pgtype.Timestamptz
|
|
Lat float64
|
|
Lng float64
|
|
FinderPhone string
|
|
TagCode string
|
|
ScannedAt string
|
|
ItemType string
|
|
Name string
|
|
PhotoURL string
|
|
Phone string
|
|
Address string
|
|
}
|
|
|
|
// ServeShort resolves a short code to its scan and renders the owner's
|
|
// location page: "your item was found here" — map embed + finder details +
|
|
// the tag's return 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 {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
data := locationData{
|
|
UpdatedAt: tag.UpdatedAt,
|
|
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"),
|
|
Name: tag.Description.String,
|
|
PhotoURL: tag.PhotoUrl.String,
|
|
Phone: tag.Phone.String,
|
|
Address: tag.Address.String,
|
|
}
|
|
if tag.ItemType.Valid {
|
|
data.ItemType = titleCase(tag.ItemType.String)
|
|
}
|
|
a.render(w, r, "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{}
|