diff options
-rw-r--r-- | config.yml | 4 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | hosted/tmpl/index.html | 2 | ||||
-rw-r--r-- | hosted/tmpl/past.html | 31 | ||||
-rw-r--r-- | hosted/tmpl/pastTalks.html | 41 | ||||
-rw-r--r-- | src/db.go | 5 | ||||
-rw-r--r-- | src/db/go.mod | 1 | ||||
-rw-r--r-- | src/db/go.sum | 3 | ||||
-rw-r--r-- | src/db/talk.go | 39 | ||||
-rw-r--r-- | src/http.go | 60 |
10 files changed, 177 insertions, 11 deletions
diff --git a/config.yml b/config.yml index ba730a0..8339da0 100644 --- a/config.yml +++ b/config.yml @@ -1,6 +1,6 @@ # logging verbosity leve (1-7) 1=panic, 7=trace verbose: - level: 6 + level: 7 # Host information (ip to bind to and port to listen on) server: @@ -9,7 +9,7 @@ server: # The database that should be accessed db: - addr: "postgres:5432" + addr: "localhost:5432" user: "postgres" password: "postgres" database: "postgres" diff --git a/go.mod b/go.mod index 5eef6ae..42edc46 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.13 require ( git.darknebu.la/chaosdorf/freitagsfoo/src/db v0.0.0-00010101000000-000000000000 - git.darknebu.la/chaosdorf/freitagsfoo/src/structs v0.0.0-00010101000000-000000000000 + git.darknebu.la/chaosdorf/freitagsfoo/src/structs v0.0.0-20200718235609-fc63adf55849 github.com/go-pg/pg/v9 v9.1.6 github.com/google/uuid v1.1.1 github.com/gorilla/mux v1.7.4 diff --git a/hosted/tmpl/index.html b/hosted/tmpl/index.html index 124271a..c9bf28d 100644 --- a/hosted/tmpl/index.html +++ b/hosted/tmpl/index.html @@ -24,6 +24,8 @@ {{ template "upcomingTalks" . }} + {{ template "pastTalks" . }} + </main> </body> diff --git a/hosted/tmpl/past.html b/hosted/tmpl/past.html new file mode 100644 index 0000000..233bc55 --- /dev/null +++ b/hosted/tmpl/past.html @@ -0,0 +1,31 @@ +{{define "past"}} +<!DOCTYPE html> +<html lang="en"> + + <head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <meta http-equiv="X-UA-Compatible" content="ie=edge"> + {{ template "stylesheets" . }} + <title>Freitagsfoo</title> + </head> + + <body> + <main role="main"> + + <div class="container-fluid bg-success text-white p-5"> + <div class="container"> + <h1 class="display-3"> + <a href="/" class="text-decoration-none text-white">Freitagsfoo!</a> + </h1> + <p><a class="btn btn-primary btn-lg" href="/propose" role="button">Propose a talk »</a></p> + </div> + </div> + + {{ template "pastTalks" . }} + + </main> + </body> + +</html> +{{end}} \ No newline at end of file diff --git a/hosted/tmpl/pastTalks.html b/hosted/tmpl/pastTalks.html new file mode 100644 index 0000000..cf2f0f1 --- /dev/null +++ b/hosted/tmpl/pastTalks.html @@ -0,0 +1,41 @@ +{{ define "pastTalks" }} + <div class="container mt-5"> + <h2>Past talks: {{ .pastCount }}</h2> + <hr> + <div class="row"> + <div class="row row-cols-1 row-cols-md-3"> + {{ range $i, $talk := .pastTalks }} + <div class="col mb-3"> + <div class="card h-100"> + <div class="card-header"> + <h5 class="card-title">{{ $talk.Title }}</h5> + </div> + <div class="card-body"> + <p class="card-text">{{ $talk.Description }}</p> + <a href="/talk/{{ $talk.UUID }}" class="btn btn-primary">Details</a> + </div> + <div class="card-footer text-muted"> + <div class="row"> + <div class="col text-left"> + {{ $talk.FormattedDate }} + </div> + <div class="col text-right"> + {{ $talk.Nickname }} + </div> + </div> + </div> + </div> + </div> + {{ end }} + </div> + + {{ if .All }} + {{ else }} + <div class="text-right mt-3"> + <a href="/past" class="link-secondary">view all</a> + </div> + {{ end }} + </div> + <hr> + </div> +{{ end }} \ No newline at end of file diff --git a/src/db.go b/src/db.go index 713d8e6..ec03fdd 100644 --- a/src/db.go +++ b/src/db.go @@ -61,7 +61,8 @@ func createSomeTalks(pgdb *pg.DB) { "Bombur", } for i := 0; i < 10; i++ { - date := time.Now().Add(time.Duration(i) * 7 * 24 * time.Hour) + + date := time.Now().Add(-3 * 7 * 24 * time.Hour).Add(time.Duration(i) * 7 * 24 * time.Hour) layout := "2006-01-02" formattedDate := date.Format(layout) @@ -72,7 +73,7 @@ func createSomeTalks(pgdb *pg.DB) { Description: fmt.Sprintf("Sunt rerum illo corrupti. Similique qui rem debitis. Accusamus et rerum sint et amet eos nemo. Et enim omnis et. Tempora et corrupti aut ea et vel. \n Dolor est quae sed molestiae nisi esse aliquid atque. Voluptas vero et ducimus voluptatem in eaque. Quo illum et delectus vel sed molestias quidem. Consequuntur unde dolores quis sunt exercitationem eos et provident. Animi eaque temporibus alias. %d", i), Slides: "./uploads/black.png", Nickname: nicknames[rand.Intn(len(nicknames))], - Date: time.Now(), + Date: date, FormattedDate: formattedDate, Upcoming: true, } diff --git a/src/db/go.mod b/src/db/go.mod index 4e70ab6..64c66b8 100644 --- a/src/db/go.mod +++ b/src/db/go.mod @@ -3,6 +3,7 @@ module git.darknebu.la/chaosdorf/freitagsfoo/src/db go 1.13 require ( + git.darknebu.la/chaosdorf/freitagsfoo/src/structs v0.0.0-20200718235609-fc63adf55849 github.com/go-pg/pg/v9 v9.1.6 github.com/google/uuid v1.1.1 github.com/sirupsen/logrus v1.6.0 diff --git a/src/db/go.sum b/src/db/go.sum index eb7a67e..e0d640e 100644 --- a/src/db/go.sum +++ b/src/db/go.sum @@ -11,6 +11,9 @@ cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqCl cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +git.darknebu.la/chaosdorf/freitagsfoo v0.0.0-20200718235609-fc63adf55849 h1:xI4zrY9O8OoR7ais9O+b+ntKoKi4Ln4mb2iUys/e66k= +git.darknebu.la/chaosdorf/freitagsfoo/src/structs v0.0.0-20200718235609-fc63adf55849 h1:dkaulG2hAwns3QvFwFQEq2DVhiRsGPKvDmV5VrI3sfQ= +git.darknebu.la/chaosdorf/freitagsfoo/src/structs v0.0.0-20200718235609-fc63adf55849/go.mod h1:dxRyc1J0ktxxbrlUpd3Uujtx+KgGGidcIpfFBT5ftBc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= diff --git a/src/db/talk.go b/src/db/talk.go index c286aa8..9693d0c 100644 --- a/src/db/talk.go +++ b/src/db/talk.go @@ -2,6 +2,7 @@ package db import ( "fmt" + "time" "git.darknebu.la/chaosdorf/freitagsfoo/src/structs" pg "github.com/go-pg/pg/v9" @@ -21,7 +22,7 @@ func InsertTalk(db *pg.DB, talk *structs.Talk) error { // UpcomingTalksLimited returns the next 3 upcoming talks func UpcomingTalksLimited(db *pg.DB) ([]structs.Talk, error) { var talks []structs.Talk - err := db.Model(&talks).Order("id DESC").Limit(3).Select() + err := db.Model(&talks).Order("date DESC").Where("date > ?", time.Now()).Limit(3).Select() if err != nil { return []structs.Talk{}, fmt.Errorf("could not get the talks from the db: %s", err) } @@ -32,7 +33,29 @@ func UpcomingTalksLimited(db *pg.DB) ([]structs.Talk, error) { // UpcomingTalks returns the next upcoming talks func UpcomingTalks(db *pg.DB) ([]structs.Talk, error) { var talks []structs.Talk - err := db.Model(&talks).Order("id DESC").Select() + err := db.Model(&talks).Order("date DESC").Where("date > ?", time.Now()).Select() + if err != nil { + return []structs.Talk{}, fmt.Errorf("could not get the talks from the db: %s", err) + } + + return talks, nil +} + +// PastTalksLimited returns the past 3 talks +func PastTalksLimited(db *pg.DB) ([]structs.Talk, error) { + var talks []structs.Talk + err := db.Model(&talks).Order("date DESC").Where("date < ?", time.Now()).Limit(3).Select() + if err != nil { + return []structs.Talk{}, fmt.Errorf("could not get the talks from the db: %s", err) + } + + return talks, nil +} + +// PastTalks returns the next upcoming talks +func PastTalks(db *pg.DB) ([]structs.Talk, error) { + var talks []structs.Talk + err := db.Model(&talks).Order("date DESC").Where("date < ?", time.Now()).Select() if err != nil { return []structs.Talk{}, fmt.Errorf("could not get the talks from the db: %s", err) } @@ -42,9 +65,19 @@ func UpcomingTalks(db *pg.DB) ([]structs.Talk, error) { // CountUpcomingTalks counts the amount of talks upcoming func CountUpcomingTalks(db *pg.DB) (int, error) { + var talks []structs.Talk + count, err := db.Model(&talks).Where("date > ?", time.Now()).Where("upcoming = ?", true).SelectAndCount() + if err != nil { + return -1, fmt.Errorf("could not get the talks from the db: %s", err) + } + + return count, nil +} +// CountPastTalks counts the amount of past talks +func CountPastTalks(db *pg.DB) (int, error) { var talks []structs.Talk - count, err := db.Model(&talks).Where("upcoming = ?", true).SelectAndCount() + count, err := db.Model(&talks).Where("date < ?", time.Now()).SelectAndCount() if err != nil { return -1, fmt.Errorf("could not get the talks from the db: %s", err) } diff --git a/src/http.go b/src/http.go index ddab171..823a969 100644 --- a/src/http.go +++ b/src/http.go @@ -19,6 +19,7 @@ func initHTTPServer() { r := mux.NewRouter() r.HandleFunc("/", indexHandler).Methods("GET") r.HandleFunc("/upcoming", upcomingHandler).Methods("GET") + r.HandleFunc("/past", pastHandler).Methods("GET") r.HandleFunc("/propose", proposeHandler).Methods("GET") r.HandleFunc("/talk/{uuid}", talkHandler).Methods("GET") @@ -59,17 +60,32 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { logrus.Warn(err) return } + // fetch the last three talks + lastThreeTalks, err := db.PastTalksLimited(pgdb) + if err != nil { + logrus.Warn(err) + return + } upcomingCount, err := db.CountUpcomingTalks(pgdb) if err != nil { logrus.Warn(err) return } + pastCount, err := db.CountPastTalks(pgdb) + if err != nil { + logrus.Warn(err) + return + } content := map[string]interface{}{ "upcomingTalks": firstThreeTalks, "upcomingCount": upcomingCount, - "All": false, + + "pastTalks": lastThreeTalks, + "pastCount": pastCount, + + "All": false, } // define a template @@ -92,7 +108,7 @@ func upcomingHandler(w http.ResponseWriter, r *http.Request) { defer db.Disconnect(pgdb) // fetch the next talks - firstThreeTalks, err := db.UpcomingTalks(pgdb) + upcomingTalks, err := db.UpcomingTalks(pgdb) if err != nil { logrus.Warn(err) return @@ -105,7 +121,7 @@ func upcomingHandler(w http.ResponseWriter, r *http.Request) { } content := map[string]interface{}{ - "upcomingTalks": firstThreeTalks, + "upcomingTalks": upcomingTalks, "upcomingCount": upcomingCount, "All": true, } @@ -125,6 +141,44 @@ func upcomingHandler(w http.ResponseWriter, r *http.Request) { } } +func pastHandler(w http.ResponseWriter, r *http.Request) { + pgdb := db.Connect() + defer db.Disconnect(pgdb) + + // fetch the next talks + pastTalks, err := db.PastTalks(pgdb) + if err != nil { + logrus.Warn(err) + return + } + + pastCount, err := db.CountPastTalks(pgdb) + if err != nil { + logrus.Warn(err) + return + } + + content := map[string]interface{}{ + "pastTalks": pastTalks, + "pastCount": pastCount, + "All": true, + } + + // define a template + t := template.New("") + t, err = t.ParseGlob("./hosted/tmpl/*.html") + if err != nil { + logrus.Warn(err) + return + } + + // execute the template + err = t.ExecuteTemplate(w, "past", content) + if err != nil { + logrus.Warn(err) + } +} + func proposeHandler(w http.ResponseWriter, r *http.Request) { // find the date of the next friday |