diff --git a/api/dataapi/lib.go b/api/dataapi/lib.go index aae3cc4..1909cc3 100644 --- a/api/dataapi/lib.go +++ b/api/dataapi/lib.go @@ -1,6 +1,9 @@ package dataapi import ( + "strconv" + "time" + "go.digitalcircle.com.br/open/httpcli" ) @@ -58,3 +61,47 @@ func LCR_Create() { panic(err) } } + +func RegistrarExec(table string) { + //Para robôs, age como contador e registra cada repetição no mês + c := NewCli("RnJpIDA1IE5vdiAyMDIxIDExOjE1OjIyIEFNIFVUQwo") + 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 { + result := res.Data[0].(map[string]interface{}) + registros := result["exec"].(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{}{ + "exec": registros, + }, + }) + if err != nil { + panic(err) + } + } else { + _, err = c.Do(&DataAPIRequest{ + Col: table, + Op: CREATE, + Data: map[string]interface{}{ + "date": current, + "exec": 1, + }, + }) + if err != nil { + panic(err) + } + } +} diff --git a/api/dataapi/lib_test.go b/api/dataapi/lib_test.go index 7c03e6a..953ff4f 100644 --- a/api/dataapi/lib_test.go +++ b/api/dataapi/lib_test.go @@ -98,48 +98,3 @@ func LCR_Create(t *testing.T) { log.Printf("%#v", res) } - -/*func CreateControl(t *testing.T, table string) { - c := dataapi.NewCli("RnJpIDA1IE5vdiAyMDIxIDExOjE1OjIyIEFNIFVUQwo") - current := time.Now().Format("2006-01") - res, err := c.Do(&dataapi.DataAPIRequest{ - Col: table, - Op: dataapi.CREATE, - Data: map[string]interface{}{ - "date": current, - "qtd": 0, - }, - }) - if err != nil { - t.Fatal(err.Error()) - } - - log.Printf("%#v", res) -} - -func UpdateControl(t *testing.T, table string) { - c := dataapi.NewCli("RnJpIDA1IE5vdiAyMDIxIDExOjE1OjIyIEFNIFVUQwo") - current := time.Now().Format("2006-01") - res, err := c.Do(&dataapi.DataAPIRequest{ - Col: table, - Op: dataapi.RETRIEVE, - Q: "@[?date=="+current+"]", - }) - if err != nil { - t.Fatal(err.Error()) - } - res.Data()[0] - res, err := c.Do(&dataapi.DataAPIRequest{ - Col: "C1", - Op: dataapi.UPDATE, - Id: "2", - Data: map[string]interface{}{ - "F2": "Novo Campo", - }, - }) - if err != nil { - t.Fatal(err.Error()) - } - - log.Printf("%#v", res) -}*/