Skip to content

Commit

Permalink
fixed next and previous bounty
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Feb 5, 2024
1 parent eb3591a commit 1e986fb
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 97 deletions.
240 changes: 231 additions & 9 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,27 +712,249 @@ func (db database) GetBountyById(id string) ([]Bounty, error) {
return ms, err
}

func (db database) GetNextBountyById(id string) ([]Bounty, error) {
func (db database) GetNextBountyById(r *http.Request) ([]Bounty, error) {
id := chi.URLParam(r, "bountyId")
keys := r.URL.Query()
_, _, _, _, search := utils.GetPaginationParams(r)
ms := []Bounty{}
err := db.db.Raw(`SELECT * FROM public.bounty WHERE id > '` + id + `' AND show = true ORDER BY id ASC LIMIT 1`).Find(&ms).Error

open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)

var languageQuery string
var statusQuery string
var searchQuery string
var statusConditions []string

if search != "" {
searchQuery = fmt.Sprintf("AND LOWER(title) LIKE %s", "'%"+strings.ToLower(search)+"%'")
}

if open == "true" {
statusConditions = append(statusConditions, "assignee = '' AND paid != true")
}
if assingned == "true" {
statusConditions = append(statusConditions, "assignee != '' AND paid = false")
}
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
} else {
statusQuery = ""
}

if languageLength > 0 {
langs := ""
for i, val := range languageArray {
if val != "" {
if i == 0 {
langs = "'" + val + "'"
} else {
langs = langs + ", '" + val + "'"
}
languageQuery = "AND coding_languages && ARRAY[" + langs + "]"
}
}
}

query := `SELECT * FROM public.bounty WHERE id > '` + id + `' AND show = true`
orderQuery := "ORDER BY id ASC LIMIT 1"

allQuery := query + " " + searchQuery + " " + statusQuery + " " + languageQuery + " " + orderQuery

err := db.db.Raw(allQuery).Find(&ms).Error
return ms, err
}

func (db database) GetPreviousBountyById(id string) ([]Bounty, error) {
func (db database) GetPreviousBountyById(r *http.Request) ([]Bounty, error) {
id := chi.URLParam(r, "bountyId")
keys := r.URL.Query()
_, _, _, _, search := utils.GetPaginationParams(r)
ms := []Bounty{}
err := db.db.Raw(`SELECT * FROM public.bounty WHERE id < '` + id + `' AND show = true ORDER BY id DESC LIMIT 1`).Find(&ms).Error

open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)

var languageQuery string
var statusQuery string
var searchQuery string
var statusConditions []string

if search != "" {
searchQuery = fmt.Sprintf("AND LOWER(title) LIKE %s", "'%"+strings.ToLower(search)+"%'")
}

if open == "true" {
statusConditions = append(statusConditions, "assignee = '' AND paid != true")
}
if assingned == "true" {
statusConditions = append(statusConditions, "assignee != '' AND paid = false")
}
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
} else {
statusQuery = ""
}

if languageLength > 0 {
langs := ""
for i, val := range languageArray {
if val != "" {
if i == 0 {
langs = "'" + val + "'"
} else {
langs = langs + ", '" + val + "'"
}
languageQuery = "AND coding_languages && ARRAY[" + langs + "]"
}
}
}

query := `SELECT * FROM public.bounty WHERE id < '` + id + `' AND show = true`
orderQuery := "ORDER BY id DESC LIMIT 1"

allQuery := query + " " + searchQuery + " " + statusQuery + " " + languageQuery + " " + orderQuery

err := db.db.Raw(allQuery).Find(&ms).Error
return ms, err
}

func (db database) GetNextOrganizationBountyById(uuid string, id string) ([]Bounty, error) {
func (db database) GetNextOrganizationBountyById(r *http.Request) ([]Bounty, error) {
id := chi.URLParam(r, "bountyId")
uuid := chi.URLParam(r, "uuid")
keys := r.URL.Query()
_, _, _, _, search := utils.GetPaginationParams(r)
ms := []Bounty{}
err := db.db.Raw(`SELECT * FROM public.bounty WHERE org_uuid = '` + uuid + `' AND id > '` + id + `' AND show = true ORDER BY id ASC LIMIT 1`).Find(&ms).Error

open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)

var languageQuery string
var statusQuery string
var searchQuery string
var statusConditions []string

if search != "" {
searchQuery = fmt.Sprintf("AND LOWER(title) LIKE %s", "'%"+strings.ToLower(search)+"%'")
}

if open == "true" {
statusConditions = append(statusConditions, "assignee = '' AND paid != true")
}
if assingned == "true" {
statusConditions = append(statusConditions, "assignee != '' AND paid = false")
}
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
} else {
statusQuery = ""
}

if languageLength > 0 {
langs := ""
for i, val := range languageArray {
if val != "" {
if i == 0 {
langs = "'" + val + "'"
} else {
langs = langs + ", '" + val + "'"
}
languageQuery = "AND coding_languages && ARRAY[" + langs + "]"
}
}
}

query := `SELECT * FROM public.bounty WHERE org_uuid = '` + uuid + `' AND id > '` + id + `' AND show = true`
orderQuery := "ORDER BY id ASC LIMIT 1"

allQuery := query + " " + searchQuery + " " + statusQuery + " " + languageQuery + " " + orderQuery

err := db.db.Raw(allQuery).Find(&ms).Error
return ms, err
}

func (db database) GetPreviousOrganizationBountyById(uuid string, id string) ([]Bounty, error) {
func (db database) GetPreviousOrganizationBountyById(r *http.Request) ([]Bounty, error) {
id := chi.URLParam(r, "bountyId")
uuid := chi.URLParam(r, "uuid")
keys := r.URL.Query()
_, _, _, _, search := utils.GetPaginationParams(r)
ms := []Bounty{}
err := db.db.Raw(`SELECT * FROM public.bounty WHERE org_uuid = '` + uuid + `' AND id < '` + id + `' AND show = true ORDER BY id DESC LIMIT 1`).Find(&ms).Error

open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)

var languageQuery string
var statusQuery string
var searchQuery string
var statusConditions []string

if search != "" {
searchQuery = fmt.Sprintf("AND LOWER(title) LIKE %s", "'%"+strings.ToLower(search)+"%'")
}

if open == "true" {
statusConditions = append(statusConditions, "assignee = '' AND paid != true")
}
if assingned == "true" {
statusConditions = append(statusConditions, "assignee != '' AND paid = false")
}
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
} else {
statusQuery = ""
}

if languageLength > 0 {
langs := ""
for i, val := range languageArray {
if val != "" {
if i == 0 {
langs = "'" + val + "'"
} else {
langs = langs + ", '" + val + "'"
}
languageQuery = "AND coding_languages && ARRAY[" + langs + "]"
}
}
}

query := `SELECT * FROM public.bounty WHERE org_uuid = '` + uuid + `' AND id < '` + id + `' AND show = true`
orderQuery := "ORDER BY id DESC LIMIT 1"

allQuery := query + " " + searchQuery + " " + statusQuery + " " + languageQuery + " " + orderQuery

err := db.db.Raw(allQuery).Find(&ms).Error
return ms, err
}

Expand Down Expand Up @@ -785,7 +1007,7 @@ func (db database) GetAllBounties(r *http.Request) []Bounty {
limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
}
if search != "" {
searchQuery = fmt.Sprintf("AND LOWER(title) LIKE %s", "'%"+search+"%'")
searchQuery = fmt.Sprintf("AND LOWER(title) LIKE %s", "'%"+strings.ToLower(search)+"%'")
}
if open != "" && open == "true" {
openQuery = "AND assignee = '' AND paid != true"
Expand Down
8 changes: 4 additions & 4 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ type Database interface {
GetAssignedBounties(r *http.Request) ([]Bounty, error)
GetCreatedBounties(r *http.Request) ([]Bounty, error)
GetBountyById(id string) ([]Bounty, error)
GetNextBountyById(id string) ([]Bounty, error)
GetPreviousBountyById(id string) ([]Bounty, error)
GetNextOrganizationBountyById(uuid string, id string) ([]Bounty, error)
GetPreviousOrganizationBountyById(uuid string, id string) ([]Bounty, error)
GetNextBountyById(r *http.Request) ([]Bounty, error)
GetPreviousBountyById(r *http.Request) ([]Bounty, error)
GetNextOrganizationBountyById(r *http.Request) ([]Bounty, error)
GetPreviousOrganizationBountyById(r *http.Request) ([]Bounty, error)
GetBountyIndexById(id string) int64
GetBountyDataByCreated(created string) ([]Bounty, error)
AddBounty(b Bounty) (Bounty, error)
Expand Down
26 changes: 4 additions & 22 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ func GetBountyById(w http.ResponseWriter, r *http.Request) {
}

func GetNextBountyById(w http.ResponseWriter, r *http.Request) {
bountyId := chi.URLParam(r, "bountyId")
if bountyId == "" {
w.WriteHeader(http.StatusNotFound)
}
bounties, err := db.DB.GetNextBountyById(bountyId)
bounties, err := db.DB.GetNextBountyById(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
Expand All @@ -71,11 +67,7 @@ func GetNextBountyById(w http.ResponseWriter, r *http.Request) {
}

func GetPreviousBountyById(w http.ResponseWriter, r *http.Request) {
bountyId := chi.URLParam(r, "bountyId")
if bountyId == "" {
w.WriteHeader(http.StatusNotFound)
}
bounties, err := db.DB.GetPreviousBountyById(bountyId)
bounties, err := db.DB.GetPreviousBountyById(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
Expand All @@ -87,12 +79,7 @@ func GetPreviousBountyById(w http.ResponseWriter, r *http.Request) {
}

func GetOrganizationNextBountyById(w http.ResponseWriter, r *http.Request) {
bountyId := chi.URLParam(r, "bountyId")
uuid := chi.URLParam(r, "uuid")
if bountyId == "" || uuid == "" {
w.WriteHeader(http.StatusNotFound)
}
bounties, err := db.DB.GetNextOrganizationBountyById(uuid, bountyId)
bounties, err := db.DB.GetNextOrganizationBountyById(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
Expand All @@ -104,12 +91,7 @@ func GetOrganizationNextBountyById(w http.ResponseWriter, r *http.Request) {
}

func GetOrganizationPreviousBountyById(w http.ResponseWriter, r *http.Request) {
bountyId := chi.URLParam(r, "bountyId")
uuid := chi.URLParam(r, "uuid")
if bountyId == "" || uuid == "" {
w.WriteHeader(http.StatusNotFound)
}
bounties, err := db.DB.GetPreviousOrganizationBountyById(uuid, bountyId)
bounties, err := db.DB.GetPreviousOrganizationBountyById(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
Expand Down
Loading

0 comments on commit 1e986fb

Please sign in to comment.