From 82ebd0eca72ab030bac6c5308f3f8b7aa8cf4353 Mon Sep 17 00:00:00 2001 From: TheLastBilly Date: Fri, 19 May 2023 15:40:15 -0400 Subject: [PATCH] added ALL view --- comics.go | 134 +++++++++++++++++++++++++---------- templates/comic.html | 27 ++++++- templates/static/css/all.css | 36 ++++++++++ 3 files changed, 158 insertions(+), 39 deletions(-) create mode 100644 templates/static/css/all.css diff --git a/comics.go b/comics.go index 70af36f..162c7b4 100644 --- a/comics.go +++ b/comics.go @@ -31,6 +31,32 @@ type Comic struct { Tags string } +type Context struct { + Comics []struct{ + No int + Title string + Date string + } + Comic *Comic + Current int + Previous int + Combo int + Next int + First int + Last int + Title string +} + +type Err struct { + msg string +} + +func (e * Err) Error() string { + return e.msg +} + +var NoSuchComicErr Err = Err{msg: "no such comic Found"} + func (c *Comic) readRow(db * sql.Rows) error { return db.Scan(&c.DateTime, &c.Title, &c.Image, &c.Description, &c.Tags) @@ -40,13 +66,18 @@ func getComic(id int) (*Comic, error) { c := new(Comic) if id < 0 { - id = 0 + return c, &NoSuchComicErr } comics, err := allComics() - if err != nil || len(comics) <= id { + if err != nil { return c, err } + + if len(comics) <= id || id < 0 { + return c, &NoSuchComicErr + } + *c = comics[id] return c, err } @@ -85,10 +116,18 @@ func allComics() ([]Comic, error) { return readRows(rows) } -func insertComic(c * Comic) error { - _, err := db.Exec("INSERT INTO comic VALUES (?, ?, ?, ?, ?, ?)", - c.DateTime, c.Title, c.Image, c.Description, c.Tags) - return err +func executeMainTemplate(w http.ResponseWriter, context * Context) error { + t, err := template.ParseFiles(filepath.Join(*templatesPath, "comic.html")) + if err != nil { + return err + } + tmp := new(strings.Builder) + err = t.Execute(tmp, context) + if err != nil { + return err + } + _, err = w.Write([]byte(tmp.String())) + return err } func comicView(w http.ResponseWriter, r * http.Request) { @@ -107,24 +146,14 @@ func comicView(w http.ResponseWriter, r * http.Request) { } }() - c := &Comic{ - Title: "Test Comic", - } - context := struct { - Comic Comic - Current int - Previous int - Combo int - Next int - First int - Last int - Title string - }{ - Comic: *c, + c := &Comic{} + context := Context{ + Comics: nil, + Comic: c, Title: "Black Ram Comics", } - i := 0 + i := 1 if len(path) > 0{ i, err = strconv.Atoi(path) if err != nil { @@ -133,7 +162,7 @@ func comicView(w http.ResponseWriter, r * http.Request) { } context.Current = i - c, err = getComic(i) + c, err = getComic(i-1) if err != nil { errlog.Println(err) return404(w,r) @@ -141,17 +170,22 @@ func comicView(w http.ResponseWriter, r * http.Request) { return } - context.Comic = *c - context.Title = fmt.Sprintf("BRC #%d: %s", i, c.Title) + context.Comic = c + context.Title = fmt.Sprintf("No. %d", i) comics, err := allComics() + if err != nil { + return500(w,r) + err = nil + return + } - context.First = 0 - context.Last = len(comics) -1 + context.First = 1 + context.Last = len(comics) context.Previous = i - if i > 0 { - context.Previous = i - 1 + if i > 1 { + context.Previous = i - 1 } context.Next = i @@ -159,18 +193,44 @@ func comicView(w http.ResponseWriter, r * http.Request) { context.Next = i + 1 } - t, err := template.ParseFiles(filepath.Join(*templatesPath, "comic.html")) + err = executeMainTemplate(w, &context) +} + +func allView(w http.ResponseWriter, r * http.Request) { + var err error + + defer func() { + if err != nil { + errlog.Println(err) + return500(w,r) + err = nil + return + } + } () + + context := Context{ + Comics: nil, + Title: "All Issues", + } + + comics, err := allComics() if err != nil { - errlog.Println(err) return } - tmp := new(strings.Builder) - err = t.Execute(tmp, &context) - if err != nil { - return + for i, comic := range comics { + context.Comics = append(context.Comics, struct { + No int + Title string + Date string + }{ + Title: comic.Title, + No: i+1, + Date: comic.DateTime, + }) } - _, err = w.Write([]byte(tmp.String())) + + err = executeMainTemplate(w, &context) } func latestView(w http.ResponseWriter, r * http.Request) { @@ -212,7 +272,7 @@ func randomView(w http.ResponseWriter, r * http.Request) { if len(comics) < 1 { return404(w, r) } else { - r.URL.Path = fmt.Sprintf("/%d", rand.Intn(len(comics))) + r.URL.Path = fmt.Sprintf("/%d", rand.Intn(len(comics))+1) comicView(w, r) } } @@ -332,7 +392,7 @@ func main() { http.HandleFunc("/first", firstView) http.HandleFunc("/random", randomView) http.HandleFunc("/about", firstView) - http.HandleFunc("/all", firstView) + http.HandleFunc("/all", allView) http.HandleFunc("/blog", firstView) // errors diff --git a/templates/comic.html b/templates/comic.html index 1c0b210..34f3818 100644 --- a/templates/comic.html +++ b/templates/comic.html @@ -30,6 +30,7 @@ + {{ .Title }} @@ -43,15 +44,37 @@
+ {{ if .Comic }} {{ template "comic" . }} + {{ end }} + + {{ if .Comics }} +
+ +

Releases

+
+ +
+ {{ end }} +