From 65936259e9312ec9022525ef8747e68456e6eafd Mon Sep 17 00:00:00 2001 From: kelmith Date: Tue, 21 Oct 2025 01:23:22 +0530 Subject: [PATCH] GET /gobounties/code/:code endpoint to get bounties by code --- db/db.go | 6 ++++++ db/interface.go | 1 + handlers/bounty.go | 24 +++++++++++++++++++++++- routes/bounty.go | 1 + 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/db/db.go b/db/db.go index 5b6c30ecd..6d6aef943 100644 --- a/db/db.go +++ b/db/db.go @@ -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{}{ diff --git a/db/interface.go b/db/interface.go index 8c02cad7a..e59a0ab22 100644 --- a/db/interface.go +++ b/db/interface.go @@ -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) diff --git a/handlers/bounty.go b/handlers/bounty.go index db00f148e..19c007540 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -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 @@ -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 } diff --git a/routes/bounty.go b/routes/bounty.go index 5d96bf2f4..c3157283b 100644 --- a/routes/bounty.go +++ b/routes/bounty.go @@ -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)