chore: work on the expandable models
This commit is contained in:
@@ -423,6 +423,7 @@ func (x Handle) createContext(handler *Handle, mode AnswerType, r *http.Request)
|
||||
var token *string
|
||||
|
||||
logger := log.NewWithOptions(os.Stdout, log.Options{
|
||||
ReportCaller: true,
|
||||
ReportTimestamp: true,
|
||||
TimeFormat: time.Kitchen,
|
||||
Prefix: r.URL.Path,
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -17,37 +19,37 @@ func CheckEmpty(f url.Values, path string) bool {
|
||||
}
|
||||
|
||||
func CheckNumber(f url.Values, path string, number *int) bool {
|
||||
if CheckEmpty(f, path) {
|
||||
fmt.Println("here", path)
|
||||
fmt.Println(f.Get(path))
|
||||
return false
|
||||
}
|
||||
n, err := strconv.Atoi(f.Get(path))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
}
|
||||
*number = n
|
||||
return true
|
||||
if CheckEmpty(f, path) {
|
||||
fmt.Println("here", path)
|
||||
fmt.Println(f.Get(path))
|
||||
return false
|
||||
}
|
||||
n, err := strconv.Atoi(f.Get(path))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
}
|
||||
*number = n
|
||||
return true
|
||||
}
|
||||
|
||||
func CheckFloat64(f url.Values, path string, number *float64) bool {
|
||||
if CheckEmpty(f, path) {
|
||||
fmt.Println("here", path)
|
||||
fmt.Println(f.Get(path))
|
||||
return false
|
||||
}
|
||||
n, err := strconv.ParseFloat(f.Get(path), 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
}
|
||||
*number = n
|
||||
return true
|
||||
if CheckEmpty(f, path) {
|
||||
fmt.Println("here", path)
|
||||
fmt.Println(f.Get(path))
|
||||
return false
|
||||
}
|
||||
n, err := strconv.ParseFloat(f.Get(path), 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
}
|
||||
*number = n
|
||||
return true
|
||||
}
|
||||
|
||||
func CheckId(f url.Values, path string) bool {
|
||||
return !CheckEmpty(f, path) && IsValidUUID(f.Get(path))
|
||||
return !CheckEmpty(f, path) && IsValidUUID(f.Get(path))
|
||||
}
|
||||
|
||||
func IsValidUUID(u string) bool {
|
||||
@@ -57,19 +59,19 @@ func IsValidUUID(u string) bool {
|
||||
|
||||
func GetIdFromUrl(r *http.Request, target string) (string, error) {
|
||||
if !r.URL.Query().Has(target) {
|
||||
return "", errors.New("Query does not have " + target)
|
||||
return "", errors.New("Query does not have " + target)
|
||||
}
|
||||
|
||||
id := r.URL.Query().Get("id")
|
||||
if len(id) == 0 {
|
||||
return "", errors.New("Query is empty for " + target)
|
||||
return "", errors.New("Query is empty for " + target)
|
||||
}
|
||||
|
||||
if !IsValidUUID(id) {
|
||||
return "", errors.New("Value of query is not a valid uuid for " + target)
|
||||
return "", errors.New("Value of query is not a valid uuid for " + target)
|
||||
}
|
||||
|
||||
return id, nil
|
||||
return id, nil
|
||||
}
|
||||
|
||||
type maxBytesReader struct {
|
||||
@@ -180,3 +182,48 @@ func MyParseForm(r *http.Request) (vs url.Values, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type Generic struct{ reflect.Type }
|
||||
|
||||
var NotFoundError = errors.New("Not found")
|
||||
|
||||
func GetDBOnce(c *Context, store interface{}, tablename string, args ...any) error {
|
||||
|
||||
t := reflect.TypeOf(store).Elem()
|
||||
|
||||
nargs := t.NumField()
|
||||
|
||||
query := ""
|
||||
|
||||
for i := 0; i < nargs; i += 1 {
|
||||
query += strings.ToLower(t.Field(i).Name) + ","
|
||||
}
|
||||
|
||||
query = query[0 : len(query)-1]
|
||||
|
||||
rows, err := c.Db.Query(fmt.Sprintf("select %s from %s", query, tablename), args...)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
if !rows.Next() {
|
||||
return NotFoundError
|
||||
}
|
||||
|
||||
|
||||
val := reflect.ValueOf(store).Elem()
|
||||
scan_args := make([]interface{}, nargs);
|
||||
for i := 0; i < nargs; i++ {
|
||||
valueField := val.Field(i)
|
||||
scan_args[i] = valueField.Addr().Interface()
|
||||
}
|
||||
|
||||
err = rows.Scan(scan_args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user