added ALL view

This commit is contained in:
TheLastBilly 2023-05-19 15:40:15 -04:00
parent 81bc211907
commit 82ebd0eca7
3 changed files with 158 additions and 39 deletions

134
comics.go
View File

@ -31,6 +31,32 @@ type Comic struct {
Tags string 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 { func (c *Comic) readRow(db * sql.Rows) error {
return db.Scan(&c.DateTime, &c.Title, &c.Image, return db.Scan(&c.DateTime, &c.Title, &c.Image,
&c.Description, &c.Tags) &c.Description, &c.Tags)
@ -40,13 +66,18 @@ func getComic(id int) (*Comic, error) {
c := new(Comic) c := new(Comic)
if id < 0 { if id < 0 {
id = 0 return c, &NoSuchComicErr
} }
comics, err := allComics() comics, err := allComics()
if err != nil || len(comics) <= id { if err != nil {
return c, err return c, err
} }
if len(comics) <= id || id < 0 {
return c, &NoSuchComicErr
}
*c = comics[id] *c = comics[id]
return c, err return c, err
} }
@ -85,10 +116,18 @@ func allComics() ([]Comic, error) {
return readRows(rows) return readRows(rows)
} }
func insertComic(c * Comic) error { func executeMainTemplate(w http.ResponseWriter, context * Context) error {
_, err := db.Exec("INSERT INTO comic VALUES (?, ?, ?, ?, ?, ?)", t, err := template.ParseFiles(filepath.Join(*templatesPath, "comic.html"))
c.DateTime, c.Title, c.Image, c.Description, c.Tags) if err != nil {
return err 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) { func comicView(w http.ResponseWriter, r * http.Request) {
@ -107,24 +146,14 @@ func comicView(w http.ResponseWriter, r * http.Request) {
} }
}() }()
c := &Comic{ c := &Comic{}
Title: "Test Comic", context := Context{
} Comics: nil,
context := struct { Comic: c,
Comic Comic
Current int
Previous int
Combo int
Next int
First int
Last int
Title string
}{
Comic: *c,
Title: "Black Ram Comics", Title: "Black Ram Comics",
} }
i := 0 i := 1
if len(path) > 0{ if len(path) > 0{
i, err = strconv.Atoi(path) i, err = strconv.Atoi(path)
if err != nil { if err != nil {
@ -133,7 +162,7 @@ func comicView(w http.ResponseWriter, r * http.Request) {
} }
context.Current = i context.Current = i
c, err = getComic(i) c, err = getComic(i-1)
if err != nil { if err != nil {
errlog.Println(err) errlog.Println(err)
return404(w,r) return404(w,r)
@ -141,17 +170,22 @@ func comicView(w http.ResponseWriter, r * http.Request) {
return return
} }
context.Comic = *c context.Comic = c
context.Title = fmt.Sprintf("BRC #%d: %s", i, c.Title) context.Title = fmt.Sprintf("No. %d", i)
comics, err := allComics() comics, err := allComics()
if err != nil {
return500(w,r)
err = nil
return
}
context.First = 0 context.First = 1
context.Last = len(comics) -1 context.Last = len(comics)
context.Previous = i context.Previous = i
if i > 0 { if i > 1 {
context.Previous = i - 1 context.Previous = i - 1
} }
context.Next = i context.Next = i
@ -159,18 +193,44 @@ func comicView(w http.ResponseWriter, r * http.Request) {
context.Next = i + 1 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 { if err != nil {
errlog.Println(err)
return return
} }
tmp := new(strings.Builder) for i, comic := range comics {
err = t.Execute(tmp, &context) context.Comics = append(context.Comics, struct {
if err != nil { No int
return 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) { func latestView(w http.ResponseWriter, r * http.Request) {
@ -212,7 +272,7 @@ func randomView(w http.ResponseWriter, r * http.Request) {
if len(comics) < 1 { if len(comics) < 1 {
return404(w, r) return404(w, r)
} else { } 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) comicView(w, r)
} }
} }
@ -332,7 +392,7 @@ func main() {
http.HandleFunc("/first", firstView) http.HandleFunc("/first", firstView)
http.HandleFunc("/random", randomView) http.HandleFunc("/random", randomView)
http.HandleFunc("/about", firstView) http.HandleFunc("/about", firstView)
http.HandleFunc("/all", firstView) http.HandleFunc("/all", allView)
http.HandleFunc("/blog", firstView) http.HandleFunc("/blog", firstView)
// errors // errors

View File

@ -30,6 +30,7 @@
<link rel="stylesheet" href="/static/css/base.css"> <link rel="stylesheet" href="/static/css/base.css">
<link rel="stylesheet" href="/static/css/bar.css"> <link rel="stylesheet" href="/static/css/bar.css">
<link rel="stylesheet" href="/static/css/top.css"> <link rel="stylesheet" href="/static/css/top.css">
<link rel="stylesheet" href="/static/css/all.css">
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
</head> </head>
<body> <body>
@ -43,15 +44,37 @@
<div class="bar"> <div class="bar">
<ul> <ul>
<li><a href="/about">about</a></li> <li><a href="/">home</a></li>
<li><a href="/all">all</a></li> <li><a href="/all">all</a></li>
<li><a href="/blog">blog</a></li> <li><a href="https://tilde.zone/@chickfilla">blog</a></li>
</ul> </ul>
</div> </div>
</div> </div>
<div class="content"> <div class="content">
{{ if .Comic }}
{{ template "comic" . }} {{ template "comic" . }}
{{ end }}
{{ if .Comics }}
<div class="comic-list">
<a href="/all">
<h1>Releases</h1>
</a>
<ul>
{{ range $comic := .Comics }}
<li>
<a href="/{{ $comic.No }}">
<div class="comic-list-item">
<p>No. {{ $comic.No }}, {{ $comic.Title }}</p>
</div>
</a>
</li>
{{ end }}
</ul>
</div>
{{ end }}
</div> </div>
<div class="footer"> <div class="footer">
{{ if .Combo }} {{ if .Combo }}

View File

@ -0,0 +1,36 @@
.comic-list {
text-align: center;
}
.comic-list a {
text-align: center;
color: #ebdbb2;
}
.comic-list h1 {
text-transform: uppercase;
font-weight: bold;
}
.comic-list ul {
margin-top: 2em;
list-style-type: none;
box-sizing: border-box;
}
.comic-list-item {
color: #ebdbb2;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.5em;
}
.comic-list-item:hover {
text-decoration: underline;
text-decoration-thickness: 0.1em;
}
.comic-list-item * {
margin: 0.5em;
}