wip
This commit is contained in:
parent
9ce3730cc7
commit
72f0c0d273
@ -3,9 +3,9 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"text/template"
|
"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/storage"
|
||||||
"git.espin.casa/albert/cml04-falcon-ui/types"
|
"git.espin.casa/albert/cml04-falcon-ui/types"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
@ -36,6 +36,12 @@ func BarcodesHandler(storage storage.Storager) httprouter.Handle {
|
|||||||
codigo := r.FormValue("codigo")
|
codigo := r.FormValue("codigo")
|
||||||
// date range
|
// date range
|
||||||
fechas := r.FormValue("fechas")
|
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
|
// loading bed
|
||||||
evacuadores := r.FormValue("evacuadores")
|
evacuadores := r.FormValue("evacuadores")
|
||||||
// check form values
|
// check form values
|
||||||
@ -49,10 +55,7 @@ func BarcodesHandler(storage storage.Storager) httprouter.Handle {
|
|||||||
// append barcode to holder
|
// append barcode to holder
|
||||||
Barcodes = append(Barcodes, *data)
|
Barcodes = append(Barcodes, *data)
|
||||||
} else {
|
} else {
|
||||||
inicio := strings.Split(fechas, "-")[0]
|
|
||||||
inicio = strings.TrimSpace(inicio)
|
|
||||||
final := strings.Split(fechas, "-")[1]
|
|
||||||
final = strings.TrimSpace(final)
|
|
||||||
// get loading bed from string
|
// get loading bed from string
|
||||||
lb, ok := types.MapLoadingBed[evacuadores]
|
lb, ok := types.MapLoadingBed[evacuadores]
|
||||||
// if loading bed not found
|
// if loading bed not found
|
||||||
@ -72,8 +75,7 @@ func BarcodesHandler(storage storage.Storager) httprouter.Handle {
|
|||||||
// parse template files
|
// parse template files
|
||||||
t, _ := template.ParseFiles("templates/base.html", "templates/barcodes.html")
|
t, _ := template.ParseFiles("templates/base.html", "templates/barcodes.html")
|
||||||
// execute templates
|
// execute templates
|
||||||
err := t.Execute(w, Barcodes)
|
if err := t.Execute(w, Barcodes); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
26
helper/helper.go
Normal file
26
helper/helper.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package helper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Dates(dr string) (start, finish time.Time, err error) {
|
||||||
|
// split string in dates
|
||||||
|
inicio := strings.Split(dr, "-")[0]
|
||||||
|
final := strings.Split(dr, "-")[1]
|
||||||
|
// remove spaces from strings
|
||||||
|
inicio = strings.TrimSpace(inicio)
|
||||||
|
final = strings.TrimSpace(final)
|
||||||
|
// parse start date
|
||||||
|
start, err = time.Parse("02/01/2006 15:04", inicio)
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, time.Time{}, err
|
||||||
|
}
|
||||||
|
// parse finish date
|
||||||
|
finish, err = time.Parse("02/01/2006 15:04", final)
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, time.Time{}, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
@ -21,9 +21,9 @@ type DBConfig struct {
|
|||||||
|
|
||||||
type Storager interface {
|
type Storager interface {
|
||||||
Barcode(ctx context.Context, reading string) (barcode *types.Barcode, err error)
|
Barcode(ctx context.Context, reading string) (barcode *types.Barcode, err error)
|
||||||
ListBarcode(ctx context.Context, lb types.LoadingBed, inicio, final string) (barcodes []types.Barcode, err error)
|
ListBarcode(ctx context.Context, lb types.LoadingBed, inicio, final time.Time) (barcodes []types.Barcode, err error)
|
||||||
Bundle(ctx context.Context, ua string) (bundle *types.BundleData, err error)
|
Bundle(ctx context.Context, ua string) (bundle *types.BundleData, err error)
|
||||||
ListBundle(ctx context.Context, lb types.LoadingBed, inicio string, final string) (bundles []types.BundleData, err error)
|
ListBundle(ctx context.Context, lb types.LoadingBed, inicio, final time.Time) (bundles []types.BundleData, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type storage struct {
|
type storage struct {
|
||||||
@ -32,21 +32,13 @@ type storage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListBundle implements Storager.
|
// ListBundle implements Storager.
|
||||||
func (s *storage) ListBundle(ctx context.Context, lb types.LoadingBed, inicio string, final string) (bundles []types.BundleData, err error) {
|
func (s *storage) ListBundle(ctx context.Context, lb types.LoadingBed, inicio time.Time, final time.Time) (bundles []types.BundleData, err error) {
|
||||||
ti, err := time.Parse("02/01/2006 15:04", inicio)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tf, err := time.Parse("02/01/2006 15:04", final)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if lb == types.ALL {
|
if lb == types.ALL {
|
||||||
if err := s.db.Where("created_at >= ? and created_at < ?", ti, tf).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
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := s.db.Where("loading_bed = ? and created_at >= ? and created_at < ?", lb, ti, tf).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
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,26 +54,16 @@ func (s *storage) Bundle(ctx context.Context, ua string) (bundle *types.BundleDa
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListBarcode implements Storager.
|
// ListBarcode implements Storager.
|
||||||
func (s *storage) ListBarcode(ctx context.Context, lb types.LoadingBed, inicio string, final string) (barcodes []types.Barcode, err error) {
|
func (s *storage) ListBarcode(ctx context.Context, lb types.LoadingBed, inicio time.Time, final time.Time) (barcodes []types.Barcode, err error) {
|
||||||
ti, err := time.Parse("02/01/2006 15:04", inicio)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tf, err := time.Parse("02/01/2006 15:04", final)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if lb == types.ALL {
|
if lb == types.ALL {
|
||||||
if err := s.db.Where("created_at >= ? and created_at < ?", ti, tf).Find(&barcodes).WithContext(ctx).Error; err != nil {
|
if err := s.db.Where("created_at >= ? and created_at < ?", inicio, final).Find(&barcodes).WithContext(ctx).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := s.db.Where("loading_bed = ? and created_at >= ? and created_at < ?", lb, ti, tf).Find(&barcodes).WithContext(ctx).Error; err != nil {
|
if err := s.db.Where("loading_bed = ? and created_at >= ? and created_at < ?", lb, inicio, final).Find(&barcodes).WithContext(ctx).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Printf("result: +%v\n", barcodes)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user