| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "github.com/stevedylandev/andromeda/pkg/web" |
| 7 | ) |
| 8 | |
| 9 | func (a *App) apiListWines(w http.ResponseWriter, r *http.Request) { |
| 10 | wines, err := getCellarWines(a.DB) |
| 11 | if err != nil { |
| 12 | a.Log.Error("api list wines", "err", err) |
| 13 | w.WriteHeader(http.StatusInternalServerError) |
| 14 | return |
| 15 | } |
| 16 | web.WriteJSON(w, http.StatusOK, wines) |
| 17 | } |
| 18 | |
| 19 | func (a *App) apiGetWine(w http.ResponseWriter, r *http.Request) { |
| 20 | wine, err := getWineByShortID(a.DB, r.PathValue("short_id")) |
| 21 | if err != nil { |
| 22 | w.WriteHeader(http.StatusInternalServerError) |
| 23 | return |
| 24 | } |
| 25 | if wine == nil { |
| 26 | http.NotFound(w, r) |
| 27 | return |
| 28 | } |
| 29 | web.WriteJSON(w, http.StatusOK, wine) |
| 30 | } |
| 31 | |
| 32 | func (a *App) apiPentagonSVG(w http.ResponseWriter, r *http.Request) { |
| 33 | wine, err := getWineByShortID(a.DB, r.PathValue("short_id")) |
| 34 | if err != nil { |
| 35 | w.WriteHeader(http.StatusInternalServerError) |
| 36 | return |
| 37 | } |
| 38 | if wine == nil { |
| 39 | http.NotFound(w, r) |
| 40 | return |
| 41 | } |
| 42 | svg := buildPentagonSVG(wine.Sweetness, wine.Acidity, wine.Tannin, wine.Alcohol, wine.Body, 250.0, true) |
| 43 | w.Header().Set("Content-Type", "image/svg+xml") |
| 44 | _, _ = w.Write([]byte(svg)) |
| 45 | } |
| 46 | |
| 47 | func (a *App) apiBarsSVG(w http.ResponseWriter, r *http.Request) { |
| 48 | wine, err := getWineByShortID(a.DB, r.PathValue("short_id")) |
| 49 | if err != nil { |
| 50 | w.WriteHeader(http.StatusInternalServerError) |
| 51 | return |
| 52 | } |
| 53 | if wine == nil { |
| 54 | http.NotFound(w, r) |
| 55 | return |
| 56 | } |
| 57 | svg := buildBarsSVG(wine.Clarity, wine.ColorIntensity, wine.AromaIntensity, wine.NoseComplexity, 250.0) |
| 58 | w.Header().Set("Content-Type", "image/svg+xml") |
| 59 | _, _ = w.Write([]byte(svg)) |
| 60 | } |