Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
fix: do not list cities with no events on home page (#12)
Browse files Browse the repository at this point in the history
* fix: do not list cities with no events on home page

* fix: add time margin before make event disappear
  • Loading branch information
eze-kiel authored Jun 12, 2022
1 parent cd5181a commit ceb217d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ func (a App) homePage(c *gin.Context) {

// todo: better error handling
// if the following fails, it is not critical. Should we display an error?
p.Cities, err = events.GetCitiesWithEvents()
// p.Cities, err = events.GetCitiesWithEvents()
p.Cities, err = events.GetCitiesWithFutureEvents()
if err != nil {
log.Errorf("cannot get cities list: %s", err)
}
Expand Down
38 changes: 38 additions & 0 deletions events/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,41 @@ func UpdateEventReports(id string, num int) error {
_, err := database.DB.Query(`UPDATE events SET num_of_reports=$1 WHERE id=$2;`, num, id)
return err
}

func GetCitiesWithFutureEvents() ([]string, error) {
rows, err := database.DB.Query(`SELECT city, date FROM events;`)
if err != nil {
return nil, err
}

type row struct {
City string `db:"city"`
Date string `db:"date"`
}
var cities []string

for rows.Next() {
var r row
if err := rows.Scan(&r.City, &r.Date); err != nil {
return nil, err
}

if r.City != "" {
dateTmp, err := time.Parse(time.RFC3339, r.Date)
if err != nil {
return cities, err
}

if time.Now().After(dateTmp.Add(time.Hour * 8)) {
continue
}

// avoid duplicatas
if !utils.StringInSlice(r.City, cities) {
cities = append(cities, r.City)
}
}
}
sort.Strings(cities)
return cities, nil
}

0 comments on commit ceb217d

Please sign in to comment.