From de85664af5056d22cfcad454d112ca113fb1f351 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Tue, 8 Dec 2020 23:57:03 +0100 Subject: [PATCH] various updates on qrz source files --- .gitignore | 1 + src/qrzws/main.go | 60 +++++++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 0f2458b..8d14c49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *-packr.go *.ini +*.swp /*.tar.gz /dist /packr2 diff --git a/src/qrzws/main.go b/src/qrzws/main.go index bd94f47..2e1784b 100644 --- a/src/qrzws/main.go +++ b/src/qrzws/main.go @@ -4,7 +4,6 @@ package qrzws import ( "fmt" - "log" "net/http" "regexp" "strings" @@ -39,14 +38,16 @@ func RunServer(config config.Config) (err error) { e.POST("/qrzws", func(c echo.Context) (err error) { res, err := Run(c, config) if err != nil { - return c.String(http.StatusInternalServerError, "Erreur 500 ta mère") + return c.String(http.StatusInternalServerError, + fmt.Sprintf("Error 500 %s", err)) } return c.JSON(http.StatusOK, res) }) e.GET("/export_frs.csv", func(c echo.Context) (err error) { data, mime, err := RunCSVExport(c, config) if err != nil { - log.Fatalln(err) + return c.String(http.StatusInternalServerError, + fmt.Sprintf("Error 500 %s", err)) } return c.Blob(http.StatusOK, mime, data) }) @@ -66,7 +67,7 @@ func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err erro return } - rows, err := BuildQuery(config, *qrzinputjson) + rows, err := BuildQuery(config, qrzinputjson) if err != nil { fmt.Println(err) return @@ -76,13 +77,13 @@ func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err erro res.Data = append(res.Data, qrz.ToSlice(b)) } - res.RecordsFiltered, err = BuildQueryCountFiltered(config, *qrzinputjson) + res.RecordsFiltered, err = BuildQCntFil(config, qrzinputjson) if err != nil { fmt.Println(err, "filtered") return } - res.RecordsTotal, err = BuildQueryCountTotal(config, *qrzinputjson) + res.RecordsTotal, err = BuildQCntTot(config, qrzinputjson) if err != nil { fmt.Println(err, "total") return @@ -94,7 +95,7 @@ func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err erro } // BuildQuery builds main query -func BuildQuery(config config.Config, qrzdt QrzDatatableInput) (qrz []qrz.Qrz, err error) { +func BuildQuery(config config.Config, qrzdt *QrzDatatableInput) (qrz []qrz.Qrz, err error) { selectstatement, err := SetSelect(config, qrzdt) if err != nil { return nil, err @@ -116,26 +117,34 @@ func BuildQuery(config config.Config, qrzdt QrzDatatableInput) (qrz []qrz.Qrz, e } if orderdir == "desc" { - err = config.Db.Where(searchstatement).Limit(limit, offset).Desc(ordercol...).Select(selectstatement).Find(&qrz) + err = config.Db.Where(searchstatement).Limit( + limit, offset).Desc( + ordercol...).Select(selectstatement).Find(&qrz) } else if orderdir == "asc" { - err = config.Db.Where(searchstatement).Limit(limit, offset).Asc(ordercol...).Select(selectstatement).Find(&qrz) + err = config.Db.Where(searchstatement).Limit( + limit, offset).Asc( + ordercol...).Select(selectstatement).Find(&qrz) } else { - err = config.Db.Where(searchstatement).Limit(limit, offset).Asc("qrz").Select(selectstatement).Find(&qrz) + err = config.Db.Where(searchstatement).Limit( + limit, offset).Asc( + "qrz").Select(selectstatement).Find(&qrz) } return } -// BuildQueryCountFiltered builds query for counting filtered -func BuildQueryCountFiltered(config config.Config, qrzdt QrzDatatableInput) (cnt int64, err error) { +// BuildQCntFil builds query for counting filtered +func BuildQCntFil(config config.Config, qrzdt *QrzDatatableInput) (cnt int64, err error) { searchstatement, err := SetSearchLike(config, qrzdt) - cnt, err = config.Db.SQL(fmt.Sprintf("SELECT COUNT(*) FROM qrz WHERE %s", searchstatement)).Count() + cnt, err = config.Db.SQL( + fmt.Sprintf("SELECT COUNT(*) FROM qrz WHERE %s", + searchstatement)).Count() return } -// BuildQueryCountTotal builds query for counting totals -func BuildQueryCountTotal(config config.Config, qrzdt QrzDatatableInput) (cnt int64, err error) { +// BuildQCntTot builds query for counting totals +func BuildQCntTot(config config.Config, qrzdt *QrzDatatableInput) (cnt int64, err error) { var qrz qrz.Qrz cnt, err = config.Db.Count(&qrz) @@ -143,7 +152,7 @@ func BuildQueryCountTotal(config config.Config, qrzdt QrzDatatableInput) (cnt in } // SetSelect build the sql select statement part -func SetSelect(config config.Config, qrzdt QrzDatatableInput) (selectstatement string, err error) { +func SetSelect(config config.Config, qrzdt *QrzDatatableInput) (selectstatement string, err error) { var cols []string colre := regexp.MustCompile(`^[A-Za-z0-9]+$`) @@ -166,13 +175,15 @@ func SetSelect(config config.Config, qrzdt QrzDatatableInput) (selectstatement s } // SetOrder build the sql order statement part -func SetOrder(config config.Config, qrzdt QrzDatatableInput) (orderdir string, ordercols []string, err error) { +func SetOrder(config config.Config, qrzdt *QrzDatatableInput) (orderdir string, ordercols []string, err error) { colre := regexp.MustCompile(`^[A-Za-z0-9]+$`) orderre := regexp.MustCompile(`^(ASC|asc|DESC|desc)$`) for _, col := range qrzdt.Order { - if colre.MatchString(qrzdt.Columns[col.Column].Name) && orderre.MatchString(col.Dir) { - ordercols = append(ordercols, fmt.Sprintf("%s", qrzdt.Columns[col.Column].Name)) + if colre.MatchString(qrzdt.Columns[col.Column].Name) && + orderre.MatchString(col.Dir) { + ordercols = append(ordercols, + fmt.Sprintf("%s", qrzdt.Columns[col.Column].Name)) orderdir = col.Dir } else { err = fmt.Errorf("Order statements does not match prerequisites") @@ -184,14 +195,16 @@ func SetOrder(config config.Config, qrzdt QrzDatatableInput) (orderdir string, o } // SetLimit build the sql limit -func SetLimit(config config.Config, qrzdt QrzDatatableInput) (limit int, offset int, err error) { +func SetLimit(config config.Config, qrzdt *QrzDatatableInput) (limit int, offset int, err error) { intre := regexp.MustCompile(`^[0-9]+$`) if qrzdt.Length < 1 { qrzdt.Length = 50 } - if intre.MatchString(fmt.Sprintf("%d", qrzdt.Length)) && intre.MatchString(fmt.Sprintf("%d", qrzdt.Start)) { + if intre.MatchString( + fmt.Sprintf("%d", qrzdt.Length)) && intre.MatchString( + fmt.Sprintf("%d", qrzdt.Start)) { limit = qrzdt.Length offset = qrzdt.Start } else { @@ -203,11 +216,12 @@ func SetLimit(config config.Config, qrzdt QrzDatatableInput) (limit int, offset } // SetSearchLike build the where clause in sql statement -func SetSearchLike(config config.Config, qrzdt QrzDatatableInput) (searchstmt string, err error) { +func SetSearchLike(config config.Config, qrzdt *QrzDatatableInput) (searchstmt string, err error) { var searchstmtslice []string if len(qrzdt.Columns) > 0 { for id, i := range qrzdt.Columns { - searchstmtslice = append(searchstmtslice, fmt.Sprintf("%s ILIKE '%%%s%%'", i.Name, qrzdt.Search.Value)) + searchstmtslice = append(searchstmtslice, + fmt.Sprintf("%s ILIKE '%%%s%%'", i.Name, qrzdt.Search.Value)) if id < len(qrzdt.Columns)-1 { searchstmtslice = append(searchstmtslice, "OR") }