various updates on qrz source files
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Paul 2020-12-08 23:57:03 +01:00
parent d99c6c9959
commit de85664af5
2 changed files with 38 additions and 23 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
*-packr.go *-packr.go
*.ini *.ini
*.swp
/*.tar.gz /*.tar.gz
/dist /dist
/packr2 /packr2

View File

@ -4,7 +4,6 @@ package qrzws
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@ -39,14 +38,16 @@ func RunServer(config config.Config) (err error) {
e.POST("/qrzws", func(c echo.Context) (err error) { e.POST("/qrzws", func(c echo.Context) (err error) {
res, err := Run(c, config) res, err := Run(c, config)
if err != nil { 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) return c.JSON(http.StatusOK, res)
}) })
e.GET("/export_frs.csv", func(c echo.Context) (err error) { e.GET("/export_frs.csv", func(c echo.Context) (err error) {
data, mime, err := RunCSVExport(c, config) data, mime, err := RunCSVExport(c, config)
if err != nil { if err != nil {
log.Fatalln(err) return c.String(http.StatusInternalServerError,
fmt.Sprintf("Error 500 %s", err))
} }
return c.Blob(http.StatusOK, mime, data) return c.Blob(http.StatusOK, mime, data)
}) })
@ -66,7 +67,7 @@ func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err erro
return return
} }
rows, err := BuildQuery(config, *qrzinputjson) rows, err := BuildQuery(config, qrzinputjson)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return 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.Data = append(res.Data, qrz.ToSlice(b))
} }
res.RecordsFiltered, err = BuildQueryCountFiltered(config, *qrzinputjson) res.RecordsFiltered, err = BuildQCntFil(config, qrzinputjson)
if err != nil { if err != nil {
fmt.Println(err, "filtered") fmt.Println(err, "filtered")
return return
} }
res.RecordsTotal, err = BuildQueryCountTotal(config, *qrzinputjson) res.RecordsTotal, err = BuildQCntTot(config, qrzinputjson)
if err != nil { if err != nil {
fmt.Println(err, "total") fmt.Println(err, "total")
return return
@ -94,7 +95,7 @@ func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err erro
} }
// BuildQuery builds main query // 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) selectstatement, err := SetSelect(config, qrzdt)
if err != nil { if err != nil {
return nil, err return nil, err
@ -116,26 +117,34 @@ func BuildQuery(config config.Config, qrzdt QrzDatatableInput) (qrz []qrz.Qrz, e
} }
if orderdir == "desc" { 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" { } 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 { } 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 return
} }
// BuildQueryCountFiltered builds query for counting filtered // BuildQCntFil builds query for counting filtered
func BuildQueryCountFiltered(config config.Config, qrzdt QrzDatatableInput) (cnt int64, err error) { func BuildQCntFil(config config.Config, qrzdt *QrzDatatableInput) (cnt int64, err error) {
searchstatement, err := SetSearchLike(config, qrzdt) 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 return
} }
// BuildQueryCountTotal builds query for counting totals // BuildQCntTot builds query for counting totals
func BuildQueryCountTotal(config config.Config, qrzdt QrzDatatableInput) (cnt int64, err error) { func BuildQCntTot(config config.Config, qrzdt *QrzDatatableInput) (cnt int64, err error) {
var qrz qrz.Qrz var qrz qrz.Qrz
cnt, err = config.Db.Count(&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 // 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 var cols []string
colre := regexp.MustCompile(`^[A-Za-z0-9]+$`) 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 // 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]+$`) colre := regexp.MustCompile(`^[A-Za-z0-9]+$`)
orderre := regexp.MustCompile(`^(ASC|asc|DESC|desc)$`) orderre := regexp.MustCompile(`^(ASC|asc|DESC|desc)$`)
for _, col := range qrzdt.Order { for _, col := range qrzdt.Order {
if colre.MatchString(qrzdt.Columns[col.Column].Name) && orderre.MatchString(col.Dir) { if colre.MatchString(qrzdt.Columns[col.Column].Name) &&
ordercols = append(ordercols, fmt.Sprintf("%s", qrzdt.Columns[col.Column].Name)) orderre.MatchString(col.Dir) {
ordercols = append(ordercols,
fmt.Sprintf("%s", qrzdt.Columns[col.Column].Name))
orderdir = col.Dir orderdir = col.Dir
} else { } else {
err = fmt.Errorf("Order statements does not match prerequisites") 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 // 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]+$`) intre := regexp.MustCompile(`^[0-9]+$`)
if qrzdt.Length < 1 { if qrzdt.Length < 1 {
qrzdt.Length = 50 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 limit = qrzdt.Length
offset = qrzdt.Start offset = qrzdt.Start
} else { } else {
@ -203,11 +216,12 @@ func SetLimit(config config.Config, qrzdt QrzDatatableInput) (limit int, offset
} }
// SetSearchLike build the where clause in sql statement // 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 var searchstmtslice []string
if len(qrzdt.Columns) > 0 { if len(qrzdt.Columns) > 0 {
for id, i := range qrzdt.Columns { 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 { if id < len(qrzdt.Columns)-1 {
searchstmtslice = append(searchstmtslice, "OR") searchstmtslice = append(searchstmtslice, "OR")
} }