Skip to content

Commit

Permalink
Basic parser
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Jul 23, 2024
1 parent 4a1ccb6 commit 64c3ecf
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions internal/handlers/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"encoding/json"
"io"
"log/slog"
"net/http"
)
Expand All @@ -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",
Expand Down

0 comments on commit 64c3ecf

Please sign in to comment.