From f7fb4eb103df9122eacebc21f57fe53b037fe082 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Wed, 13 Mar 2024 17:11:56 +0000 Subject: Handler for creating a datamapLine object --- cmd/api/datamaps.go | 27 ++++++++++++++++++++------- cmd/api/routes.go | 1 + 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/api/datamaps.go b/cmd/api/datamaps.go index 10dfa38..8a79d0f 100644 --- a/cmd/api/datamaps.go +++ b/cmd/api/datamaps.go @@ -2,6 +2,7 @@ package main import ( "encoding/csv" + "encoding/json" "fmt" "net/http" "strconv" @@ -12,9 +13,10 @@ import ( // The fields need to be exported otherwise they won't be included when encoding // the struct to json. type datamapLine struct { - Key string `json:"key"` - Sheet string `json:"sheet"` - Cellref string `json:"cellref"` + Key string `json:"key"` + Sheet string `json:"sheet"` + DataType string `json:"datatype"` + Cellref string `json:"cellref"` } // datamap includes a slice of datamapLine objects alongside header metadata @@ -60,15 +62,16 @@ func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Requ http.Error(w, err.Error(), http.StatusInternalServerError) return } - if len(line) != 3 { + if len(line) != 4 { http.Error(w, "Invalid CSV Format", http.StatusBadRequest) return } dmls = append(dmls, datamapLine{ - Key: line[0], - Sheet: line[1], - Cellref: line[2], + Key: line[0], + Sheet: line[1], + DataType: line[2], + Cellref: line[3], }) } dm = datamap{Name: dmName, Description: dmDesc, Created: time.Now(), DMLs: dmls} @@ -91,3 +94,13 @@ func (app *application) showDatamapHandler(w http.ResponseWriter, r *http.Reques } fmt.Fprintf(w, "show the details for datamap %d\n", id_int) } + +func (app *application) createDatamapLine(w http.ResponseWriter, r *http.Request) { + var input datamapLine + err := json.NewDecoder(r.Body).Decode(&input) + if err != nil { + app.errorResponse(w, r, http.StatusBadRequest, err.Error()) + return + } + fmt.Fprintf(w, "%v\n", input) +} diff --git a/cmd/api/routes.go b/cmd/api/routes.go index acf13d0..90e7403 100644 --- a/cmd/api/routes.go +++ b/cmd/api/routes.go @@ -8,6 +8,7 @@ func (app *application) routes() *http.ServeMux { mux.HandleFunc("GET /v1/healthcheck", app.healthcheckHandler) mux.HandleFunc("POST /v1/datamaps", app.createDatamapHandler) + mux.HandleFunc("POST /v1/datamapline", app.createDatamapLine) mux.HandleFunc("GET /v1/datamaps/{id}", app.showDatamapHandler) return mux } -- cgit v1.2.3