diff --git a/db/db.go b/db/db.go index 3aa3fcab9..c030aae36 100644 --- a/db/db.go +++ b/db/db.go @@ -437,9 +437,35 @@ func (db database) GetUserBountiesCount(personKey string, tabType string) int64 return count } -func (db database) GetBountiesCount() int64 { +func (db database) GetBountiesCount(r *http.Request) int64 { + keys := r.URL.Query() + open := keys.Get("Open") + assingned := keys.Get("Assigned") + paid := keys.Get("Paid") + openQuery := "" + assignedQuery := "" + paidQuery := "" + + if open != "" && open == "true" { + openQuery = "AND assignee = ''" + assignedQuery = "" + } + if assingned != "" && assingned == "true" { + if open != "" && open == "true" { + assignedQuery = "OR assignee != ''" + } else { + assignedQuery = "AND assignee != ''" + } + } + if paid != "" && paid == "true" { + paidQuery = "AND paid = true" + } + var count int64 - db.db.Model(&Bounty{}).Where("show != ?", false).Count(&count) + + query := "SELECT COUNT(*) FROM bounty WHERE show != false" + allQuery := query + " " + openQuery + " " + assignedQuery + " " + paidQuery + db.db.Raw(allQuery).Scan(&count) return count } @@ -447,11 +473,18 @@ func (db database) GetOrganizationBounties(r *http.Request, org_uuid string) []B keys := r.URL.Query() tags := keys.Get("tags") // this is a string of tags separated by commas offset, limit, sortBy, direction, search := utils.GetPaginationParams(r) + open := keys.Get("Open") + assingned := keys.Get("Assigned") + paid := keys.Get("Paid") ms := []Bounty{} orderQuery := "" limitQuery := "" searchQuery := "" + openQuery := "" + assignedQuery := "" + paidQuery := "" + if sortBy != "" && direction != "" { orderQuery = "ORDER BY " + sortBy + " " + direction } else { @@ -463,10 +496,24 @@ func (db database) GetOrganizationBounties(r *http.Request, org_uuid string) []B if search != "" { searchQuery = fmt.Sprintf("WHERE LOWER(title) LIKE %s", "'%"+search+"%'") } + if open != "" && open == "true" { + openQuery = "AND assignee = ''" + assignedQuery = "" + } + if assingned != "" && assingned == "true" { + if open != "" && open == "true" { + assignedQuery = "OR assignee != ''" + } else { + assignedQuery = "AND assignee != ''" + } + } + if paid != "" && paid == "true" { + paidQuery = "AND paid = true" + } - rawQuery := `SELECT * FROM bounty WHERE org_uuid = '` + org_uuid + `'` - - theQuery := db.db.Raw(rawQuery + " " + searchQuery + " " + orderQuery + " " + limitQuery) + query := `SELECT * FROM bounty WHERE org_uuid = '` + org_uuid + `'` + allQuery := query + " " + openQuery + " " + assignedQuery + " " + paidQuery + " " + searchQuery + " " + orderQuery + " " + limitQuery + theQuery := db.db.Raw(allQuery) if tags != "" { // pull out the tags and add them in here @@ -514,12 +561,18 @@ func (db database) GetAllBounties(r *http.Request) []Bounty { keys := r.URL.Query() tags := keys.Get("tags") // this is a string of tags separated by commas offset, limit, sortBy, direction, search := utils.GetPaginationParams(r) + open := keys.Get("Open") + assingned := keys.Get("Assigned") + paid := keys.Get("Paid") ms := []Bounty{} orderQuery := "" limitQuery := "" searchQuery := "" + openQuery := "" + assignedQuery := "" + paidQuery := "" if sortBy != "" && direction != "" { orderQuery = "ORDER BY " + sortBy + " " + direction @@ -532,10 +585,24 @@ func (db database) GetAllBounties(r *http.Request) []Bounty { if search != "" { searchQuery = fmt.Sprintf("AND LOWER(title) LIKE %s", "'%"+search+"%'") } + if open != "" && open == "true" { + openQuery = "AND assignee = ''" + assignedQuery = "" + } + if assingned != "" && assingned == "true" { + if open != "" && open == "true" { + assignedQuery = "OR assignee != ''" + } else { + assignedQuery = "AND assignee != ''" + } + } + if paid != "" && paid == "true" { + paidQuery = "AND paid = true" + } query := "SELECT * FROM public.bounty WHERE show != false" - allQuery := query + " " + searchQuery + " " + orderQuery + " " + limitQuery + allQuery := query + " " + openQuery + " " + assignedQuery + " " + paidQuery + " " + searchQuery + " " + orderQuery + " " + limitQuery theQuery := db.db.Raw(allQuery) diff --git a/db/db_codes_test.go b/db/db_codes_test.go index 4caad51cf..9a552688e 100644 --- a/db/db_codes_test.go +++ b/db/db_codes_test.go @@ -28,9 +28,7 @@ func TestCodeGet(t *testing.T) { defer db.Close() gorm.Open("postgres", db) - rows := sqlmock.NewRows([]string{"connection_string", "date_created", "is_used", "date_created"}).AddRow(code.ID, code.ConnectionString, code.IsUsed, code.DateCreated) - mock.ExpectQuery(regexp.QuoteMeta( `SELECT connection_string, date_created FROM connectioncodes WHERE is_used = ? ORDER BY id DESC LIMIT 1`)). WithArgs(false). diff --git a/db/store_test.go b/db/store_test.go index f6c365ace..3d404f1e3 100644 --- a/db/store_test.go +++ b/db/store_test.go @@ -9,9 +9,7 @@ func TestSetCache(t *testing.T) { var value = "Trial" InitCache() - Store.SetCache(key, value) - cacheValue, err := Store.GetCache(key) if err != nil { @@ -30,7 +28,6 @@ func TestDeleteCache(t *testing.T) { InitCache() Store.SetCache(key, value) - cacheValue, err := Store.GetCache(key) if err != nil { @@ -42,7 +39,6 @@ func TestDeleteCache(t *testing.T) { } Store.DeleteCache(key) - _, errD := Store.GetCache(key) if errD == nil { @@ -60,9 +56,7 @@ func TestSetLnCache(t *testing.T) { } InitCache() - Store.SetLnCache(key, value) - cacheValue, err := Store.GetLnCache(key) if err != nil { diff --git a/handlers/bounty.go b/handlers/bounty.go index e0dbc6315..d4e15e523 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -71,7 +71,7 @@ func GetUserBountyCount(w http.ResponseWriter, r *http.Request) { } func GetBountyCount(w http.ResponseWriter, r *http.Request) { - bountyCount := db.DB.GetBountiesCount() + bountyCount := db.DB.GetBountiesCount(r) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyCount) }