|
7 | 7 | "errors" |
8 | 8 | "log" |
9 | 9 | "net/http" |
| 10 | + "strconv" |
10 | 11 | "time" |
11 | 12 |
|
12 | 13 | "github.com/google/uuid" |
@@ -97,6 +98,47 @@ func (s *Server) handleIngestBatch(w http.ResponseWriter, r *http.Request) { |
97 | 98 | writeJSON(w, http.StatusCreated, map[string]any{"count": len(receipts), "receipts": receipts}) |
98 | 99 | } |
99 | 100 |
|
| 101 | +// ListReceiptsResponse is returned by GET /v1/receipts. |
| 102 | +type ListReceiptsResponse struct { |
| 103 | + Receipts []vur.Receipt `json:"receipts"` |
| 104 | + Offset int `json:"offset"` |
| 105 | + Limit int `json:"limit"` |
| 106 | + Count int `json:"count"` |
| 107 | +} |
| 108 | + |
| 109 | +func (s *Server) handleListReceipts(w http.ResponseWriter, r *http.Request) { |
| 110 | + const defaultLimit = 50 |
| 111 | + const maxLimit = 500 |
| 112 | + |
| 113 | + offset, _ := strconv.Atoi(r.URL.Query().Get("offset")) |
| 114 | + limit, _ := strconv.Atoi(r.URL.Query().Get("limit")) |
| 115 | + if limit <= 0 { |
| 116 | + limit = defaultLimit |
| 117 | + } |
| 118 | + if limit > maxLimit { |
| 119 | + limit = maxLimit |
| 120 | + } |
| 121 | + if offset < 0 { |
| 122 | + offset = 0 |
| 123 | + } |
| 124 | + |
| 125 | + stored, err := s.store.ListReceipts(offset, limit) |
| 126 | + if err != nil { |
| 127 | + writeError(w, http.StatusInternalServerError, "list receipts: "+err.Error()) |
| 128 | + return |
| 129 | + } |
| 130 | + receipts := make([]vur.Receipt, len(stored)) |
| 131 | + for i, sr := range stored { |
| 132 | + receipts[i] = sr.Receipt |
| 133 | + } |
| 134 | + writeJSON(w, http.StatusOK, ListReceiptsResponse{ |
| 135 | + Receipts: receipts, |
| 136 | + Offset: offset, |
| 137 | + Limit: limit, |
| 138 | + Count: len(receipts), |
| 139 | + }) |
| 140 | +} |
| 141 | + |
100 | 142 | func (s *Server) handleGetReceipt(w http.ResponseWriter, r *http.Request) { |
101 | 143 | id := r.PathValue("id") |
102 | 144 | sr, err := s.store.GetReceiptByID(id) |
|
0 commit comments