| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "github.com/stevedylandev/andromeda/pkg/web" |
| 7 | ) |
| 8 | |
| 9 | type apiArtwork struct { |
| 10 | Date string `json:"date"` |
| 11 | ArtworkID int64 `json:"artwork_id"` |
| 12 | Title string `json:"title"` |
| 13 | ArtistDisplay *string `json:"artist_display,omitempty"` |
| 14 | DateDisplay *string `json:"date_display,omitempty"` |
| 15 | MediumDisplay *string `json:"medium_display,omitempty"` |
| 16 | Dimensions *string `json:"dimensions,omitempty"` |
| 17 | PlaceOfOrigin *string `json:"place_of_origin,omitempty"` |
| 18 | CreditLine *string `json:"credit_line,omitempty"` |
| 19 | ShortDescription *string `json:"short_description,omitempty"` |
| 20 | ImageID string `json:"image_id"` |
| 21 | ImageURL string `json:"image_url"` |
| 22 | SourceURL string `json:"source_url"` |
| 23 | } |
| 24 | |
| 25 | func toAPI(a DailyArtwork) apiArtwork { |
| 26 | out := apiArtwork{ |
| 27 | Date: a.Date, |
| 28 | ArtworkID: a.ArtworkID, |
| 29 | Title: a.Title, |
| 30 | ImageID: a.ImageID, |
| 31 | ImageURL: iiifURL(a.ImageID), |
| 32 | SourceURL: sourceURL(a.ArtworkID), |
| 33 | } |
| 34 | if a.ArtistDisplay.Valid { |
| 35 | v := a.ArtistDisplay.String |
| 36 | out.ArtistDisplay = &v |
| 37 | } |
| 38 | if a.DateDisplay.Valid { |
| 39 | v := a.DateDisplay.String |
| 40 | out.DateDisplay = &v |
| 41 | } |
| 42 | if a.MediumDisplay.Valid { |
| 43 | v := a.MediumDisplay.String |
| 44 | out.MediumDisplay = &v |
| 45 | } |
| 46 | if a.Dimensions.Valid { |
| 47 | v := a.Dimensions.String |
| 48 | out.Dimensions = &v |
| 49 | } |
| 50 | if a.PlaceOfOrigin.Valid { |
| 51 | v := a.PlaceOfOrigin.String |
| 52 | out.PlaceOfOrigin = &v |
| 53 | } |
| 54 | if a.CreditLine.Valid { |
| 55 | v := a.CreditLine.String |
| 56 | out.CreditLine = &v |
| 57 | } |
| 58 | if a.ShortDescription.Valid { |
| 59 | v := a.ShortDescription.String |
| 60 | out.ShortDescription = &v |
| 61 | } |
| 62 | return out |
| 63 | } |
| 64 | |
| 65 | func (a *App) apiToday(w http.ResponseWriter, r *http.Request) { |
| 66 | d, err := getDaily(a.DB, a.todayInTZ()) |
| 67 | if err != nil { |
| 68 | a.Log.Error("api_today db error", "err", err) |
| 69 | w.WriteHeader(http.StatusInternalServerError) |
| 70 | return |
| 71 | } |
| 72 | if d == nil { |
| 73 | web.WriteError(w, http.StatusNotFound, "today not yet populated") |
| 74 | return |
| 75 | } |
| 76 | web.WriteJSON(w, http.StatusOK, toAPI(*d)) |
| 77 | } |
| 78 | |
| 79 | func (a *App) apiDay(w http.ResponseWriter, r *http.Request) { |
| 80 | date := r.PathValue("date") |
| 81 | if _, ok := parseDate(date); !ok { |
| 82 | web.WriteError(w, http.StatusBadRequest, "invalid date format") |
| 83 | return |
| 84 | } |
| 85 | if date > a.todayInTZ() { |
| 86 | web.WriteError(w, http.StatusNotFound, "future date") |
| 87 | return |
| 88 | } |
| 89 | d, err := getDaily(a.DB, date) |
| 90 | if err != nil { |
| 91 | a.Log.Error("api_day db error", "err", err) |
| 92 | w.WriteHeader(http.StatusInternalServerError) |
| 93 | return |
| 94 | } |
| 95 | if d == nil { |
| 96 | web.WriteError(w, http.StatusNotFound, "no record for date") |
| 97 | return |
| 98 | } |
| 99 | web.WriteJSON(w, http.StatusOK, toAPI(*d)) |
| 100 | } |
| 101 | |
| 102 | func (a *App) apiArchive(w http.ResponseWriter, r *http.Request) { |
| 103 | items, err := listDaily(a.DB, 1000) |
| 104 | if err != nil { |
| 105 | a.Log.Error("api_archive db error", "err", err) |
| 106 | w.WriteHeader(http.StatusInternalServerError) |
| 107 | return |
| 108 | } |
| 109 | out := make([]apiArtwork, 0, len(items)) |
| 110 | for _, it := range items { |
| 111 | out = append(out, toAPI(it)) |
| 112 | } |
| 113 | web.WriteJSON(w, http.StatusOK, out) |
| 114 | } |