Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bounty.go handlers GetPersonCreatedBounties, GetPersonAssignedBounties, & GetBountyByCreated #1537

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler {

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

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyResponse)
}

func GetBountyById(w http.ResponseWriter, r *http.Request) {
func (h *bountyHandler) GetBountyById(w http.ResponseWriter, r *http.Request) {
bountyId := chi.URLParam(r, "bountyId")
if bountyId == "" {
w.WriteHeader(http.StatusNotFound)
}
bounties, err := db.DB.GetBountyById(bountyId)
bounties, err := h.db.GetBountyById(bountyId)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
} else {
var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties)
var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyResponse)
}
Expand Down Expand Up @@ -100,12 +100,12 @@ func (h *bountyHandler) GetOrganizationPreviousBountyByCreated(w http.ResponseWr
}
}

func GetBountyIndexById(w http.ResponseWriter, r *http.Request) {
func (h *bountyHandler) GetBountyIndexById(w http.ResponseWriter, r *http.Request) {
bountyId := chi.URLParam(r, "bountyId")
if bountyId == "" {
w.WriteHeader(http.StatusNotFound)
}
bountyIndex := db.DB.GetBountyIndexById(bountyId)
bountyIndex := h.db.GetBountyIndexById(bountyId)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyIndex)
}
Expand All @@ -120,7 +120,8 @@ func (h *bountyHandler) GetBountyByCreated(w http.ResponseWriter, r *http.Reques
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
} else {
var bountyResponse []db.BountyResponse = h.generateBountyResponse(bounties)
var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyResponse)
}
Expand All @@ -145,25 +146,25 @@ func GetBountyCount(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(bountyCount)
}

func GetPersonCreatedBounties(w http.ResponseWriter, r *http.Request) {
bounties, err := db.DB.GetCreatedBounties(r)
func (h *bountyHandler) GetPersonCreatedBounties(w http.ResponseWriter, r *http.Request) {
bounties, err := h.db.GetCreatedBounties(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
} else {
var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties)
var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyResponse)
}
}

func GetPersonAssignedBounties(w http.ResponseWriter, r *http.Request) {
bounties, err := db.DB.GetAssignedBounties(r)
func (h *bountyHandler) GetPersonAssignedBounties(w http.ResponseWriter, r *http.Request) {
bounties, err := h.db.GetAssignedBounties(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
} else {
var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties)
var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyResponse)
}
Expand Down Expand Up @@ -319,15 +320,15 @@ func UpdatePaymentStatus(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(bounty)
}

func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse {
func (h *bountyHandler) GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse {
var bountyResponse []db.BountyResponse

for i := 0; i < len(bounties); i++ {
bounty := bounties[i]

owner := db.DB.GetPersonByPubkey(bounty.OwnerID)
assignee := db.DB.GetPersonByPubkey(bounty.Assignee)
organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid)
owner := h.db.GetPersonByPubkey(bounty.OwnerID)
assignee := h.db.GetPersonByPubkey(bounty.Assignee)
organization := h.db.GetOrganizationByUuid(bounty.OrgUuid)

b := db.BountyResponse{
Bounty: db.Bounty{
Expand Down
126 changes: 105 additions & 21 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -451,45 +452,126 @@ func TestDeleteBounty(t *testing.T) {
}

func TestGetBountyByCreated(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
mockDb := dbMocks.NewDatabase(t)
mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
return []db.BountyResponse{} // Mocked response
}
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)

t.Run("Should return bounty by its created value", func(t *testing.T) {
bHandler.generateBountyResponse = mockGenerateBountyResponse

expectedBounty := []db.Bounty{{
t.Run("Should return if bounty is present in db", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetBountyByCreated)
bounty := db.Bounty{
ID: 1,
Type: "type1",
Title: "Test Bounty",
Description: "Description",
Created: 123456789,
}}
mockDb.On("GetBountyDataByCreated", "123456789").Return(expectedBounty, nil).Once()
Type: "coding",
Title: "first bounty",
Description: "first bounty description",
OrgUuid: "org-1",
Assignee: "user1",
Created: 1707991475,
OwnerID: "owner-1",
}
createdStr := strconv.FormatInt(bounty.Created, 10)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("created", "1707991475")
req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "gobounties/created/1707991475", nil)
mockDb.On("GetBountyDataByCreated", createdStr).Return([]db.Bounty{bounty}, nil).Once()
mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}).Once()
mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once()
mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}).Once()
handler.ServeHTTP(rr, req)

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

})
}

func TestGetPersonAssignedBounties(t *testing.T) {
mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)
t.Run("Should successfull Get Person Assigned Bounties", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetBountyByCreated)
handler := http.HandlerFunc(bHandler.GetPersonAssignedBounties)
bounty := db.Bounty{
ID: 1,
Type: "coding",
Title: "first bounty",
Description: "first bounty description",
OrgUuid: "org-1",
Assignee: "user1",
Created: 1707991475,
OwnerID: "owner-1",
}

req, err := http.NewRequestWithContext(ctx, "GET", "/bounty/123456789", nil)
if err != nil {
t.Fatal(err)
rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", "clu80datu2rjujsmim40")
rctx.URLParams.Add("sortBy", "paid")
rctx.URLParams.Add("page", "1")
rctx.URLParams.Add("limit", "20")
rctx.URLParams.Add("search", "")
req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "people/wanteds/assigned/clu80datu2rjujsmim40?sortBy=paid&page=1&limit=20&search=", nil)

mockDb.On("GetAssignedBounties", req).Return([]db.Bounty{bounty}, nil).Once()
mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}, nil).Once()
mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}, nil).Once()
mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}, nil).Once()
handler.ServeHTTP(rr, req)

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

func TestGetPersonCreatedBounties(t *testing.T) {
mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)
t.Run("Should successfull Get Person Created Bounties", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetPersonCreatedBounties)
bounty := db.Bounty{
ID: 1,
Type: "coding",
Title: "first bounty",
Description: "first bounty description",
OrgUuid: "org-1",
Assignee: "user1",
Created: 1707991475,
OwnerID: "owner-1",
}
chiCtx := chi.NewRouteContext()
chiCtx.URLParams.Add("created", "123456789")
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chiCtx))

rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", "clu80datu2rjujsmim40")
rctx.URLParams.Add("sortBy", "paid")
rctx.URLParams.Add("page", "1")
rctx.URLParams.Add("limit", "20")
rctx.URLParams.Add("search", "")
req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "people/wanteds/created/clu80datu2rjujsmim40?sortBy=paid&page=1&limit=20&search=", nil)

mockDb.On("GetCreatedBounties", req).Return([]db.Bounty{bounty}, nil).Once()
mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}, nil).Once()
mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}, nil).Once()
mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}, nil).Once()
handler.ServeHTTP(rr, req)

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

func TestGetNextBountyByCreated(t *testing.T) {
ctx := context.Background()

mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)
Expand All @@ -509,6 +591,7 @@ func TestGetNextBountyByCreated(t *testing.T) {

func TestGetPreviousBountyByCreated(t *testing.T) {
ctx := context.Background()

mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)
Expand Down Expand Up @@ -560,6 +643,7 @@ func TestGetOrganizationPreviousBountyByCreated(t *testing.T) {
bHandler.GetOrganizationPreviousBountyByCreated(rr, req.WithContext(ctx))

assert.Equal(t, http.StatusOK, rr.Code)

mockDb.AssertExpectations(t)
})
}
84 changes: 84 additions & 0 deletions handlers/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,87 @@ func (oh *organizationHandler) DeleteOrganization(w http.ResponseWriter, r *http
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(org)
}

func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse {
var bountyResponse []db.BountyResponse

for i := 0; i < len(bounties); i++ {
bounty := bounties[i]

owner := db.DB.GetPersonByPubkey(bounty.OwnerID)
assignee := db.DB.GetPersonByPubkey(bounty.Assignee)
organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid)

b := db.BountyResponse{
Bounty: db.Bounty{
ID: bounty.ID,
OwnerID: bounty.OwnerID,
Paid: bounty.Paid,
Show: bounty.Show,
Type: bounty.Type,
Award: bounty.Award,
AssignedHours: bounty.AssignedHours,
BountyExpires: bounty.BountyExpires,
CommitmentFee: bounty.CommitmentFee,
Price: bounty.Price,
Title: bounty.Title,
Tribe: bounty.Tribe,
Created: bounty.Created,
Assignee: bounty.Assignee,
TicketUrl: bounty.TicketUrl,
Description: bounty.Description,
WantedType: bounty.WantedType,
Deliverables: bounty.Deliverables,
GithubDescription: bounty.GithubDescription,
OneSentenceSummary: bounty.OneSentenceSummary,
EstimatedSessionLength: bounty.EstimatedSessionLength,
EstimatedCompletionDate: bounty.EstimatedCompletionDate,
OrgUuid: bounty.OrgUuid,
Updated: bounty.Updated,
CodingLanguages: bounty.CodingLanguages,
},
Assignee: db.Person{
ID: assignee.ID,
Uuid: assignee.Uuid,
OwnerPubKey: assignee.OwnerPubKey,
OwnerAlias: assignee.OwnerAlias,
UniqueName: assignee.UniqueName,
Description: assignee.Description,
Tags: assignee.Tags,
Img: assignee.Img,
Created: assignee.Created,
Updated: assignee.Updated,
LastLogin: assignee.LastLogin,
OwnerRouteHint: assignee.OwnerRouteHint,
OwnerContactKey: assignee.OwnerContactKey,
PriceToMeet: assignee.PriceToMeet,
TwitterConfirmed: assignee.TwitterConfirmed,
},
Owner: db.Person{
ID: owner.ID,
Uuid: owner.Uuid,
OwnerPubKey: owner.OwnerPubKey,
OwnerAlias: owner.OwnerAlias,
UniqueName: owner.UniqueName,
Description: owner.Description,
Tags: owner.Tags,
Img: owner.Img,
Created: owner.Created,
Updated: owner.Updated,
LastLogin: owner.LastLogin,
OwnerRouteHint: owner.OwnerRouteHint,
OwnerContactKey: owner.OwnerContactKey,
PriceToMeet: owner.PriceToMeet,
TwitterConfirmed: owner.TwitterConfirmed,
},
Organization: db.OrganizationShort{
Name: organization.Name,
Uuid: organization.Uuid,
Img: organization.Img,
},
}
bountyResponse = append(bountyResponse, b)
}

return bountyResponse
}
6 changes: 4 additions & 2 deletions routes/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ func BountyRoutes() chi.Router {
bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB)
r.Group(func(r chi.Router) {
r.Get("/all", bountyHandler.GetAllBounties)
r.Get("/id/{bountyId}", handlers.GetBountyById)
r.Get("/index/{bountyId}", handlers.GetBountyIndexById)

r.Get("/id/{bountyId}", bountyHandler.GetBountyById)
r.Get("/index/{bountyId}", bountyHandler.GetBountyIndexById)
r.Get("/next/{created}", bountyHandler.GetNextBountyByCreated)
r.Get("/previous/{created}", bountyHandler.GetPreviousBountyByCreated)
r.Get("/org/next/{uuid}/{created}", bountyHandler.GetOrganizationNextBountyByCreated)
r.Get("/org/previous/{uuid}/{created}", bountyHandler.GetOrganizationPreviousBountyByCreated)

r.Get("/created/{created}", bountyHandler.GetBountyByCreated)
r.Get("/count/{personKey}/{tabType}", handlers.GetUserBountyCount)
r.Get("/count", handlers.GetBountyCount)
Expand Down
Loading
Loading