Skip to content

Commit

Permalink
Merge pull request #1066 from stakwork/fix/backend_filter
Browse files Browse the repository at this point in the history
Sent status request to the backend
  • Loading branch information
elraphty authored Dec 9, 2023
2 parents 4b2f8ef + f7196c0 commit 3b65351
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
79 changes: 73 additions & 6 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,21 +437,54 @@ 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
}

func (db database) GetOrganizationBounties(r *http.Request, org_uuid string) []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
} else {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions db/db_codes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
6 changes: 0 additions & 6 deletions db/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,7 +28,6 @@ func TestDeleteCache(t *testing.T) {
InitCache()

Store.SetCache(key, value)

cacheValue, err := Store.GetCache(key)

if err != nil {
Expand All @@ -42,7 +39,6 @@ func TestDeleteCache(t *testing.T) {
}

Store.DeleteCache(key)

_, errD := Store.GetCache(key)

if errD == nil {
Expand All @@ -60,9 +56,7 @@ func TestSetLnCache(t *testing.T) {
}

InitCache()

Store.SetLnCache(key, value)

cacheValue, err := Store.GetLnCache(key)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 3b65351

Please sign in to comment.