basic common app packages

This commit is contained in:
2025-06-16 22:19:00 +05:30
parent 8654f21b62
commit 0240ec154e
49 changed files with 5481 additions and 232 deletions

90
request/pager.go Normal file
View File

@@ -0,0 +1,90 @@
// Copyright 2024 Patial Tech (Ankit Patial).
// All rights reserved.
package request
import (
"net/http"
"strconv"
"strings"
"code.patial.tech/go/appcore/ptr"
)
type Pager struct {
Search string
OrderBy *string `json:"orderBy"`
OrderAsc *bool `json:"orderAsc"`
Page int `json:"page"`
Size int `json:"size"`
Total int `json:"total"`
TotalNeeded bool `json:"-"`
}
func (i *Pager) HasSearch() bool {
if i == nil {
return false
}
return strings.TrimSpace(i.Search) != ""
}
func (i *Pager) Offset() int {
if i == nil {
return 0
}
return (i.Page - 1) * i.Limit()
}
func (i *Pager) Limit() int {
if i == nil || ptr.GetNumber(&i.Size) < 1 {
return 20
}
return i.Size
}
// GetPager info from request's query parameters
func GetPager(r *http.Request) Pager {
p := Pager{
Page: 1,
Size: 20,
OrderAsc: ptr.Bool(true),
}
if v := r.URL.Query().Get("q"); v != "" {
p.Search = strings.TrimSpace(v)
}
if v := r.URL.Query().Get("pg"); v != "" {
if vv, err := strconv.Atoi(v); err == nil {
p.Page = int(vv)
}
}
if v := r.URL.Query().Get("pg_s"); v != "" {
if vv, err := strconv.Atoi(v); err == nil {
p.Size = int(vv)
}
}
if v := r.URL.Query().Get("pg_ob"); v != "" {
p.OrderBy = &v
}
if v := r.URL.Query().Get("pg_o"); v != "" {
switch strings.ToLower(v) {
case "asc":
p.OrderAsc = ptr.Bool(true)
case "desc":
p.OrderAsc = ptr.Bool(false)
}
}
if v := r.URL.Query().Get("pg_t"); v == "n" {
p.TotalNeeded = true
}
return p
}

46
request/payload.go Normal file
View File

@@ -0,0 +1,46 @@
// Copyright 2024 Patial Tech (Ankit Patial).
// All rights reserved.
package request
import (
"encoding/json"
"fmt"
"net/http"
"code.patial.tech/go/appcore/validate"
)
func FormField(r *http.Request, key string) (string, error) {
f, err := Payload[map[string]any](r)
if err != nil {
return "", err
}
return fmt.Sprintf("%v", f[key]), nil
}
func PayloadMap(r *http.Request) (map[string]any, error) {
return Payload[map[string]any](r)
}
func PayloadWithValidate[T any](r *http.Request) (T, error) {
p, err := Payload[T](r)
if err != nil {
return p, err
}
if err := validate.Struct(p); err != nil {
return p, err
}
return p, nil
}
func Payload[T any](r *http.Request) (T, error) {
var p T
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
return p, err
}
return p, nil
}

42
request/query.go Normal file
View File

@@ -0,0 +1,42 @@
// Copyright 2024 Patial Tech (Ankit Patial).
// All rights reserved.
package request
import (
"fmt"
"net/http"
"reflect"
"strconv"
)
type Number interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}
// NumberParam from request query string
func NumberParam[T Number](r *http.Request, key string) (T, error) {
var noop T
if !r.URL.Query().Has(key) {
return noop, fmt.Errorf("query param: %q is missing", key)
}
p := r.URL.Query().Get(key)
t := reflect.TypeOf(noop)
k := t.Kind()
// float param
if k == reflect.Float32 || k == reflect.Float64 {
n, err := strconv.ParseFloat(p, 64)
if err != nil {
return noop, fmt.Errorf("query param: %q is not a valid float", key)
}
return T(n), nil
}
// int param
n, err := strconv.ParseInt(p, 10, 64)
if err != nil {
return noop, fmt.Errorf("query param: %q is not a valid integer", key)
}
return T(n), nil
}