replaycli-go/api/dataapi/lib.go

132 lines
2.6 KiB
Go
Raw Normal View History

2021-11-05 13:11:04 +00:00
package dataapi
import (
"strconv"
"time"
2021-11-29 15:11:14 +00:00
"go.digitalcircle.com.br/open/httpcli"
2021-11-05 13:11:04 +00:00
)
type DataApiOp string
const (
RETRIEVE DataApiOp = "R"
CREATE DataApiOp = "C"
DELETE DataApiOp = "D"
UPDATE DataApiOp = "U"
)
type DataAPIRequest struct {
Col string `json:"col"`
Q string `json:"q"`
Op DataApiOp `json:"op"`
Id string `json:"id"`
Data map[string]interface{} `json:"data"`
}
type DataAPIResponse struct {
Msg string `json:"msg"`
Id string `json:"id"`
Data []interface{} `json:"data"`
}
type Cli struct {
Httpcli *httpcli.Cli
}
func (c *Cli) Do(request *DataAPIRequest) (response *DataAPIResponse, err error) {
response = &DataAPIResponse{}
err = c.Httpcli.JsonPost("/", request, response)
return
}
2021-12-16 14:08:10 +00:00
func NewCli(apikey string) *Cli {
2021-11-05 13:11:04 +00:00
ret := &Cli{Httpcli: httpcli.NewCli()}
2021-11-05 13:13:48 +00:00
ret.Httpcli.AddHeader("X-API-KEY", apikey)
2021-11-05 13:11:04 +00:00
ret.Httpcli.SetBasePath("https://dataapi.digitalcircle.com.br")
return ret
2021-11-05 21:09:09 +00:00
}
2022-02-09 17:26:25 +00:00
func RegistrarExec(table, apikey string, check bool, checkC bool) {
//Para robôs, age como contador e registra cada repetição no mês
2021-12-16 14:08:10 +00:00
c := NewCli(apikey)
2021-11-09 12:43:25 +00:00
var registros int
var erro int
2022-02-09 17:26:25 +00:00
captReg := 0
current := time.Now().Format("2006-01")
res, err := c.Do(&DataAPIRequest{
Col: table,
Op: RETRIEVE,
Q: "@[?date=='" + current + "']",
})
if err != nil {
panic(err)
}
if len(res.Data) > 0 {
2021-11-09 12:43:25 +00:00
if check {
result := res.Data[0].(map[string]interface{})
registros = int(result["exec"].(float64))
registros = registros + 1
2022-02-09 17:26:25 +00:00
captReg = int(result["capt"].(float64))
if checkC {
captReg = captReg + 1
}
2021-11-09 12:43:25 +00:00
ident := strconv.Itoa(int(result["ID"].(float64)))
2021-11-09 12:43:25 +00:00
_, err = c.Do(&DataAPIRequest{
Col: table,
Op: UPDATE,
Id: ident,
Data: map[string]interface{}{
"exec": registros,
2022-02-09 17:26:25 +00:00
"capt": captReg,
2021-11-09 12:43:25 +00:00
},
})
if err != nil {
panic(err)
}
} else {
result := res.Data[0].(map[string]interface{})
registros = int(result["err"].(float64))
registros = registros + 1
ident := strconv.Itoa(int(result["ID"].(float64)))
_, err = c.Do(&DataAPIRequest{
Col: table,
Op: UPDATE,
Id: ident,
Data: map[string]interface{}{
"err": registros,
},
})
if err != nil {
panic(err)
}
}
} else {
2021-11-09 12:43:25 +00:00
if check {
registros = 1
erro = 0
2022-02-09 17:26:25 +00:00
if checkC {
captReg = 1
}
2021-11-09 12:43:25 +00:00
} else {
registros = 0
erro = 1
}
_, err = c.Do(&DataAPIRequest{
Col: table,
Op: CREATE,
Data: map[string]interface{}{
"date": current,
2021-11-09 12:43:25 +00:00
"exec": registros,
"err": erro,
2022-02-09 17:26:25 +00:00
"capt": captReg,
},
})
if err != nil {
panic(err)
}
}
}