Index page and post pages ready

This commit is contained in:
Senad Uka
2023-07-30 19:21:16 +02:00
parent 327e9caad7
commit b1561c4875
88 changed files with 273478 additions and 2 deletions

34
database/database.go Normal file
View File

@@ -0,0 +1,34 @@
package database
import (
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"sync"
)
var (
db *sqlx.DB
once sync.Once
)
func GetDB() *sqlx.DB {
once.Do(func() {
db = sqlx.MustConnect("sqlite3", "./cetvorke.db")
postsTable := `
CREATE TABLE IF NOT EXISTS posts(
id INTEGER NOT NULL PRIMARY KEY,
date DATETIME,
year INTEGER,
month INTEGER,
day INTEGER,
post_title TEXT,
title_slug TEXT,
markdown_content TEXT,
gemtext_content TEXT
);
`
db.Exec(postsTable)
})
return db
}

26
database/link.go Normal file
View File

@@ -0,0 +1,26 @@
package database
import (
"fmt"
)
type Link struct {
ID int
TitleSlug string `db:"title_slug""`
Year, Month, Day int
Title string `db:"post_title"`
}
func (l *Link) GemtextLink() string {
return fmt.Sprintf("=> /clanak/%d/%s %d-%d-%d - %s\n", l.ID, l.TitleSlug, l.Year, l.Month, l.Day, l.Title)
}
func GetRecentLinks() ([]Link, error) {
db := GetDB()
links := []Link{}
err := db.Select(&links, "SELECT id, title_slug, year, month, day, post_title FROM posts ORDER BY date DESC LIMIT 10")
if err != nil {
return nil, err
}
return links, nil
}

31
database/post.go Normal file
View File

@@ -0,0 +1,31 @@
package database
import (
"database/sql"
"fmt"
)
type Post struct {
ID int
Title string `db:"post_title"`
Content string
Date string
MarkdownContent string `db:"markdown_content"`
GemtextContent sql.NullString `db:"gemtext_content"`
TitleSlug string `db:"title_slug"`
Year, Month, Day int
}
func (p *Post) GemtextPage() string {
return fmt.Sprintf("# %s\n\n", p.Title) + p.GemtextContent.String
}
func GetPost(id int) (*Post, error) {
db := GetDB()
post := &Post{}
err := db.Get(post, "SELECT id, post_title, date, markdown_content, gemtext_content, title_slug, year, month, day FROM posts WHERE id = ?", id)
if err != nil {
return nil, err
}
return post, nil
}