aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/dbasik-api/datamaps.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/dbasik-api/datamaps.go')
-rw-r--r--cmd/dbasik-api/datamaps.go167
1 files changed, 12 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)
-}