fix: address code review findings

This commit is contained in:
2026-08-17 23:12:24 +03:00
parent 6dcec6a7a5
commit 9b89b7b9ab
2 changed files with 18 additions and 9 deletions

View File

@@ -438,7 +438,7 @@ func adminAuth(hc *HandlerContext, w http.ResponseWriter, r *http.Request) bool
}
providedAPIKey := r.Header.Get("X-Admin-Api-Key")
if !hmac.Equal([]byte(providedAPIKey), []byte(expectedAPIKey)) {
http.Error(w, "unauthorized: admin API key required", http.StatusUnauthorized)
http.Error(w, "unauthorized: invalid admin API key", http.StatusUnauthorized)
return false
}
return true
@@ -574,11 +574,13 @@ func RemoveSavedCity(hc *HandlerContext, w http.ResponseWriter, r *http.Request)
userID = "default"
}
cityCode := strings.TrimPrefix(r.URL.Path, "/v1/preferences/saved-cities/")
if cityCode == "" || cityCode == "/v1/preferences/saved-cities/" {
parts := strings.Split(r.URL.Path, "/")
// Expected: /v1/preferences/saved-cities/{city_code} -> parts: ["", "v1", "preferences", "saved-cities", "{city_code}"]
if len(parts) < 5 {
http.Error(w, "missing city code", http.StatusBadRequest)
return
}
cityCode := parts[4]
if err := hc.Preferences.RemoveSavedCity(r.Context(), userID, cityCode); err != nil {
http.Error(w, "failed to remove saved city: "+err.Error(), http.StatusInternalServerError)
@@ -628,6 +630,12 @@ func AddSearchHistory(hc *HandlerContext, w http.ResponseWriter, r *http.Request
return
}
// Validate input length
if len(req.FromCity) > 100 || len(req.ToCity) > 100 || len(req.Date) > 20 {
http.Error(w, "invalid city or date format", http.StatusBadRequest)
return
}
if err := hc.Preferences.AddSearchHistory(r.Context(), userID, req.FromCity, req.ToCity, req.Date); err != nil {
http.Error(w, "failed to add search history: "+err.Error(), http.StatusInternalServerError)
return