diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/dbasik-api/datamaps.go | 167 | ||||
-rw-r--r-- | cmd/dbasik-api/datamaps_test.go | 50 | ||||
-rw-r--r-- | cmd/dbasik-api/handlers.go | 256 | ||||
-rw-r--r-- | cmd/dbasik-api/routes.go | 1 |
4 files changed, 319 insertions, 155 deletions
diff --git a/cmd/dbasik-api/datamaps.go b/cmd/dbasik-api/datamaps.go index 94bccbf..1121581 100644 --- a/cmd/dbasik-api/datamaps.go +++ b/cmd/dbasik-api/datamaps.go @@ -19,12 +19,7 @@ package main import ( "database/sql" - "encoding/csv" - "encoding/json" "errors" - "fmt" - "net/http" - "strconv" "time" ) @@ -69,6 +64,18 @@ type datamapModel struct { DB *sql.DB } +func GetSheetsFromDM(dm datamap) []string { + sheets := map[string]struct{}{} + for _, dml := range dm.DMLs { + sheets[dml.Sheet] = struct{}{} + } + var out []string + for sheet := range sheets { + out = append(out, sheet) + } + return out +} + func NewModels(db *sql.DB) Models { return Models{ Datamaps: datamapModel{DB: db}, @@ -113,153 +120,3 @@ func (m *datamapLineModel) Insert(dm datamap, dmls []datamapLine) (int, error) { tx.Commit() return int(datamapID), nil } - -func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Request) { - // Parse the multipart form - err := r.ParseMultipartForm(10 << 20) // 10Mb max - if err != nil { - app.serverErrorResponse(w, r, err) - } - - // Get form values - dmName := r.FormValue("name") - app.logger.Info("obtain value from form", "name", dmName) - dmDesc := r.FormValue("description") - app.logger.Info("obtain value from form", "description", dmDesc) - - // Get the uploaded file and name - file, _, err := r.FormFile("file") - if err != nil { - http.Error(w, "Missing file", http.StatusBadRequest) - return - } - defer file.Close() - - // parse the csv - reader := csv.NewReader(file) - var dmls []datamapLine - var dm datamap - - for { - line, err := reader.Read() - if err != nil { - if err.Error() == "EOF" { - break // end of file - } - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - if len(line) != 4 { - http.Error(w, "Invalid CSV Format", http.StatusBadRequest) - return - } - - dmls = append(dmls, datamapLine{ - Key: line[0], - Sheet: line[1], - DataType: line[2], - Cellref: line[3], - }) - } - dm = datamap{Name: dmName, Description: dmDesc, Created: time.Now(), DMLs: dmls} - - err = app.writeJSONPretty(w, http.StatusOK, envelope{"datamap": dm}, nil) - if err != nil { - app.logger.Debug("writing out csv", "err", err) - app.serverErrorResponse(w, r, err) - } - - fmt.Fprintf(w, "file successfully uploaded") -} - -func (app *application) saveDatamapHandler(w http.ResponseWriter, r *http.Request) { - // Parse the multipart form - err := r.ParseMultipartForm(10 << 20) // 10Mb max - if err != nil { - app.serverErrorResponse(w, r, err) - } - // Get form values - dmName := r.FormValue("name") - app.logger.Info("obtain value from form", "name", dmName) - dmDesc := r.FormValue("description") - app.logger.Info("obtain value from form", "description", dmDesc) - - // Get the uploaded file and name - file, _, err := r.FormFile("file") - if err != nil { - http.Error(w, "Missing file", http.StatusBadRequest) - return - } - defer file.Close() - - // parse the csv - reader := csv.NewReader(file) - var dmls []datamapLine - var dm datamap - - for { - line, err := reader.Read() - if err != nil { - if err.Error() == "EOF" { - break // end of file - } - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - if len(line) != 4 { - http.Error(w, "Invalid CSV Format", http.StatusBadRequest) - return - } - - dmls = append(dmls, datamapLine{ - ID: 0, - Key: line[0], - Sheet: line[1], - DataType: line[2], - Cellref: line[3], - }) - - } - dm = datamap{Name: dmName, Description: dmDesc, Created: time.Now(), DMLs: dmls} - - // save to the database - _, err = app.models.DatamapLines.Insert(dm, dmls) - if err != nil { - http.Error(w, "Cannot save to database", http.StatusBadRequest) - return - } - -} - -func (app *application) getJSONForDatamap(w http.ResponseWriter, r *http.Request) { - // Get the DM out of the database - // dm = datamap{Name: dmName, Description: dmDesc, Created: time.Now(), DMLs: dmls} - - // err = app.writeJSONPretty(w, http.StatusOK, envelope{"datamap": dm}, nil) - // if err != nil { - // app.logger.Debug("writing out csv", "err", err) - // app.serverErrorResponse(w, r, err) - // } - - // fmt.Fprintf(w, "file successfully uploaded") -} - -func (app *application) showDatamapHandler(w http.ResponseWriter, r *http.Request) { - id := r.PathValue("id") - app.logger.Info("the id requested", "id", id) - id_int, err := strconv.ParseInt(id, 10, 64) - if err != nil || id_int < 1 { - app.notFoundResponse(w, r) - } - 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/dbasik-api/datamaps_test.go b/cmd/dbasik-api/datamaps_test.go new file mode 100644 index 0000000..428f301 --- /dev/null +++ b/cmd/dbasik-api/datamaps_test.go @@ -0,0 +1,50 @@ +package main + +import ( + "slices" + "testing" + "time" +) + +func TestReadDML(t *testing.T) { + dm := datamap{ + ID: 0, + Name: "Test Name", + Description: "Test description", + Created: time.Now(), + DMLs: []datamapLine{ + { + ID: 1, + Key: "Test Key", + Sheet: "Test Sheet", + DataType: "TEXT", + Cellref: "A10", + }, + { + ID: 2, + Key: "Test Key 2", + Sheet: "Test Sheet", + DataType: "TEXT", + Cellref: "A11", + }, + { + ID: 3, + Key: "Test Key 3", + Sheet: "Test Sheet 2", + DataType: "TEXT", + Cellref: "A12", + }, + }, + } + + got := GetSheetsFromDM(dm) + if !slices.Contains(got, "Test Sheet") { + t.Errorf("expected to find Test Sheet in %v but didn't find it", got) + } + if !slices.Contains(got, "Test Sheet 2") { + t.Errorf("expected to find Test Sheet in %v but didn't find it", got) + } + if slices.Contains(got, "Test Sheet 3") { + t.Errorf("expected to find Test Sheet in %v but didn't find it", got) + } +} diff --git a/cmd/dbasik-api/handlers.go b/cmd/dbasik-api/handlers.go new file mode 100644 index 0000000..c79de7e --- /dev/null +++ b/cmd/dbasik-api/handlers.go @@ -0,0 +1,256 @@ +package main + +import ( + "encoding/csv" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "strconv" + "time" + + "github.com/tealeg/xlsx/v3" +) + +func (app *application) createReturnHandler(w http.ResponseWriter, r *http.Request) { + // Parse the multipart form + err := r.ParseMultipartForm(10 << 20) // 10Mb max + if err != nil { + app.serverErrorResponse(w, r, err) + } + + // Get form values + dmName := r.FormValue("name") + dmDesc := r.FormValue("description") + + // Get the return file and save it to a file + returnFile, handler, err := r.FormFile("returnfile") + app.logger.Info("got excel file") + if err != nil { + http.Error(w, "Missing file", http.StatusBadRequest) + return + } + defer returnFile.Close() + dst, err := os.Create(handler.Filename) + if err != nil { + http.Error(w, "Cannot create new file object from uplaoded file.", http.StatusInternalServerError) + return + } + defer dst.Close() + if _, err := io.Copy(dst, returnFile); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Get the uploaded csv file and name + file, _, err := r.FormFile("file") + if err != nil { + http.Error(w, "Missing file", http.StatusBadRequest) + return + } + defer file.Close() + + // parse the csv + reader := csv.NewReader(file) + var dmls []datamapLine + + for { + line, err := reader.Read() + if err != nil { + if err.Error() == "EOF" { + break // end of file + } + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if len(line) != 4 { + http.Error(w, "Invalid CSV Format", http.StatusBadRequest) + return + } + + dmls = append(dmls, datamapLine{ + Key: line[0], + Sheet: line[1], + DataType: line[2], + Cellref: line[3], + }) + } + dm := datamap{Name: dmName, Description: dmDesc, Created: time.Now(), DMLs: dmls} + + // Parse the XLSX file based on the datamap... + // open an existing file + wb, err := xlsx.OpenFile(dst.Name()) + if err != nil { + http.Error(w, "Cannot open Excel file", http.StatusBadRequest) + return + } + // wb now contains a reference to the workbook + // show all the sheets in the workbook + fmt.Println("Sheets in this file:") + for i, sh := range wb.Sheets { + fmt.Println(i, sh.Name) + } + fmt.Println("----") + + //Here is where we parse our files. + + err = app.writeJSONPretty(w, http.StatusOK, envelope{"datamap": dm}, nil) + if err != nil { + app.logger.Debug("writing out csv", "err", err) + app.serverErrorResponse(w, r, err) + } + + fmt.Fprintf(w, "file successfully uploaded") + +} + +func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Request) { + // Parse the multipart form + err := r.ParseMultipartForm(10 << 20) // 10Mb max + if err != nil { + app.serverErrorResponse(w, r, err) + } + + // Get form values + dmName := r.FormValue("name") + app.logger.Info("obtain value from form", "name", dmName) + dmDesc := r.FormValue("description") + app.logger.Info("obtain value from form", "description", dmDesc) + + // Get the uploaded file and name + file, _, err := r.FormFile("file") + if err != nil { + http.Error(w, "Missing file", http.StatusBadRequest) + return + } + defer file.Close() + + // parse the csv + reader := csv.NewReader(file) + var dmls []datamapLine + var dm datamap + + for { + line, err := reader.Read() + if err != nil { + if err.Error() == "EOF" { + break // end of file + } + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if len(line) != 4 { + http.Error(w, "Invalid CSV Format", http.StatusBadRequest) + return + } + + dmls = append(dmls, datamapLine{ + Key: line[0], + Sheet: line[1], + DataType: line[2], + Cellref: line[3], + }) + } + dm = datamap{Name: dmName, Description: dmDesc, Created: time.Now(), DMLs: dmls} + + err = app.writeJSONPretty(w, http.StatusOK, envelope{"datamap": dm}, nil) + if err != nil { + app.logger.Debug("writing out csv", "err", err) + app.serverErrorResponse(w, r, err) + } + + fmt.Fprintf(w, "file successfully uploaded") +} + +func (app *application) saveDatamapHandler(w http.ResponseWriter, r *http.Request) { + // Parse the multipart form + err := r.ParseMultipartForm(10 << 20) // 10Mb max + if err != nil { + app.serverErrorResponse(w, r, err) + } + // Get form values + dmName := r.FormValue("name") + app.logger.Info("obtain value from form", "name", dmName) + dmDesc := r.FormValue("description") + app.logger.Info("obtain value from form", "description", dmDesc) + + // Get the uploaded file and name + file, _, err := r.FormFile("file") + if err != nil { + http.Error(w, "Missing file", http.StatusBadRequest) + return + } + defer file.Close() + + // parse the csv + reader := csv.NewReader(file) + var dmls []datamapLine + var dm datamap + + for { + line, err := reader.Read() + if err != nil { + if err.Error() == "EOF" { + break // end of file + } + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if len(line) != 4 { + http.Error(w, "Invalid CSV Format", http.StatusBadRequest) + return + } + + dmls = append(dmls, datamapLine{ + ID: 0, + Key: line[0], + Sheet: line[1], + DataType: line[2], + Cellref: line[3], + }) + + } + dm = datamap{Name: dmName, Description: dmDesc, Created: time.Now(), DMLs: dmls} + + // save to the database + _, err = app.models.DatamapLines.Insert(dm, dmls) + if err != nil { + http.Error(w, "Cannot save to database", http.StatusBadRequest) + return + } + +} + +func (app *application) getJSONForDatamap(w http.ResponseWriter, r *http.Request) { + // Get the DM out of the database + // dm = datamap{Name: dmName, Description: dmDesc, Created: time.Now(), DMLs: dmls} + + // err = app.writeJSONPretty(w, http.StatusOK, envelope{"datamap": dm}, nil) + // if err != nil { + // app.logger.Debug("writing out csv", "err", err) + // app.serverErrorResponse(w, r, err) + // } + + // fmt.Fprintf(w, "file successfully uploaded") +} + +func (app *application) showDatamapHandler(w http.ResponseWriter, r *http.Request) { + id := r.PathValue("id") + app.logger.Info("the id requested", "id", id) + id_int, err := strconv.ParseInt(id, 10, 64) + if err != nil || id_int < 1 { + app.notFoundResponse(w, r) + } + 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/dbasik-api/routes.go b/cmd/dbasik-api/routes.go index 3c9e85b..1b02028 100644 --- a/cmd/dbasik-api/routes.go +++ b/cmd/dbasik-api/routes.go @@ -25,6 +25,7 @@ func (app *application) routes() *http.ServeMux { mux.HandleFunc("GET /v1/healthcheck", app.healthcheckHandler) mux.HandleFunc("GET /v1/getdatamap/{id}", app.getJSONForDatamap) // TODO: not yet implemented + mux.HandleFunc("POST /v1/return", app.createReturnHandler) mux.HandleFunc("POST /v1/datamapsave", app.saveDatamapHandler) mux.HandleFunc("POST /v1/datamap", app.createDatamapHandler) mux.HandleFunc("POST /v1/datamapline", app.createDatamapLine) |