add line webhook router
This commit is contained in:
+146
@@ -0,0 +1,146 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// Cards -
|
||||
type Cards struct {
|
||||
ID string `db:"id" cc:"id"`
|
||||
Name string `db:"name" cc:"name"`
|
||||
CMC float32 `db:"cmc" cc:"cmc"`
|
||||
ManaCost string `db:"mana_cost" cc:"mana_cost"`
|
||||
Set string `db:"set" cc:"set"`
|
||||
Text string `db:"text" cc:"text"`
|
||||
Layout string `db:"layout" cc:"layout"`
|
||||
ImageURL string `db:"image_url" cc:"image_url"`
|
||||
Loyalty string `db:"loyalty" cc:"loyalty"`
|
||||
Type string `db:"type" cc:"type"`
|
||||
Number string `db:"number" cc:"number"`
|
||||
Power string `db:"power" cc:"power"`
|
||||
Toughness string `db:"toughness" cc:"toughness"`
|
||||
Names []string `db:"names" cc:"names"`
|
||||
Colors []string `db:"colors" cc:"colors"`
|
||||
ColorIdentity []string `db:"color_identity" cc:"color_identity"`
|
||||
Types []string `db:"types" cc:"types"`
|
||||
Subtypes []string `db:"subtypes" cc:"subtypes"`
|
||||
Supertypes []string `db:"supertypes" cc:"supertypes"`
|
||||
}
|
||||
|
||||
// order opts
|
||||
const (
|
||||
CardSearchOrderByName OrderStr = `c."name" asc`
|
||||
CardSearchOrderByNameDesc = `c."name" desc`
|
||||
CardSearchOrderByCMC = `c."cmc" asc`
|
||||
CardSearchOrderByCMCDesc = `c."cmc" desc"`
|
||||
)
|
||||
|
||||
// CardSearchCMC -
|
||||
type CardSearchCMC struct {
|
||||
Op OpStr
|
||||
Val float32
|
||||
}
|
||||
|
||||
// ListCardsOpts -
|
||||
type ListCardsOpts struct {
|
||||
OrderBy OrderStr
|
||||
Name string
|
||||
CMC *CardSearchCMC
|
||||
Set string
|
||||
Types []string
|
||||
Limit int
|
||||
Offset int
|
||||
}
|
||||
|
||||
// CountCards -
|
||||
func CountCards(arg ListCardsOpts) (c int, err error) {
|
||||
c = 0
|
||||
query := `select count(*) as count from "public"."cards" c`
|
||||
where := struct {
|
||||
Name string `db:"name"`
|
||||
CMC float32 `db:"name"`
|
||||
Set string `db:"set"`
|
||||
}{}
|
||||
|
||||
whereStr := []string{}
|
||||
if len(arg.Name) > 0 {
|
||||
where.Name = arg.Name
|
||||
whereStr = append(whereStr, `c."name" = :name`)
|
||||
}
|
||||
if len(arg.Set) > 0 {
|
||||
where.Set = arg.Set
|
||||
whereStr = append(whereStr, `c."set" = :set`)
|
||||
}
|
||||
if arg.CMC != nil {
|
||||
where.CMC = arg.CMC.Val
|
||||
whereStr = append(whereStr, fmt.Sprintf(`c."cmc" %s :cmc`, arg.CMC.Op))
|
||||
}
|
||||
|
||||
wStr := ""
|
||||
if len(whereStr) > 0 {
|
||||
wStr = fmt.Sprintf("where %s", strings.Join(whereStr, " and "))
|
||||
}
|
||||
|
||||
query = fmt.Sprintf("%s %s",
|
||||
query,
|
||||
wStr)
|
||||
q, a, err := sqlx.Named(query, where)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
err = x.Get(&c, q, a...)
|
||||
return
|
||||
}
|
||||
|
||||
// ListCards -
|
||||
func ListCards(arg ListCardsOpts) (cards []*Cards, err error) {
|
||||
if arg.Limit < 1 {
|
||||
arg.Limit = 1
|
||||
}
|
||||
if arg.Offset < 0 {
|
||||
arg.Offset = 0
|
||||
}
|
||||
query := `select c.id, c.name, c.cmc, c.mana_cost, c.text, c.layout, c.image_url, c.loyalty,
|
||||
c.type, c.number, c.power, c.toughness, c.set from "public"."cards" c`
|
||||
where := struct {
|
||||
Name string `db:"name"`
|
||||
CMC float32 `db:"cmc"`
|
||||
Set string `db:"set"`
|
||||
}{}
|
||||
whereStr := []string{}
|
||||
if len(arg.Name) > 0 {
|
||||
where.Name = arg.Name
|
||||
whereStr = append(whereStr, `c."name" = :name`)
|
||||
}
|
||||
if len(arg.Set) > 0 {
|
||||
where.Set = arg.Set
|
||||
whereStr = append(whereStr, `c."set" = :set`)
|
||||
}
|
||||
if arg.CMC != nil {
|
||||
where.CMC = arg.CMC.Val
|
||||
whereStr = append(whereStr, fmt.Sprintf(`c."cmc" %s :cmc`, arg.CMC.Op))
|
||||
}
|
||||
|
||||
wStr := ""
|
||||
if len(whereStr) > 0 {
|
||||
wStr = fmt.Sprintf("where %s", strings.Join(whereStr, " and "))
|
||||
}
|
||||
|
||||
query = fmt.Sprintf("%s %s %s %s",
|
||||
query,
|
||||
wStr,
|
||||
arg.OrderBy,
|
||||
fmt.Sprintf("limit %d offset %d", arg.Limit, arg.Offset))
|
||||
q, a, err := sqlx.Named(query, where)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = x.Select(&cards, q, a...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.trj.tw/golang/mtgbot/modules/config"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var x *sqlx.DB
|
||||
|
||||
// NewDB -
|
||||
func NewDB() *sqlx.DB {
|
||||
var err error
|
||||
conf := config.GetConf()
|
||||
userPassStr := conf.Database.User
|
||||
if len(conf.Database.Pass) > 0 {
|
||||
userPassStr += ":" + conf.Database.Pass
|
||||
}
|
||||
|
||||
connStr := fmt.Sprintf("postgres://%s@%s/%s?sslmode=disable", userPassStr, conf.Database.Host, conf.Database.DB)
|
||||
|
||||
x, err = sqlx.Connect("postgres", connStr)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
x.SetMaxIdleConns(10)
|
||||
x.SetMaxOpenConns(100)
|
||||
err = x.Ping()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
// String - get string ptr
|
||||
func String(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
// Int - get int ptr
|
||||
func Int(i int) *int {
|
||||
return &i
|
||||
}
|
||||
|
||||
// OrderStr -
|
||||
type OrderStr string
|
||||
|
||||
func (o OrderStr) String() string {
|
||||
return string(o)
|
||||
}
|
||||
|
||||
// OpStr -
|
||||
type OpStr string
|
||||
|
||||
func (s OpStr) String() string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
// Op var
|
||||
const (
|
||||
SearchOpEq OpStr = "="
|
||||
SearchOpNeq = "!="
|
||||
SearchOpLt = "<"
|
||||
SearchOpLte = "<="
|
||||
SearchOpGt = ">"
|
||||
SearchOpGte = ">="
|
||||
)
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// Sets - product set
|
||||
type Sets struct {
|
||||
Code string `db:"code" cc:"code"`
|
||||
Name string `db:"name" cc:"name"`
|
||||
ReleaseDate time.Time `db:"release_date" cc:"release_date"`
|
||||
}
|
||||
|
||||
// ListSetsOpts -
|
||||
type ListSetsOpts struct {
|
||||
OrderBy OrderStr
|
||||
Code []string
|
||||
Limit, Offset int
|
||||
}
|
||||
|
||||
// order opts
|
||||
const (
|
||||
SetSearchOrderByName OrderStr = `s."name" asc`
|
||||
SetSearchOrderByNameDesc = `s."name" desc`
|
||||
)
|
||||
|
||||
// CountSets -
|
||||
func CountSets(arg ListSetsOpts) (c int, err error) {
|
||||
c = 0
|
||||
query := `select count(*) as count from "public"."sets" s`
|
||||
|
||||
where := struct {
|
||||
Code []string `db:"code"`
|
||||
}{}
|
||||
whereStr := []string{}
|
||||
|
||||
if len(arg.Code) > 0 {
|
||||
where.Code = arg.Code
|
||||
whereStr = append(whereStr, `s."code" in (:code)`)
|
||||
}
|
||||
|
||||
wStr := ""
|
||||
if len(whereStr) > 0 {
|
||||
wStr = fmt.Sprintf(`where %s`, strings.Join(whereStr, " and "))
|
||||
}
|
||||
|
||||
query = fmt.Sprintf("%s %s", query, wStr)
|
||||
|
||||
query, args, err := sqlx.Named(query, where)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
query, args, err = sqlx.In(query, args)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
query = x.Rebind(query)
|
||||
|
||||
err = x.Get(&c, query, args...)
|
||||
return
|
||||
}
|
||||
|
||||
// ListSets -
|
||||
func ListSets(arg ListSetsOpts) (sets []*Sets, err error) {
|
||||
if arg.Limit < 1 {
|
||||
arg.Limit = 1
|
||||
}
|
||||
if arg.Offset < 0 {
|
||||
arg.Offset = 0
|
||||
}
|
||||
query := `select s.code, s.name, s.release_date from "public"."sets" s`
|
||||
|
||||
where := struct {
|
||||
Code []string `db:"code"`
|
||||
}{}
|
||||
whereStr := []string{}
|
||||
|
||||
if len(arg.Code) > 0 {
|
||||
where.Code = arg.Code
|
||||
whereStr = append(whereStr, `s."code" in (:code)`)
|
||||
}
|
||||
|
||||
wStr := ""
|
||||
if len(whereStr) > 0 {
|
||||
wStr = fmt.Sprintf("where %s", strings.Join(whereStr, " and "))
|
||||
}
|
||||
|
||||
query = fmt.Sprintf("%s %s %s %s",
|
||||
query,
|
||||
wStr,
|
||||
arg.OrderBy,
|
||||
fmt.Sprintf("limit %d offset %d", arg.Limit, arg.Offset))
|
||||
|
||||
query, args, err := sqlx.Named(query, where)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query, args, err = sqlx.In(query, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query = x.Rebind(query)
|
||||
|
||||
err = x.Select(&sets, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user