From 64c3ecfbd27b55bbbfd42be1d56fc0baafe7deef Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Tue, 23 Jul 2024 08:32:16 -0400 Subject: [PATCH] Basic parser --- internal/handlers/check.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/internal/handlers/check.go b/internal/handlers/check.go index d922c64..5267a80 100644 --- a/internal/handlers/check.go +++ b/internal/handlers/check.go @@ -2,7 +2,6 @@ package handlers import ( "encoding/json" - "io" "log/slog" "net/http" ) @@ -13,14 +12,34 @@ func CheckMyWork(w http.ResponseWriter, r *http.Request) { return } - body, err := io.ReadAll(r.Body) - if err != nil { - http.Error(w, "Error reading request body", http.StatusInternalServerError) + if r.ContentLength == 0 { + http.Error(w, "Request body is empty", http.StatusBadRequest) return } + defer r.Body.Close() - slog.Info("Payload", "payload", string(body)) + var csvData [][]string + err := json.NewDecoder(r.Body).Decode(&csvData) + if err != nil { + http.Error(w, "Error parsing CSV", http.StatusBadRequest) + return + } + + if len(csvData) < 2 { + http.Error(w, "No rows in CSV to process", http.StatusBadRequest) + } + + header := csvData[0] + + for rowIndex, row := range csvData[1:] { + item := make(map[string]string, len(header)) + for colIndex, cell := range row { + column := header[colIndex] + item[column] = cell + } + slog.Info("Parsed row", "row", rowIndex, "values", item) + } response := map[string]string{ "A2": "Failed date format",