1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package web
import (
"log/slog"
"net/http"
"strconv"
"github.com/martinlehoux/biking_home/ride"
)
func (s *Server) handleUpdateRideCategories(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil || id <= 0 {
http.NotFound(w, r)
return
}
item, found, err := ride.GetByID(s.db, id)
if err != nil {
slog.Error("Failed to load ride for category update", "ride_id", id, "error", err)
http.Error(w, "failed to load ride", http.StatusInternalServerError)
return
}
if !found {
http.NotFound(w, r)
return
}
_ = item
if err := r.ParseForm(); err != nil {
http.Error(w, "invalid form", http.StatusBadRequest)
return
}
bikeType := ride.BikeType(r.FormValue("bike_type"))
rideType := ride.RideType(r.FormValue("ride_type"))
if !ride.IsValidBikeType(string(bikeType)) {
http.Error(w, "invalid bike_type", http.StatusBadRequest)
return
}
if !ride.IsValidRideType(string(rideType)) {
http.Error(w, "invalid ride_type", http.StatusBadRequest)
return
}
if err := ride.UpdateCategories(s.db, id, bikeType, rideType); err != nil {
slog.Error("Failed to update ride categories", "ride_id", id, "error", err)
http.Error(w, "failed to update categories", http.StatusInternalServerError)
return
}
slog.Info("Updated ride categories", "ride_id", id, "bike_type", bikeType, "ride_type", rideType)
http.Redirect(w, r, rideDetailURL(id)+"?categories=updated", http.StatusSeeOther)
}
func rideCategoriesUpdateURL(id int64) string {
return rideDetailURL(id) + "/categories"
}
var bikeTypeOptions = []ride.BikeType{ride.BikeTypeRoad, ride.BikeTypeMTB}
var rideTypeOptions = []ride.RideType{ride.RideTypeTraining, ride.RideTypeIndoor, ride.RideTypeRace, ride.RideTypeTransport, ride.RideTypeTouring}
var bikeTypeLabels = map[ride.BikeType]string{
ride.BikeTypeRoad: "Road",
ride.BikeTypeMTB: "MTB",
}
var rideTypeLabels = map[ride.RideType]string{
ride.RideTypeTraining: "Training",
ride.RideTypeIndoor: "Indoor",
ride.RideTypeRace: "Race",
ride.RideTypeTransport: "Transport",
ride.RideTypeTouring: "Touring",
}
func formatCategoryOrEmpty(value string) string {
if value == "" {
return "—"
}
return value
}
|