aboutsummaryrefslogtreecommitdiff
path: root/web/preview.go
diff options
context:
space:
mode:
Diffstat (limited to 'web/preview.go')
-rw-r--r--web/preview.go25
1 files changed, 22 insertions, 3 deletions
diff --git a/web/preview.go b/web/preview.go
index 2c36b71..88978b8 100644
--- a/web/preview.go
+++ b/web/preview.go
@@ -15,6 +15,7 @@ import (
"github.com/martinlehoux/biking_home/mountain_pass"
"github.com/martinlehoux/biking_home/official_climb"
"github.com/martinlehoux/biking_home/ride"
+ "github.com/martinlehoux/biking_home/rideanalysis"
"github.com/martinlehoux/kagamigo/kcore"
)
@@ -53,7 +54,7 @@ func (s *Server) handlePreview(w http.ResponseWriter, r *http.Request) {
s.renderPreviewForm(w, r, "The file could not be parsed as a ride (GPX without elevation or an unsupported format?).")
return
}
- passes, err := mountain_pass.LoadMountainPassesAroundRide(s.db, parsed.Coords, rideDetailMountainPassMarginM)
+ passes, err := mountain_pass.LoadMountainPassesAroundRide(s.db, parsed.Coords, rideanalysis.PassMarginM)
if err != nil {
slog.Warn("Failed to load nearby mountain passes for preview", "error", err)
}
@@ -82,8 +83,7 @@ func rideParserFor(filename string) (ride.RideParser, error) {
}
}
-// buildPreviewRideView derives the ride summary metrics from the parsed route,
-// the same values ride.Save persists for imported rides.
+// buildPreviewRideView derives the ride summary metrics from the parsed route, the same values ride.Save persists.
func buildPreviewRideView(filename string, parsed ride.Ride) RideView {
distanceM := parsed.Distances[len(parsed.Distances)-1]
elevationGainM := 0.0
@@ -151,10 +151,16 @@ func (s *Server) handlePreviewOfficialClimb(w http.ResponseWriter, r *http.Reque
writePreviewSaveError(w, "valid end coordinates are required")
return
}
+ sample, err := parseSampleForm(r)
+ if err != nil {
+ writePreviewSaveError(w, "a valid recorded sample is required")
+ return
+ }
created, err := official_climb.Create(s.db, official_climb.OfficialClimb{
Name: r.FormValue("name"),
StartCoord: startCoord,
EndCoord: endCoord,
+ Sample: sample,
})
if err != nil {
slog.Warn("Failed to create official climb from preview", "error", err)
@@ -166,6 +172,19 @@ func (s *Server) handlePreviewOfficialClimb(w http.ResponseWriter, r *http.Reque
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "name": created.Name})
}
+// parseSampleForm reads the sample the preview client computed for the selected boundaries.
+func parseSampleForm(r *http.Request) (climb.SegmentSample, error) {
+ distanceM, err := strconv.ParseFloat(r.FormValue("sample_distance_m"), 64)
+ if err != nil {
+ return climb.SegmentSample{}, err
+ }
+ elevationGainM, err := strconv.ParseFloat(r.FormValue("sample_elevation_gain_m"), 64)
+ if err != nil {
+ return climb.SegmentSample{}, err
+ }
+ return climb.SegmentSample{DistanceM: distanceM, ElevationGainM: elevationGainM}, nil
+}
+
func parseCoordForm(r *http.Request, prefix string) (coord geodist.Coord, err error) {
lat, err := strconv.ParseFloat(r.FormValue(prefix+"_lat"), 64)
if err != nil {