func (d *DataSourceController) GetDataSources(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } search := payload["search"].(string) var query *dbox.Filter query = dbox.Or(dbox.Contains("_id", search), dbox.Contains("ConnectionID", search)) cursor, err := colonycore.Find(new(colonycore.DataSource), query) if err != nil { return helper.CreateResult(false, nil, err.Error()) } data := []colonycore.DataSource{} err = cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } defer cursor.Close() return helper.CreateResult(true, data, "") }
func (d *DataGrabberController) FindDataGrabber(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson //~ payload := map[string]string{"inputText":"test"} payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } text := payload["inputText"].(string) textLow := strings.ToLower(text) var query *dbox.Filter valueInt, errv := strconv.Atoi(text) fmt.Printf("", valueInt) fmt.Printf("", errv) if errv == nil { // == try useing Eq for support integer query = dbox.Or(dbox.Eq("GrabInterval", valueInt), dbox.Eq("TimeoutInterval", valueInt)) } else { // == try useing Contains for support autocomplite query = dbox.Or(dbox.Contains("_id", text), dbox.Contains("_id", textLow), dbox.Contains("DataSourceOrigin", text), dbox.Contains("DataSourceOrigin", textLow), dbox.Contains("DataSourceDestination", text), dbox.Contains("DataSourceDestination", textLow), dbox.Contains("IntervalType", text), dbox.Contains("IntervalType", textLow)) } data := []colonycore.DataGrabber{} cursor, err := colonycore.Find(new(colonycore.DataGrabber), query) cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } defer cursor.Close() return helper.CreateResult(true, data, "") }
func (a *DataFlowController) GetListData(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } search := tk.ToString(payload["search"]) var query *dbox.Filter if search != "" { query = dbox.Or(dbox.Contains("name", search), dbox.Contains("description", search), dbox.Contains("createdby", search)) } cursor, err := colonycore.Find(new(colonycore.DataFlow), query) dataDs := []colonycore.DataFlow{} if cursor != nil { cursor.Fetch(&dataDs, 0, false) defer cursor.Close() } if err != nil && cursor != nil { return helper.CreateResult(false, nil, err.Error()) } return helper.CreateResult(true, dataDs, "success") }
func (d *DataSourceController) FindConnection(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson //~ payload := map[string]string{"inputText":"tes","inputDrop":""} payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } text := payload["inputText"].(string) pilih := payload["inputDrop"].(string) textLow := strings.ToLower(text) // == try useing Contains for support autocomplite var query *dbox.Filter if text != "" { query = dbox.Or(dbox.Contains("_id", text), dbox.Contains("_id", textLow), dbox.Contains("Database", text), dbox.Contains("Database", textLow), dbox.Contains("Driver", text), dbox.Contains("Driver", textLow), dbox.Contains("Host", text), dbox.Contains("Host", textLow), dbox.Contains("UserName", text), dbox.Contains("UserName", textLow), dbox.Contains("Password", text), dbox.Contains("Password", textLow)) } if pilih != "" { query = dbox.And(query, dbox.Eq("Driver", pilih)) } data := []colonycore.Connection{} cursor, err := colonycore.Find(new(colonycore.Connection), query) cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } defer cursor.Close() return helper.CreateResult(true, data, "") }
func TestSelectCondition(t *testing.T) { t.Skip("Just Skip Test") c, e := prepareConnection() if e != nil { t.Errorf("Unable to connect %s \n", e.Error()) } defer c.Close() csr, e := c.NewQuery().Select("Id", "LastName", "Age"). Where(dbox.Contains("LastName", "m")). Take(10).Skip(0). Cursor(nil) if e != nil { t.Errorf("Cursor pre error: %s \n", e.Error()) return } if csr == nil { t.Errorf("Cursor not initialized") return } resultsstruct := make([]employee, 0) e = csr.Fetch(&resultsstruct, 0, false) if e != nil { t.Errorf("Unable to fetch N(0-10): %s \n", e.Error()) } else { fmt.Printf("Record count(0-10) : %v \n", csr.Count()) fmt.Printf("Fetch N(0-10) OK. Result: %v \n", resultsstruct) } csr.Close() csr, e = c.NewQuery().Select("Id", "LastName", "Age"). Where(dbox.Contains("LastName", "m")). Take(10).Skip(10). Cursor(nil) if e != nil { t.Errorf("Cursor pre error: %s \n", e.Error()) return } if csr == nil { t.Errorf("Cursor not initialized") return } resultsstruct = make([]employee, 0) e = csr.Fetch(&resultsstruct, 0, false) if e != nil { t.Errorf("Unable to fetch N(10-20): %s \n", e.Error()) } else { fmt.Printf("Record count(10-20) : %v \n", csr.Count()) fmt.Printf("Fetch N(10-20) OK. Result: %v \n", resultsstruct) } csr.Close() }
func (s *ServerController) GetServers(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson payload := struct { Search string `json:"search"` ServerOS string `json:"serverOS"` ServerType string `json:"serverType"` SSHType string `json:"sshType"` }{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } filters := []*dbox.Filter{} if payload.Search != "" { filters = append(filters, dbox.Or( dbox.Contains("_id", payload.Search), dbox.Contains("os", payload.Search), dbox.Contains("host", payload.Search), dbox.Contains("serverType", payload.Search), dbox.Contains("sshtype", payload.Search), )) } if payload.ServerOS != "" { filters = append(filters, dbox.Eq("os", payload.ServerOS)) } if payload.ServerType != "" { filters = append(filters, dbox.Eq("serverType", payload.ServerType)) } if payload.SSHType != "" { filters = append(filters, dbox.Eq("sshtype", payload.SSHType)) } var query *dbox.Filter if len(filters) > 0 { query = dbox.And(filters...) } cursor, err := colonycore.Find(new(colonycore.Server), query) if err != nil { return helper.CreateResult(false, nil, err.Error()) } data := []colonycore.Server{} err = cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } defer cursor.Close() return helper.CreateResult(true, data, "") }
func TestFilter(t *testing.T) { fb := dbox.NewFilterBuilder(new(FilterBuilder)) fb.AddFilter(dbox.Or( dbox.Contains("_id", "1"), dbox.Contains("group", "adm", "test"))) b, e := fb.Build() if e != nil { t.Errorf("Error %s", e.Error()) } else { fmt.Printf("Result:\n%v\n", toolkit.JsonString(b)) } }
func TestSelectFilter(t *testing.T) { c, e := prepareConnection() if e != nil { t.Errorf("Unable to connect %s \n", e.Error()) return } defer c.Close() csr, e := c.NewQuery().Select(). Where(dbox.Contains("fullname", "43")). From("TestUsers").Cursor(nil) if e != nil { t.Errorf("Cursor pre error: %s \n", e.Error()) return } if csr == nil { t.Errorf("Cursor not initialized") return } defer csr.Close() results := make([]map[string]interface{}, 0) e = csr.Fetch(&results, 10, false) if e != nil { t.Errorf("Unable to fetch N1: %s \n", e.Error()) } else { fmt.Printf("Fetch N1 OK. Result: %v \n", results) } csr, e = c.NewQuery(). Where(dbox.Contains("fullname", "43", "44")). From("TestUsers").Cursor(nil) if e != nil { t.Errorf("Cursor pre error: %s \n", e.Error()) return } if csr == nil { t.Errorf("Cursor not initialized") return } defer csr.Close() results = make([]map[string]interface{}, 0) e = csr.Fetch(&results, 10, false) if e != nil { t.Errorf("Unable to fetch N1: %s \n", e.Error()) } else { fmt.Printf("Fetch N2 OK. Result: %v \n", results) } }
func (d *DataBrowserController) GetBrowser(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } search := payload["search"].(string) // search = "" var query *dbox.Filter if search != "" { query = dbox.Contains("BrowserName", search) } data := []colonycore.DataBrowser{} cursor, err := colonycore.Find(new(colonycore.DataBrowser), query) if err != nil { return helper.CreateResult(false, nil, err.Error()) } err = cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } defer cursor.Close() return helper.CreateResult(true, data, "") }
func (ws *WidgetSelectorController) GetSelectorConfigs(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } var search string search = payload["search"].(string) var query *dbox.Filter if search != "" { query = dbox.Contains("_id", search) } data := []colonycore.Selector{} cursor, err := colonycore.Find(new(colonycore.Selector), query) if err != nil { return helper.CreateResult(false, nil, err.Error()) } err = cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } defer cursor.Close() return helper.CreateResult(true, data, "") }
func (l *LoginController) PrepareDefaultUser() (err error) { username := colonycore.GetConfig("default_username", "").(string) password := colonycore.GetConfig("default_password", "").(string) user := new(acl.User) filter := dbox.Contains("loginid", username) c, err := acl.Find(user, filter, nil) if err != nil { return } if c.Count() == 0 { user.ID = toolkit.RandomString(32) user.LoginID = username user.FullName = username user.Password = password user.Enable = true err = acl.Save(user) if err != nil { return } err = acl.ChangePassword(user.ID, password) if err != nil { return } fmt.Printf(`Default user "%s" with standard password has been created%s`, username, "\n") } return }
func (d *WebGrabberController) FindWebGrabber(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson //~ payload := map[string]string{"inputText": "GRAB_TEST", "inputRequest": "", "inputType": ""} payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } text := payload["inputText"].(string) req := payload["inputRequest"].(string) tipe := payload["inputType"].(string) textLow := strings.ToLower(text) // == bug, cant find if autocomplite, just full text can be get result var query *dbox.Filter if text != "" { valueInt, errv := strconv.Atoi(text) if errv == nil { // == try useing Eq for support integer query = dbox.Or(dbox.Eq("GrabInterval", valueInt), dbox.Eq("TimeoutInterval", valueInt)) } else { // == try useing Contains for support autocomplite query = dbox.Or(dbox.Contains("_id", text), dbox.Contains("_id", textLow), dbox.Contains("Calltype", text), dbox.Contains("Calltype", textLow), dbox.Contains("SourceType", text), dbox.Contains("SourceType", textLow), dbox.Contains("IntervalType", text), dbox.Contains("IntervalType", textLow)) } } if req != "" { query = dbox.And(query, dbox.Eq("Calltype", req)) } if tipe != "" { query = dbox.And(query, dbox.Eq("SourceType", tipe)) } data := []colonycore.WebGrabber{} cursor, err := colonycore.Find(new(colonycore.WebGrabber), query) cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } defer cursor.Close() return helper.CreateResult(true, payload, "") }
func (a *UserController) GetUser(r *knot.WebContext) interface{} { var filter *dbox.Filter r.Config.OutputType = knot.OutputJson _ = a.InitialSetDatabase() payload := map[string]interface{}{} err := r.GetForms(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } if strings.Contains(toolkit.TypeName(payload["find"]), "float") { payload["find"] = toolkit.ToInt(payload["find"], toolkit.RoundingAuto) } tUser := new(acl.User) if find := toolkit.ToString(payload["find"]); find != "" { filter = new(dbox.Filter) filter = dbox.Or(dbox.Contains("id", find), dbox.Contains("fullname", find), dbox.Contains("email", find)) } take := toolkit.ToInt(payload["take"], toolkit.RoundingAuto) skip := toolkit.ToInt(payload["skip"], toolkit.RoundingAuto) c, err := acl.Find(tUser, filter, toolkit.M{}.Set("take", take).Set("skip", skip)) if err != nil { return helper.CreateResult(true, nil, err.Error()) } data := toolkit.M{} arrm := make([]toolkit.M, 0, 0) err = c.Fetch(&arrm, 0, false) c.Close() c, err = acl.Find(tUser, filter, nil) data.Set("Datas", arrm) data.Set("total", c.Count()) if err != nil { return helper.CreateResult(true, nil, err.Error()) } else { return helper.CreateResult(true, data, "") } }
func (a *GroupController) Search(r *knot.WebContext) interface{} { var filter *dbox.Filter r.Config.OutputType = knot.OutputJson _ = a.InitialSetDatabase() payload := map[string]interface{}{} err := r.GetForms(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } if strings.Contains(toolkit.TypeName(payload["search"]), "float") { payload["search"] = toolkit.ToInt(payload["search"], toolkit.RoundingAuto) } tGroup := new(acl.Group) if search := toolkit.ToString(payload["search"]); search != "" { filter = new(dbox.Filter) filter = dbox.Or(dbox.Contains("_id", search), dbox.Contains("title", search), dbox.Contains("owner", search)) } fmt.Println(filter) take := toolkit.ToInt(payload["take"], toolkit.RoundingAuto) skip := toolkit.ToInt(payload["skip"], toolkit.RoundingAuto) c, err := acl.Find(tGroup, filter, toolkit.M{}.Set("take", take).Set("skip", skip)) if err != nil { return helper.CreateResult(true, nil, err.Error()) } data := toolkit.M{} arrm := make([]toolkit.M, 0, 0) err = c.Fetch(&arrm, 0, false) data.Set("Datas", arrm) data.Set("total", c.Count()) if err != nil { return helper.CreateResult(true, nil, err.Error()) } return helper.CreateResult(true, data, "") }
func (a *AdministrationController) GetAccess(r *knot.WebContext) interface{} { var filter *dbox.Filter r.Config.OutputType = knot.OutputJson a.InitialSetDatabase() payload := map[string]interface{}{} err := r.GetForms(&payload) if strings.Contains(toolkit.TypeName(payload["find"]), "float") { payload["find"] = toolkit.ToInt(payload["find"], toolkit.RoundingAuto) } tAccess := new(acl.Access) if find := toolkit.ToString(payload["find"]); find != "" { filter = new(dbox.Filter) filter = dbox.Or(dbox.Contains("id", find), dbox.Contains("title", find), dbox.Contains("group1", find), dbox.Contains("group2", find), dbox.Contains("group3", find), dbox.Contains("specialaccess1", find), dbox.Contains("specialaccess2", find), dbox.Contains("specialaccess3", find), dbox.Contains("specialaccess4", find)) } data := toolkit.M{} arrm := make([]toolkit.M, 0, 0) take := toolkit.ToInt(payload["take"], toolkit.RoundingAuto) skip := toolkit.ToInt(payload["skip"], toolkit.RoundingAuto) c, err := acl.Find(tAccess, filter, toolkit.M{}.Set("take", take).Set("skip", skip)) if err == nil { err = c.Fetch(&arrm, 0, false) } c.Close() c, err = acl.Find(tAccess, filter, nil) data.Set("Datas", arrm) data.Set("total", c.Count()) if err != nil { return helper.CreateResult(true, nil, err.Error()) } else { return helper.CreateResult(true, data, "") } }
func (d *DataSourceController) GetConnections(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } search := "" if payload["search"] != nil { search = payload["search"].(string) } driver := "" if payload["driver"] != nil { search = payload["driver"].(string) } // search := payload["search"] // driver := payload["driver"] var query *dbox.Filter query = dbox.Or(dbox.Contains("_id", search), dbox.Contains("Driver", search), dbox.Contains("Host", search), dbox.Contains("Database", search), dbox.Contains("UserName", search)) if driver != "" { query = dbox.And(query, dbox.Eq("Driver", driver)) } data := []colonycore.Connection{} cursor, err := colonycore.Find(new(colonycore.Connection), query) if err != nil { return helper.CreateResult(false, nil, err.Error()) } err = cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } defer cursor.Close() return helper.CreateResult(true, data, "") }
func (a *SessionController) GetSession(r *knot.WebContext) interface{} { var filter *dbox.Filter r.Config.OutputType = knot.OutputJson _ = a.InitialSetDatabase() payload := map[string]interface{}{} err := r.GetForms(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } if strings.Contains(toolkit.TypeName(payload["find"]), "float") { payload["find"] = toolkit.ToInt(payload["find"], toolkit.RoundingAuto) } tSession := new(acl.Session) if find := toolkit.ToString(payload["find"]); find != "" { filter = new(dbox.Filter) filter = dbox.Contains("loginid", find) } take := toolkit.ToInt(payload["take"], toolkit.RoundingAuto) skip := toolkit.ToInt(payload["skip"], toolkit.RoundingAuto) // c, err := acl.Find(tAccess, filter, toolkit.M{}.Set("take", take).Set("skip", skip)) // c, err := acl.Find(tSession, nil, toolkit.M{}.Set("take", payload["take"].(int)).Set("skip", payload["skip"].(int))) c, err := acl.Find(tSession, filter, toolkit.M{}.Set("take", take).Set("skip", skip)) if err != nil { return helper.CreateResult(true, nil, err.Error()) } data := toolkit.M{} arrm := make([]toolkit.M, 0, 0) err = c.Fetch(&arrm, 0, false) for i, val := range arrm { arrm[i].Set("duration", time.Since(val["created"].(time.Time)).Hours()) arrm[i].Set("status", "ACTIVE") if val["expired"].(time.Time).Before(time.Now().UTC()) { arrm[i].Set("duration", val["expired"].(time.Time).Sub(val["created"].(time.Time)).Hours()) arrm[i].Set("status", "EXPIRED") } } c.Close() c, err = acl.Find(tSession, filter, nil) data.Set("Datas", arrm) data.Set("total", c.Count()) if err != nil { return helper.CreateResult(true, nil, err.Error()) } else { return helper.CreateResult(true, data, "") } }
func (a *UserController) Search(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson a.InitialSetDatabase() payload := map[string]interface{}{} err := r.GetPayload(&payload) find := payload["search"].(string) bfind, err := strconv.ParseBool(find) tUser := new(acl.User) arrm := make([]toolkit.M, 0, 0) filter := dbox.Or(dbox.Contains("_id", find), dbox.Contains("id", find), dbox.Contains("loginid", find), dbox.Contains("fullname", find), dbox.Contains("email", find), dbox.Eq("enable", bfind)) c, e := acl.Find(tUser, filter, toolkit.M{}.Set("take", 0)) if e == nil { e = c.Fetch(&arrm, 0, false) } if e != nil { return helper.CreateResult(true, nil, err.Error()) } else { return helper.CreateResult(true, arrm, "") } }
func (d *DataSourceController) FindDataSource(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } text := payload["inputText"].(string) textLow := strings.ToLower(text) // == try useing Contains for support autocomplite var query *dbox.Filter query = dbox.Or(dbox.Contains("_id", text), dbox.Contains("_id", textLow), dbox.Contains("ConnectionID", text), dbox.Contains("ConnectionID", textLow)) data := []colonycore.DataSource{} cursor, err := colonycore.Find(new(colonycore.DataSource), query) cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } // == bug, i dont know what i can to do if find by database name.== //~ if data == nil { //~ query = dbox.Eq("Database",text) //~ data := []colonycore.Connection{} //~ cursor, err := colonycore.Find(new(colonycore.Connection), query) //~ cursor.Fetch(&data, 0, false) //~ if err != nil { //~ return helper.CreateResult(false, nil, err.Error()) //~ } //~ fmt.Printf("========asdasd=======%#v",data) //~ } defer cursor.Close() return helper.CreateResult(true, data, "") }
func filterCondition(cond toolkit.M) *dbox.Filter { fb := new(dbox.Filter) for key, val := range cond { if key == "$and" || key == "$or" { afb := []*dbox.Filter{} for _, sVal := range val.([]interface{}) { rVal := sVal.(map[string]interface{}) mVal := toolkit.M{} for rKey, mapVal := range rVal { mVal.Set(rKey, mapVal) } afb = append(afb, filterCondition(mVal)) } if key == "$and" { fb = dbox.And(afb...) } else { fb = dbox.Or(afb...) } } else { if reflect.ValueOf(val).Kind() == reflect.Map { mVal := val.(map[string]interface{}) tomVal, _ := toolkit.ToM(mVal) switch { case tomVal.Has("$eq"): fb = dbox.Eq(key, tomVal["$eq"].(string)) case tomVal.Has("$ne"): fb = dbox.Ne(key, tomVal["$ne"].(string)) case tomVal.Has("$regex"): fb = dbox.Contains(key, tomVal["$regex"].(string)) case tomVal.Has("$gt"): fb = dbox.Gt(key, tomVal["$gt"].(string)) case tomVal.Has("$gte"): fb = dbox.Gte(key, tomVal["$gte"].(string)) case tomVal.Has("$lt"): fb = dbox.Lt(key, tomVal["$lt"].(string)) case tomVal.Has("$lte"): fb = dbox.Lte(key, tomVal["$lte"].(string)) } } else { fb = dbox.Eq(key, val) } } } return fb }
func (w *Widget) Get(search string) ([]Widget, error) { var query *dbox.Filter if search != "" { query = dbox.Contains("_id", search) } data := []Widget{} cursor, err := Find(new(Widget), query) if err != nil { return nil, err } if err := cursor.Fetch(&data, 0, false); err != nil { return nil, err } defer cursor.Close() return data, nil }
func (p *Page) GetPages(search string) []Page { var query *dbox.Filter if search != "" { query = dbox.Contains("_id", search) } data := []Page{} cursor, err := Find(new(Page), query) if err != nil { return data } if err := cursor.Fetch(&data, 0, false); err != nil { return data } defer cursor.Close() return data }
func TestDelete(t *testing.T) { t.Skip() skipIfConnectionIsNil(t) e := ctx.NewQuery(). Delete(). From(tableName). Where(dbox.Contains("nama", "item")). Exec(nil) if e != nil { t.Fatalf("Delete fail: %s", e.Error()) } operation = "Test Delete" sintaks = ` ctx.NewQuery(). Delete(). From(tableName). Where(dbox.Contains("nama", "item")). Exec(nil)` TestSelect(t) }
func (mg *MapGrid) Get(search string) ([]MapGrid, error) { var query *dbox.Filter if search != "" { query = dbox.Contains("_id", search) } mapgrid := []MapGrid{} cursor, err := Find(new(MapGrid), query) if err != nil { return mapgrid, err } err = cursor.Fetch(&mapgrid, 0, false) if err != nil { return mapgrid, err } defer cursor.Close() return mapgrid, nil }
func (a *AdministrationController) Search(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson a.InitialSetDatabase() payload := map[string]interface{}{} err := r.GetPayload(&payload) find := payload["search"].(string) bfind, err := strconv.ParseBool(find) tAccess := new(acl.Access) arrm := make([]toolkit.M, 0, 0) filter := dbox.Or(dbox.Contains("_id", find), dbox.Contains("id", find), dbox.Contains("title", find), dbox.Contains("group1", find), dbox.Contains("group2", find), dbox.Contains("group3", find), dbox.Contains("specialaccess1", find), dbox.Contains("specialaccess2", find), dbox.Contains("specialaccess3", find), dbox.Contains("specialaccess4", find), dbox.Eq("enable", bfind)) c, e := acl.Find(tAccess, filter, toolkit.M{}.Set("take", 0)) if e == nil { e = c.Fetch(&arrm, 0, false) } if e != nil { return helper.CreateResult(true, nil, err.Error()) } else { return helper.CreateResult(true, arrm, "") } }
func TestFilter(t *testing.T) { fb := dbox.NewFilterBuilder(new(FilterBuilder)) fb.AddFilter(dbox.Or( dbox.Contains("regfield", "1"), dbox.Ne("nefield", 1), dbox.Eq("group", "administrators"))) b, e := fb.Build() if e != nil { t.Errorf("Error %s", e.Error()) } else { fmt.Printf("Result:\n%v\n", toolkit.JsonString(b)) } fb = dbox.NewFilterBuilder(new(FilterBuilder)) fb.AddFilter(dbox.And(dbox.Or(dbox.Eq("EmployeeId", "101-102-10"), dbox.Eq("EmployeeId", "101-102-3"), dbox.Eq("EmployeeId", "101-102-4")), dbox.Eq("Age", "30"))) c, e := fb.Build() if e != nil { t.Errorf("Error %s", e.Error()) } else { fmt.Printf("Result:\n%v\n", toolkit.JsonString(c)) } }
func TestUpdate(t *testing.T) { t.Skip() skipIfConnectionIsNil(t) e := ctx.NewQuery(). Update(). From(tableName). Where(dbox.Contains("nama", "item")). Exec(toolkit.M{}.Set("data", toolkit.M{}.Set("nama", "itemUpdate"))) if e != nil { t.Fatalf("Update fail: %s", e.Error()) } operation = "Test Update" sintaks = ` ctx.NewQuery(). Update(). From(tableName). Where(dbox.Contains("nama", "item")). Exec(toolkit.M{}.Set("data", toolkit.M{}. Set("nama", "itemUpdate")))` TestSelect(t) }
func (w *WebGrabberController) GetScrapperData(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } search := payload["search"].(string) requesttype := payload["requesttype"].(string) sourcetype := payload["sourcetype"].(string) var query *dbox.Filter query = dbox.Or(dbox.Contains("_id", search)) if sourcetype == "" { //default sourcetype == "SourceType_HttpHtml" query = dbox.And(query, dbox.Eq("sourcetype", "SourceType_HttpHtml")) } else { query = dbox.And(query, dbox.Eq("sourcetype", sourcetype)) } if requesttype != "" { query = dbox.And(query, dbox.Eq("grabconf.calltype", requesttype)) } cursor, err := colonycore.Find(new(colonycore.WebGrabber), query) if err != nil { return helper.CreateResult(false, nil, err.Error()) } data := []colonycore.WebGrabber{} err = cursor.Fetch(&data, 0, false) if err != nil { return helper.CreateResult(false, nil, err.Error()) } defer cursor.Close() return helper.CreateResult(true, data, "") }
func (c *AdministrationController) DeactivateUser(k *knot.WebContext) interface{} { d := struct { Email string }{} e := k.GetPayload(&d) if e != nil { return ResultInfo(nil, e) } /*csr, e := c.Ctx.Connection.NewQuery().From(new(UserModel).TableName()).Where(dbox.Eq("Email", d.Email)).Cursor(nil) result := new(UserModel) e = csr.Fetch(&result, 1, false) defer csr.Close() e = c.Ctx.Connection.NewQuery().From(new(UserModel).TableName()).Where(dbox.Eq("Email", d.Email)).Delete().Exec(nil) if e != nil { return ResultInfo(nil, e) } data := result data.Enable = false e = c.Ctx.Save(data)*/ e = c.Ctx.Connection.NewQuery(). Update(). From(new(UserModel).TableName()). Where(dbox.Contains("email", d.Email)). Exec(toolkit.M{}.Set("data", toolkit.M{}.Set("enable", false))) if e != nil { return ResultInfo(nil, e) } return ResultInfo(nil, e) }
func (d *DataSourceController) filterParse(where toolkit.M) *dbox.Filter { field := where.Get("field", "").(string) value := fmt.Sprintf("%v", where["value"]) if key := where.Get("key", "").(string); key == "Eq" { valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"])) if errv == nil { return dbox.Eq(field, valueInt) } else { return dbox.Eq(field, value) } } else if key == "Ne" { valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"])) if errv == nil { return dbox.Ne(field, valueInt) } else { return dbox.Ne(field, value) } } else if key == "Lt" { valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"])) if errv == nil { return dbox.Lt(field, valueInt) } else { return dbox.Lt(field, value) } } else if key == "Lte" { valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"])) if errv == nil { return dbox.Lte(field, valueInt) } else { return dbox.Lte(field, value) } } else if key == "Gt" { valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"])) if errv == nil { return dbox.Gt(field, valueInt) } else { return dbox.Gt(field, value) } } else if key == "Gte" { valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"])) if errv == nil { return dbox.Gte(field, valueInt) } else { return dbox.Gte(field, value) } } else if key == "In" { valueArray := []interface{}{} for _, e := range strings.Split(value, ",") { valueArray = append(valueArray, strings.Trim(e, "")) } return dbox.In(field, valueArray...) } else if key == "Nin" { valueArray := []interface{}{} for _, e := range strings.Split(value, ",") { valueArray = append(valueArray, strings.Trim(e, "")) } return dbox.Nin(field, valueArray...) } else if key == "Contains" { return dbox.Contains(field, value) } else if key == "Or" { subs := where.Get("value", []interface{}{}).([]interface{}) filtersToMerge := []*dbox.Filter{} for _, eachSub := range subs { eachWhere, _ := toolkit.ToM(eachSub) filtersToMerge = append(filtersToMerge, d.filterParse(eachWhere)) } return dbox.Or(filtersToMerge...) } else if key == "And" { subs := where.Get("value", []interface{}{}).([]interface{}) filtersToMerge := []*dbox.Filter{} for _, eachSub := range subs { eachWhere, _ := toolkit.ToM(eachSub) filtersToMerge = append(filtersToMerge, d.filterParse(eachWhere)) } return dbox.And(filtersToMerge...) } return nil }