diff --git a/assets/js/barcode.js b/assets/js/barcode.js index 19148f8..20660d9 100644 --- a/assets/js/barcode.js +++ b/assets/js/barcode.js @@ -7,10 +7,5 @@ $(document).ready(function () { isRange:true, color:"link", minuteSteps:1}); - // To access to bulmaCalendar instance of an element - $('#submit').click(function() { - const dateInput = $('#inicio').val(); - //$('#selected-date').text(`Fecha seleccionada: ${dateInput}`); - }); }); diff --git a/assets/js/bundle.js b/assets/js/bundle.js new file mode 100644 index 0000000..2199a8f --- /dev/null +++ b/assets/js/bundle.js @@ -0,0 +1,10 @@ +$(document).ready(function () { + // Initialize all input of date type. + const calendar = bulmaCalendar.attach('[type="date"]',{ + weekStart: 1, + dateFormat: 'dd/MM/yyyy', + type:"datetime", + isRange:true, + color:"link", + minuteSteps:1}); +}); diff --git a/handlers/barcodes.go b/handlers/barcodes.go index 80fc0c5..4922070 100644 --- a/handlers/barcodes.go +++ b/handlers/barcodes.go @@ -36,12 +36,6 @@ func BarcodesHandler(storage storage.Storager) httprouter.Handle { codigo := r.FormValue("codigo") // date range fechas := r.FormValue("fechas") - // get range dates - inicio, final, err := helper.Dates(fechas) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } // loading bed evacuadores := r.FormValue("evacuadores") // check form values @@ -55,6 +49,12 @@ func BarcodesHandler(storage storage.Storager) httprouter.Handle { // append barcode to holder Barcodes = append(Barcodes, *data) } else { + // get range dates + inicio, final, err := helper.Dates(fechas) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } // get loading bed from string lb, ok := types.MapLoadingBed[evacuadores] // if loading bed not found diff --git a/handlers/bundle.go b/handlers/bundle.go index d916f21..c04a13c 100644 --- a/handlers/bundle.go +++ b/handlers/bundle.go @@ -1,10 +1,13 @@ package handlers import ( + "fmt" "net/http" "text/template" + "git.espin.casa/albert/cml04-falcon-ui/helper" "git.espin.casa/albert/cml04-falcon-ui/storage" + "git.espin.casa/albert/cml04-falcon-ui/types" "github.com/julienschmidt/httprouter" ) @@ -20,24 +23,60 @@ func BundleHandler(storage storage.Storager) httprouter.Handle { } } - // if r.Method == http.MethodPost { - // // barcodes holder - // Bundles := []types.BundleData{} - // // parse form data from formulary - // if err := r.ParseForm(); err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // } - // // barcode - // codigo := r.FormValue("codigo") - // // date range - // fechas := r.FormValue("fechas") - // // get range dates - // inicio, final, err := helper.Dates(fechas) - // if err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // return - // } - // } - + // post method + if r.Method == http.MethodPost { + // bundles holder + Bundles := []types.BundleData{} + // parse form data from formulary + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + // ua + codigo := r.FormValue("ua") + // date range + fechas := r.FormValue("fechas") + // loading bed + evacuadores := r.FormValue("evacuadores") + // check form values + if codigo != "" { + // call storager + data, err := storage.Bundle(r.Context(), codigo) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // append barcode to holder + Bundles = append(Bundles, *data) + } else { + // get range dates + inicio, final, err := helper.Dates(fechas) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // get loading bed from string + lb, ok := types.MapLoadingBed[evacuadores] + // if loading bed not found + if !ok { + http.Error(w, fmt.Errorf("loading bed value not found").Error(), http.StatusInternalServerError) + return + } + // call storager + data, err := storage.ListBundle(r.Context(), lb, inicio, final) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + //set barcodes + Bundles = data + } + // parse template files + t, _ := template.ParseFiles("templates/base.html", "templates/bundle.html") + // execute templates + if err := t.Execute(w, Bundles); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } } } diff --git a/storage/storage.go b/storage/storage.go index c40ef26..e4b4707 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -34,11 +34,11 @@ type storage struct { // ListBundle implements Storager. func (s *storage) ListBundle(ctx context.Context, lb types.LoadingBed, inicio time.Time, final time.Time) (bundles []types.BundleData, err error) { if lb == types.ALL { - if err := s.db.Where("created_at >= ? and created_at < ?", inicio, final).Find(bundles).WithContext(ctx).Error; err != nil { + if err := s.db.Where("created_at >= ? and created_at < ?", inicio, final).Find(&bundles).WithContext(ctx).Error; err != nil { return nil, err } } else { - if err := s.db.Where("loading_bed = ? and created_at >= ? and created_at < ?", lb, inicio, final).Find(bundles).WithContext(ctx).Error; err != nil { + if err := s.db.Where("loading_bed = ? and created_at >= ? and created_at < ?", lb, inicio, final).Find(&bundles).WithContext(ctx).Error; err != nil { return nil, err } } @@ -47,7 +47,7 @@ func (s *storage) ListBundle(ctx context.Context, lb types.LoadingBed, inicio ti // Bundle implements Storager. func (s *storage) Bundle(ctx context.Context, ua string) (bundle *types.BundleData, err error) { - if err := s.db.First(bundle, ua).WithContext(ctx).Error; err != nil { + if err := s.db.First(&bundle, ua).WithContext(ctx).Error; err != nil { return nil, err } return diff --git a/templates/barcodes.html b/templates/barcodes.html index a016534..b6d0e24 100644 --- a/templates/barcodes.html +++ b/templates/barcodes.html @@ -115,8 +115,8 @@ {{ range . }}