Skip to content
Merged
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
6 changes: 6 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,12 @@ func (db database) GetBounty(id uint) NewBounty {
return b
}

func (db database) GetBountyByUnlockCode(code string) (NewBounty, error) {
b := NewBounty{}
err := db.db.Where("unlock_code = ?", code).Where("show = ?", true).First(&b).Error
return b, err
}

func (db database) UpdateBountyPaymentStatuses(bounty NewBounty) (NewBounty, error) {

bountyUpdates := map[string]interface{}{
Expand Down
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Database interface {
DeleteBounty(pubkey string, created string) (NewBounty, error)
GetBountyByCreated(created uint) (NewBounty, error)
GetBounty(id uint) NewBounty
GetBountyByUnlockCode(code string) (NewBounty, error)
UpdateBounty(b NewBounty) (NewBounty, error)
UpdateBountyPaymentStatuses(bounty NewBounty) (NewBounty, error)
UpdateBountyPayment(b NewBounty) (NewBounty, error)
Expand Down
24 changes: 23 additions & 1 deletion handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ func (h *bountyHandler) GetBountyById(w http.ResponseWriter, r *http.Request) {
}
}

func (h *bountyHandler) GetBountyByCode(w http.ResponseWriter, r *http.Request) {
code := chi.URLParam(r, "code")
if code == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Unlock code required"})
return
}

bounty, err := h.db.GetBountyByUnlockCode(code)
if err != nil {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "Bounty not found"})
return
}

bounties := []db.NewBounty{bounty}
bountyResponse := h.GenerateBountyResponse(bounties)

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

// GetNextBountyByCreated godoc
//
// @Summary Get next bounty
Expand Down Expand Up @@ -461,7 +483,7 @@ func (h *bountyHandler) CreateOrEditBounty(w http.ResponseWriter, r *http.Reques
}
}

if bounty.UnlockCode == nil {
if bounty.UnlockCode == nil || *bounty.UnlockCode == "" {
code := generateUnlockCode()
bounty.UnlockCode = &code
}
Expand Down
1 change: 1 addition & 0 deletions routes/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func BountyRoutes() chi.Router {
r.Get("/all", bountyHandler.GetAllBounties)
r.Get("/featured/all", bountyHandler.GetAllFeaturedBounties)

r.Get("/code/{code}", bountyHandler.GetBountyByCode)
r.Get("/id/{bountyId}", bountyHandler.GetBountyById)
r.Get("/index/{bountyId}", bountyHandler.GetBountyIndexById)
r.Get("/next/{created}", bountyHandler.GetNextBountyByCreated)
Expand Down
Loading