Yimaru-BackEnd/internal/domain/common.go

223 lines
4.1 KiB
Go

package domain
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/jackc/pgx/v5/pgtype"
"go.uber.org/zap"
)
var MongoDBLogger *zap.Logger
type ValidInt64 struct {
Value int64
Valid bool
}
type ValidInt struct {
Value int
Valid bool
}
type ValidInt32 struct {
Value int32
Valid bool
}
type ValidFloat32 struct {
Value float32
Valid bool
}
type ValidString struct {
Value string
Valid bool
}
type ValidTime struct {
Value time.Time
Valid bool
}
type ValidBool struct {
Value bool
Valid bool
}
// ValidInt64 → pgtype.Int8
func (v ValidInt64) ToPG() pgtype.Int8 {
return pgtype.Int8{
Int64: v.Value,
Valid: v.Valid,
}
}
// ValidInt32 → pgtype.Int4
func (v ValidInt32) ToPG() pgtype.Int4 {
return pgtype.Int4{
Int32: v.Value,
Valid: v.Valid,
}
}
// ValidInt → pgtype.Int4 (Go int mapped to int32 for pg compatibility)
func (v ValidInt) ToPG() pgtype.Int4 {
return pgtype.Int4{
Int32: int32(v.Value),
Valid: v.Valid,
}
}
// ValidFloat32 → pgtype.Float4
func (v ValidFloat32) ToPG() pgtype.Float4 {
return pgtype.Float4{
Float32: v.Value,
Valid: v.Valid,
}
}
// ValidString → pgtype.Text
func (v ValidString) ToPG() pgtype.Text {
return pgtype.Text{
String: v.Value,
Valid: v.Valid,
}
}
// ValidTime → pgtype.Timestamp
func (v ValidTime) ToPG() pgtype.Timestamp {
return pgtype.Timestamp{
Time: v.Value,
Valid: v.Valid,
}
}
// ValidBool → pgtype.Bool
func (v ValidBool) ToPG() pgtype.Bool {
return pgtype.Bool{
Bool: v.Value,
Valid: v.Valid,
}
}
type Currency int64
// ToCurrency converts a float32 to Currency
func ToCurrency(f float32) Currency {
return Currency((f * 100) + 0.5)
}
// Float32 converts a Currency to float32
func (m Currency) Float32() float32 {
x := float32(m)
x = x / 100
return x
}
// String returns a formatted Currency value
func (m Currency) String() string {
x := float32(m)
x = x / 100
return fmt.Sprintf("$%.2f", x)
}
type ResponseWDataFactory[T any] struct {
Data T `json:"data"`
Response
}
type Response struct {
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
Success bool `json:"success"`
StatusCode int `json:"status_code"`
}
func CalculateWinnings(amount Currency, totalOdds float32) Currency {
vat := amount.Float32() * 0.15
stakeAfterVat := amount.Float32() - vat
possibleWin := stakeAfterVat * totalOdds
incomeTax := possibleWin * 0.15
return ToCurrency(possibleWin - incomeTax)
}
type Pagination struct {
Total int `json:"total"`
TotalPages int `json:"total_pages"`
CurrentPage int `json:"current_page"`
Limit int `json:"limit"`
}
func PtrFloat64(v float64) *float64 { return &v }
func PtrInt64(v int64) *int64 { return &v }
type Int64JSON int64
// func (i *Int64JSON) Parse() (int64, error) {
// var asString string
// if err := json.Unmarshal(i, &asString); err == nil {
// // Try to parse the string to int64
// return strconv.ParseInt(strings.TrimSpace(asString), 10, 64)
// }
// var asInt int64
// if err := json.Unmarshal(i, &asInt); err == nil {
// return asInt, nil
// }
// // Neither string nor int worked
// return 0, fmt.Errorf("invalid int64 value: %s", string(i))
// }
func (i *Int64JSON) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err == nil {
// it was a string, parse it
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*i = Int64JSON(v)
return nil
}
var v int64
if err := json.Unmarshal(data, &v); err == nil {
*i = Int64JSON(v)
return nil
}
return fmt.Errorf("invalid int64 value: %s", string(data))
}
type NullableInt64JSON struct {
Int64 int64
Valid bool
}
func (n *NullableInt64JSON) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err == nil {
if s == "" {
n.Valid = false
return nil
}
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
n.Int64, n.Valid = v, true
return nil
}
var v int64
if err := json.Unmarshal(data, &v); err == nil {
n.Int64, n.Valid = v, true
return nil
}
return fmt.Errorf("invalid int64 value: %s", string(data))
}