no longer using IDs, added copyright

This commit is contained in:
TheLastBilly 2023-05-19 03:47:04 -04:00
parent a9c2458f55
commit 2bc75ecc2c
4 changed files with 55 additions and 40 deletions

View File

@ -23,7 +23,6 @@ var mediaPath *string = nil
var templatesPath *string = nil
type Comic struct {
ID int
DateTime string
Title string
Image string
@ -32,7 +31,7 @@ type Comic struct {
}
func (c *Comic) readRow(db * sql.Rows) error {
return db.Scan(&c.ID, &c.DateTime, &c.Title, &c.Image,
return db.Scan(&c.DateTime, &c.Title, &c.Image,
&c.Description, &c.Tags)
}
@ -43,21 +42,16 @@ func getComic(id int) (*Comic, error) {
id = 0
}
rows, err := db.Query("SELECT * FROM comic WHERE id = ?", id)
if err != nil {
comics, err := allComics()
if err != nil || len(comics) <= id {
return c, err
}
defer rows.Close()
if rows.Next() {
err = c.readRow(rows)
}
*c = comics[id]
return c, err
}
const dbSquema string = `
CREATE TABLE IF NOT EXISTS comic (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
datetime DATETIME DEFAULT CURRENT_TIMESTAMP,
title CHAR(50),
image CHAR(200),
@ -80,7 +74,7 @@ func readRows(rows *sql.Rows) ([]Comic, error) {
}
func allComics() ([]Comic, error) {
rows, err := db.Query("SELECT * FROM comic ORDER BY id ASC")
rows, err := db.Query("SELECT * FROM comic ORDER BY datetime ASC")
if err != nil {
return nil, err
}
@ -91,7 +85,7 @@ func allComics() ([]Comic, error) {
func insertComic(c * Comic) error {
_, err := db.Exec("INSERT INTO comic VALUES (?, ?, ?, ?, ?, ?)",
c.ID, c.DateTime, c.Title, c.Image, c.Description, c.Tags)
c.DateTime, c.Title, c.Image, c.Description, c.Tags)
return err
}
@ -116,13 +110,15 @@ func comicView(w http.ResponseWriter, r * http.Request) {
}
context := struct {
Comic Comic
Current int
Previous int
Combo int
Next int
First int
Last int
Title string
}{
Comic: *c,
Previous: 1,
Next: 1,
Title: "Black Ram Comics",
}
@ -133,6 +129,7 @@ func comicView(w http.ResponseWriter, r * http.Request) {
return
}
}
context.Current = i
c, err = getComic(i)
if err != nil {
@ -143,30 +140,21 @@ func comicView(w http.ResponseWriter, r * http.Request) {
}
context.Comic = *c
context.Title = fmt.Sprintf("BRC #%d: %s", c.ID, c.Title)
context.Title = fmt.Sprintf("BRC #%d: %s", i, c.Title)
comics, err := allComics()
context.Previous = c.ID
for i, s := range comics {
if s.ID == c.ID {
if i > 0 {
i -= 1
}
context.Previous = comics[i].ID
break
}
context.First = 0
context.Last = len(comics) -1
context.Previous = i
if i > 0 {
context.Previous = i - 1
}
context.Next = c.ID
for i, s := range comics {
if s.ID == c.ID {
if i < (len(comics) - 1) {
i += 1
}
context.Next = comics[i].ID
break
}
context.Next = i
if i < (len(comics) - 1) {
context.Next = i + 1
}
t, err := template.ParseFiles(filepath.Join(*templatesPath, "comic.html"))
@ -192,7 +180,7 @@ func latestView(w http.ResponseWriter, r * http.Request) {
if len(comics) < 1 {
return404(w, r)
} else {
r.URL.Path = fmt.Sprintf("/%d", comics[len(comics)-1].ID)
r.URL.Path = fmt.Sprintf("/%d", len(comics)-1)
comicView(w, r)
}
}
@ -206,7 +194,7 @@ func firstView(w http.ResponseWriter, r * http.Request) {
if len(comics) < 1 {
return404(w, r)
} else {
r.URL.Path = fmt.Sprintf("/%d", comics[0].ID)
r.URL.Path = fmt.Sprintf("/0")
comicView(w, r)
}
}
@ -214,13 +202,15 @@ func firstView(w http.ResponseWriter, r * http.Request) {
func randomView(w http.ResponseWriter, r * http.Request) {
comics, err := allComics()
if err != nil {
errlog.Println(err)
return500(w, r)
return
}
if len(comics) < 1 {
return404(w, r)
} else {
r.URL.Path = fmt.Sprintf("/%d", comics[rand.Intn(len(comics))].ID)
r.URL.Path = fmt.Sprintf("/%d", rand.Intn(len(comics)))
comicView(w, r)
}
}
@ -339,6 +329,9 @@ func main() {
http.HandleFunc("/latest", latestView)
http.HandleFunc("/first", firstView)
http.HandleFunc("/random", randomView)
http.HandleFunc("/about", firstView)
http.HandleFunc("/all", firstView)
http.HandleFunc("/blog", firstView)
// errors
http.HandleFunc("/404", return404)

View File

@ -1,20 +1,21 @@
{{ define "nextprev" }}
<div class="nextprev">
<ul>
<li><a href="/first">&lt&lt</a></li>
<li><a href="/{{ .First }}">&lt&lt</a></li>
<li><a href="/{{ .Previous }}">prev</a></li>
<li><a href="/random">random</a></li>
<li><a href="/{{ .Next }}" style="margin-left: auto;">next</a></li>
<li><a href="/latest">&gt&gt</a></li>
<li><a href="/{{ .Last }}">&gt&gt</a></li>
</ul>
</div>
{{ end }}
{{ define "comic" }}
<div class="comic">
<h2><a href="/{{ .Comic.ID }}">{{ .Comic.Title }}</a></h2>
<h2><a href="/{{ .Current }}">{{ .Comic.Title }}</a></h2>
{{ template "nextprev" .}}
<img src="/media/{{ .Comic.Image }}"></img>
<p>Black Ram Comics Issue No. {{ .Current }}, {{ .Comic.Title }}</p>
{{ template "nextprev" .}}
</div>
{{ end }}
@ -24,6 +25,7 @@
<head>
<meta charset="utf-8">
<link rel="icon" type="/image/png" href="/static/img/favicon.png"/>
<link rel="stylesheet" href="/static/css/footer.css">
<link rel="stylesheet" href="/static/css/comic.css">
<link rel="stylesheet" href="/static/css/base.css">
<link rel="stylesheet" href="/static/css/bar.css">
@ -51,6 +53,19 @@
<div class="content">
{{ template "comic" . }}
</div>
<div class="footer">
{{ if .Combo }}
<div class="combo">
<p>current combo: {{ .Combo }}</p>
</div>
{{ end }}
<footer>
<ul>
<li>© 2023 drevil. All rights reserved.</li>
<li>The source code for this website can be found at <a href="https://tildegit.org/drevil/comics">tildegit.org/drevil/comics</a></li>
</ul>
</footer>
</div>
</body>
</html>

View File

@ -3,7 +3,7 @@
html, body {
margin:auto;
width: 60%;
width: 47em;
background: #282828;
font-family: "JetBrainsMono", monospace;
scroll-behavior: smooth;

View File

@ -0,0 +1,7 @@
.footer a {
color: #2484c1;
}
.footer ul {
list-style-type: none;
}