aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-03-13 17:11:56 +0000
committerMatthew Lemon <y@yulqen.org>2024-03-13 17:11:56 +0000
commitf7fb4eb103df9122eacebc21f57fe53b037fe082 (patch)
tree6b5c897d36ad3c416a7287a1d951c607f460aeae
parent033a6e69a473e44ccb7cd66295532846768af114 (diff)
Handler for creating a datamapLine object
-rw-r--r--cmd/api/datamaps.go27
-rw-r--r--cmd/api/routes.go1
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
}