This commit is contained in:
Albert Espín 2024-10-07 19:22:01 +02:00
parent 9ce3730cc7
commit 72f0c0d273
3 changed files with 43 additions and 33 deletions

View File

@ -3,9 +3,9 @@ package handlers
import (
"fmt"
"net/http"
"strings"
"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"
@ -36,6 +36,12 @@ 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
@ -49,10 +55,7 @@ func BarcodesHandler(storage storage.Storager) httprouter.Handle {
// append barcode to holder
Barcodes = append(Barcodes, *data)
} else {
inicio := strings.Split(fechas, "-")[0]
inicio = strings.TrimSpace(inicio)
final := strings.Split(fechas, "-")[1]
final = strings.TrimSpace(final)
// get loading bed from string
lb, ok := types.MapLoadingBed[evacuadores]
// if loading bed not found
@ -72,8 +75,7 @@ func BarcodesHandler(storage storage.Storager) httprouter.Handle {
// parse template files
t, _ := template.ParseFiles("templates/base.html", "templates/barcodes.html")
// execute templates
err := t.Execute(w, Barcodes)
if err != nil {
if err := t.Execute(w, Barcodes); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

26
helper/helper.go Normal file
View 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
}

View File

@ -21,9 +21,9 @@ type DBConfig struct {
type Storager interface {
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)
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 {
@ -32,21 +32,13 @@ type storage struct {
}
// ListBundle implements Storager.
func (s *storage) ListBundle(ctx context.Context, lb types.LoadingBed, inicio string, final string) (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
}
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 < ?", 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
}
} 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
}
}
@ -62,26 +54,16 @@ func (s *storage) Bundle(ctx context.Context, ua string) (bundle *types.BundleDa
}
// ListBarcode implements Storager.
func (s *storage) ListBarcode(ctx context.Context, lb types.LoadingBed, inicio string, final string) (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
}
func (s *storage) ListBarcode(ctx context.Context, lb types.LoadingBed, inicio time.Time, final time.Time) (barcodes []types.Barcode, err error) {
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
}
} 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
}
}
fmt.Printf("result: +%v\n", barcodes)
return
}