Skip to content

Commit

Permalink
fix-getallbounties
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Nov 7, 2024
1 parent 3433b8c commit 9cec431
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
7 changes: 3 additions & 4 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ func (db database) AddBounty(b Bounty) (Bounty, error) {

func (db database) GetAllBounties(r *http.Request) []NewBounty {
keys := r.URL.Query()
tags := keys.Get("tags") // this is a string of tags separated by commas
tags := keys.Get("tags")
offset, limit, sortBy, direction, search := utils.GetPaginationParams(r)
open := keys.Get("Open")
assingned := keys.Get("Assigned")
Expand Down Expand Up @@ -1208,7 +1208,7 @@ func (db database) GetAllBounties(r *http.Request) []NewBounty {
if sortBy != "" && direction != "" {
orderQuery = "ORDER BY " + sortBy + " " + direction
} else {
orderQuery = "ORDER BY " + sortBy + "" + "DESC"
orderQuery = "ORDER BY created DESC"
}
if limit != 0 {
limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
Expand Down Expand Up @@ -1270,14 +1270,13 @@ func (db database) GetAllBounties(r *http.Request) []NewBounty {
}
}

query := "SELECT * FROM public.bounty WHERE show != false"
query := "SELECT * FROM public.bounty"

allQuery := query + " " + statusQuery + " " + searchQuery + " " + workspaceQuery + " " + languageQuery + " " + phaseUuidQuery + " " + phasePriorityQuery + " " + orderQuery + " " + limitQuery

theQuery := db.db.Raw(allQuery)

if tags != "" {
// pull out the tags and add them in here
t := strings.Split(tags, ",")
for _, s := range t {
theQuery = theQuery.Where("'" + s + "'" + " = any (tags)")
Expand Down
10 changes: 9 additions & 1 deletion handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ func NewBountyHandler(httpClient HttpClient, database db.Database) *bountyHandle

func (h *bountyHandler) GetAllBounties(w http.ResponseWriter, r *http.Request) {
bounties := h.db.GetAllBounties(r)
var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties)

var publicBounties []db.NewBounty
for _, bounty := range bounties {
if bounty.Show {
publicBounties = append(publicBounties, bounty)
}
}

var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(publicBounties)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyResponse)
Expand Down
80 changes: 71 additions & 9 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,21 +1201,79 @@ func TestGetAllBounties(t *testing.T) {

t.Run("Should successfully return all bounties", func(t *testing.T) {
now := time.Now().Unix()
bounty := db.NewBounty{

// Create a public bounty
publicBounty := db.NewBounty{
Type: "coding",
Title: "Bounty With ID",
Description: "Bounty ID description",
Title: "Public Bounty",
Description: "Public Bounty description",
WorkspaceUuid: "",
Assignee: "",
OwnerID: "test-owner",
Show: true,
Created: now,
}
db.TestDB.CreateOrEditBounty(bounty)
db.TestDB.CreateOrEditBounty(publicBounty)

bountyInDb, err := db.TestDB.GetBountyByCreated(uint(bounty.Created))
// Create a private bounty
privateBounty := db.NewBounty{
Type: "coding",
Title: "Private Bounty",
Description: "Private Bounty description",
WorkspaceUuid: "",
Assignee: "",
OwnerID: "test-owner",
Show: false,
Created: now + 1,
}
db.TestDB.CreateOrEditBounty(privateBounty)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetAllBounties)

rctx := chi.NewRouteContext()
req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/all", nil)

handler.ServeHTTP(rr, req)

var returnedBounties []db.BountyResponse
err := json.Unmarshal(rr.Body.Bytes(), &returnedBounties)
assert.NoError(t, err)
assert.NotNil(t, bountyInDb)
assert.Equal(t, http.StatusOK, rr.Code)
assert.NotEmpty(t, returnedBounties)

// Ensure both bounties are returned
assert.Equal(t, 2, len(returnedBounties))
})

t.Run("Should successfully return only public bounties", func(t *testing.T) {
now := time.Now().Unix()

// Create a public bounty
publicBounty := db.NewBounty{
Type: "coding",
Title: "Public Bounty",
Description: "Public Bounty description",
WorkspaceUuid: "",
Assignee: "",
OwnerID: "test-owner",
Show: true,
Created: now,
}
db.TestDB.CreateOrEditBounty(publicBounty)

// Create a private bounty
privateBounty := db.NewBounty{
Type: "coding",
Title: "Private Bounty",
Description: "Private Bounty description",
WorkspaceUuid: "",
Assignee: "",
OwnerID: "test-owner",
Show: false,
Created: now + 1,
}
db.TestDB.CreateOrEditBounty(privateBounty)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetAllBounties)
Expand All @@ -1225,11 +1283,15 @@ func TestGetAllBounties(t *testing.T) {

handler.ServeHTTP(rr, req)

var returnedBounty []db.BountyResponse
err = json.Unmarshal(rr.Body.Bytes(), &returnedBounty)
var returnedBounties []db.BountyResponse
err := json.Unmarshal(rr.Body.Bytes(), &returnedBounties)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.NotEmpty(t, returnedBounty)
assert.NotEmpty(t, returnedBounties)

// Ensure only the public bounty is returned
assert.Equal(t, 1, len(returnedBounties))
assert.Equal(t, publicBounty.Title, returnedBounties[0].Bounty.Title)
})
}

Expand Down

0 comments on commit 9cec431

Please sign in to comment.