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
}
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

View File

@ -30,6 +30,7 @@
<link rel="stylesheet" href="/static/css/base.css">
<link rel="stylesheet" href="/static/css/bar.css">
<link rel="stylesheet" href="/static/css/top.css">
<link rel="stylesheet" href="/static/css/all.css">
<title>{{ .Title }}</title>
</head>
<body>
@ -43,15 +44,37 @@
<div class="bar">
<ul>
<li><a href="/about">about</a></li>
<li><a href="/">home</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>
</div>
</div>
<div class="content">
{{ if .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 class="footer">
{{ 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;
}