implemented numeric, prev, next, random, last and first

This commit is contained in:
TheLastBilly 2023-05-19 02:28:09 -04:00
parent 700983cb12
commit a9c2458f55
4 changed files with 123 additions and 69 deletions

182
comics.go
View File

@ -6,6 +6,7 @@ import (
"fmt"
"strconv"
"strings"
"math/rand"
//"net/url"
"net/http"
"database/sql"
@ -35,43 +36,9 @@ func (c *Comic) readRow(db * sql.Rows) error {
&c.Description, &c.Tags)
}
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),
description TEXT,
tags TEXT
);`
func getComic(id int) (*Comic, error) {
c := new(Comic)
func readRows(db *sql.Rows) ([]Comic, error) {
comics := []Comic{}
for db.Next() {
c := Comic{}
err := c.readRow(db)
if err != nil {
return comics, nil
}
comics = append(comics, c)
}
return comics, nil
}
func allComics() ([]Comic, error) {
rows, err := db.Query("SELECT * FROM comic")
if err != nil {
return nil, err
}
defer rows.Close()
return readRows(rows)
}
func getComic(id int) (Comic, error) {
c := Comic{}
if id < 0 {
id = 0
}
@ -82,17 +49,53 @@ func getComic(id int) (Comic, error) {
}
defer rows.Close()
err = c.readRow(rows)
if rows.Next() {
err = c.readRow(rows)
}
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),
description TEXT,
tags TEXT
);`
func readRows(rows *sql.Rows) ([]Comic, error) {
comics := []Comic{}
for rows.Next() {
c := Comic{}
err := c.readRow(rows)
if err != nil {
return comics, nil
}
comics = append(comics, c)
}
return comics, nil
}
func allComics() ([]Comic, error) {
rows, err := db.Query("SELECT * FROM comic ORDER BY id ASC")
if err != nil {
return nil, err
}
defer rows.Close()
return readRows(rows)
}
func insertComic(c * Comic) error {
_, err := db.Exec("INSERT INTO comic VALUES (?, ?, ?, ?, ?, ?)",
c.ID, c.DateTime, c.Title, c.Image, c.Description, c.Tags)
return err
}
func indexView(w http.ResponseWriter, r * http.Request) {
func comicView(w http.ResponseWriter, r * http.Request) {
var err error
path := strings.TrimPrefix(r.URL.Path, "/")
@ -103,53 +106,67 @@ func indexView(w http.ResponseWriter, r * http.Request) {
defer func() {
if err != nil {
errlog.Println(err)
return500(w,r)
return404(w,r)
return
}
}()
c := Comic{
c := &Comic{
Title: "Test Comic",
}
context := struct {
Comic *Comic
Comic Comic
Previous int
Next int
Title string
}{
Comic: &c,
Previous: 0,
Next: 0,
Comic: *c,
Previous: 1,
Next: 1,
Title: "Black Ram Comics",
}
i := 0
i := 1
if len(path) > 0{
i, err = strconv.Atoi(path)
if err != nil {
return
}
}
c, err = getComic(i)
if err != nil {
errlog.Println(err)
return404(w,r)
err = nil
return
}
c, err = getComic(i)
if err != nil {
return404(w,r)
err = nil
return
context.Comic = *c
context.Title = fmt.Sprintf("BRC #%d: %s", c.ID, 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.Comic = &c
context.Title = "BRC: " + c.Title
}
_, err = getComic(i-1)
if err == nil {
context.Previous = i - 1
}
_, err = getComic(i+1)
if err == nil {
context.Next = 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
}
}
t, err := template.ParseFiles(filepath.Join(*templatesPath, "comic.html"))
@ -167,11 +184,45 @@ func indexView(w http.ResponseWriter, r * http.Request) {
}
func latestView(w http.ResponseWriter, r * http.Request) {
returnPlainText(w, "latest")
comics, err := allComics()
if err != nil {
return500(w, r)
}
if len(comics) < 1 {
return404(w, r)
} else {
r.URL.Path = fmt.Sprintf("/%d", comics[len(comics)-1].ID)
comicView(w, r)
}
}
func firstView(w http.ResponseWriter, r * http.Request) {
comics, err := allComics()
if err != nil {
return500(w, r)
}
if len(comics) < 1 {
return404(w, r)
} else {
r.URL.Path = fmt.Sprintf("/%d", comics[0].ID)
comicView(w, r)
}
}
func randomView(w http.ResponseWriter, r * http.Request) {
returnPlainText(w, "random")
comics, err := allComics()
if err != nil {
return500(w, r)
}
if len(comics) < 1 {
return404(w, r)
} else {
r.URL.Path = fmt.Sprintf("/%d", comics[rand.Intn(len(comics))].ID)
comicView(w, r)
}
}
// Taken from: https://gist.github.com/hoitomt/c0663af8c9443f2a8294
@ -284,8 +335,9 @@ func main() {
}
// views
http.HandleFunc("/", indexView)
http.HandleFunc("/", comicView)
http.HandleFunc("/latest", latestView)
http.HandleFunc("/first", firstView)
http.HandleFunc("/random", randomView)
// errors

View File

@ -14,6 +14,7 @@
<div class="comic">
<h2><a href="/{{ .Comic.ID }}">{{ .Comic.Title }}</a></h2>
{{ template "nextprev" .}}
<img src="/media/{{ .Comic.Image }}"></img>
{{ template "nextprev" .}}
</div>
{{ end }}
@ -34,7 +35,7 @@
<div class="title">
<a href="/">
<h1>{{ .Title }}</h1>
<h1>Black Ram Comics</h1>
</a>
</div>

View File

@ -3,7 +3,7 @@
html, body {
margin:auto;
width: 65%;
width: 60%;
background: #282828;
font-family: "JetBrainsMono", monospace;
scroll-behavior: smooth;
@ -110,7 +110,8 @@ img {
border-spacing: 1em;
margin-top: 1em;
margin-bottom: 1em;
width: 60%;
width: 100%;
box-sizing: border-box;
}
h1 a, h1 a:hover {
/* color: #333;*/

View File

@ -16,7 +16,7 @@
text-align: center;
}
.top .title h1 {
margin: 0;
margin: 0em;
color: #282828;
background-color: #ebdbb2;
font-weight: bolder;