Skip to content

Commit

Permalink
Merge pull request stakwork#1538 from gouravmpk/tests#1526
Browse files Browse the repository at this point in the history
bounty.go handlers GetAllBounties, GetBountyById, & GetBountyIndexById stakwork#1526
  • Loading branch information
elraphty authored Feb 27, 2024
2 parents 0a40462 + d43b4dd commit fb8fd74
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 53 deletions.
33 changes: 17 additions & 16 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@ type bountyHandler struct {

func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler {
return &bountyHandler{
httpClient: httpClient,
db: db,
generateBountyResponse: GenerateBountyResponse,

httpClient: httpClient,
db: db,
}
}

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 Down Expand Up @@ -151,7 +152,7 @@ func (h *bountyHandler) GetPersonCreatedBounties(w http.ResponseWriter, r *http.
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 @@ -163,7 +164,7 @@ func (h *bountyHandler) GetPersonAssignedBounties(w http.ResponseWriter, r *http
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 Down Expand Up @@ -320,15 +321,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
Loading

0 comments on commit fb8fd74

Please sign in to comment.