Reachieve basic functionality

This commit is contained in:
Jason Streifling 2024-03-07 20:11:28 +01:00
parent 4d65be195b
commit fa5f189cda
5 changed files with 86 additions and 35 deletions

View File

@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"log"
"time"
"github.com/go-sql-driver/mysql"
"golang.org/x/crypto/bcrypt"
@ -36,12 +37,12 @@ func OpenDB(dbName string) (*DB, error) {
}
func (db *DB) UpdateAttribute(table string, id int64, attribute string, val interface{}) error {
query := `
UPDATE ?
SET ? = ?
query := fmt.Sprintf(`
UPDATE %s
SET %s = ?
WHERE id = ?
`
if _, err := db.Exec(query, table, attribute, val, id); err != nil {
`, table, attribute)
if _, err := db.Exec(query, val, id); err != nil {
return fmt.Errorf("error updating article in DB: %v", err)
}
return nil
@ -192,24 +193,21 @@ func (db *DB) AddTag(tagName string) error {
return nil
}
func (db *DB) GetTagList() ([]Tag, error) {
func (db *DB) GetTagList() ([]*Tag, error) {
query := "SELECT id, name FROM tags"
rows, err := db.Query(query)
if err != nil {
return nil, fmt.Errorf("error querying tags: %v", err)
}
var tagList []Tag
tagList := make([]*Tag, 0)
for rows.Next() {
var tag Tag
tag := new(Tag)
if err = rows.Scan(&tag.ID, &tag.Name); err != nil {
return nil, fmt.Errorf("error scanning tag row: %v", err)
}
tagList = append(tagList, tag)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating through rows: %v", err)
}
return tagList, nil
}
@ -243,17 +241,26 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
row := db.QueryRow(query, id)
article := new(Article)
if err := row.Scan(&article.Title, &article.Created, &article.Desc,
var created []byte
var err error
if err := row.Scan(&article.Title, &created, &article.Desc,
&article.Content, &article.Published, &article.AuthorID); err != nil {
return nil, fmt.Errorf("error scanning article row: %v", err)
}
article.ID = id
article.Created, err = time.Parse("2006-01-02 15:04:05", string(created))
if err != nil {
return nil, fmt.Errorf("error parsing created: %v", err)
}
return article, nil
}
func (db *DB) GetUnpublishedArticles() ([]Article, error) {
func (db *DB) GetUnpublishedArticles() ([]*Article, error) {
query := `
SELECT id, title, created, description, content, published, author_id
SELECT id, title, created, description, content, author_id
FROM articles
WHERE published = ?
`
@ -262,14 +269,22 @@ func (db *DB) GetUnpublishedArticles() ([]Article, error) {
return nil, fmt.Errorf("error querying articles: %v", err)
}
var articleList []Article
articleList := make([]*Article, 0)
for rows.Next() {
var article Article
if err = rows.Scan(&article.ID, &article.Title, &article.Created,
&article.Desc, &article.Content, &article.Published,
&article.AuthorID); err != nil {
article := new(Article)
var created []byte
if err = rows.Scan(&article.ID, &article.Title, &created, &article.Desc,
&article.Content, &article.AuthorID); err != nil {
return nil, fmt.Errorf("error scanning article row: %v", err)
}
article.Published = false
article.Created, err = time.Parse("2006-01-02 15:04:05", string(created))
if err != nil {
return nil, fmt.Errorf("error parsing created: %v", err)
}
articleList = append(articleList, article)
}
@ -300,3 +315,28 @@ func (db *DB) WriteArticleTags(articleID int64, tagIDs []int64) error {
}
return nil
}
func (db *DB) GetArticleTags(articleID int64) ([]*Tag, error) {
query := `
SELECT t.id, t.name
FROM articles a
INNER JOIN articles_tags at ON a.id = at.article_id
INNER JOIN tags t ON at.tag_id = t.id
WHERE a.id = ?
`
rows, err := db.Query(query, articleID)
if err != nil {
return nil, fmt.Errorf("error querying articles_tags: %v", err)
}
tags := make([]*Tag, 0)
for rows.Next() {
tag := new(Tag)
if err = rows.Scan(&tag.ID, &tag.Name); err != nil {
return nil, fmt.Errorf("error scanning rows: %v", err)
}
tags = append(tags, tag)
}
return tags, nil
}

View File

@ -21,7 +21,7 @@ func main() {
log.Fatalln(err)
}
defer logFile.Close()
log.SetOutput(logFile)
// log.SetOutput(logFile)
db, err := data.OpenDB("cpolis")
if err != nil {

View File

@ -132,16 +132,16 @@ func ReviewArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc {
return
}
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
// session, err := s.Get(r, "cookie")
// if err != nil {
// tmpl, err := template.ParseFiles("web/templates/login.html")
// msg := "Session nicht mehr gültig. Bitte erneut anmelden."
// template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
// }
tmpl, err := template.ParseFiles("web/templates/hub.html")
tmpl, err := template.ParseFiles("web/templates/to-be-published.html")
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
tmpl.ExecuteTemplate(w, "page-content", article)
}
}
@ -173,13 +173,24 @@ func PublishArticle(db *data.DB, c *data.Channel, s *data.CookieStore) http.Hand
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
tags, err := db.GetArticleTags(article.ID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tagNames := make([]string, 0)
for _, tag := range tags {
tagNames = append(tagNames, tag.Name)
}
c.Add(&rss.Item{
Title: article.Title,
Author: session.Values["name"].(string),
PubDate: article.Created.Format(time.RFC1123Z),
Description: article.Desc,
Content: &rss.Content{Value: article.Content},
// Categories: article.Tags,
Categories: tagNames,
})
c.Save("tmp/rss.gob")

View File

@ -1,9 +1,9 @@
{{define "page-content"}}
<form>
<input name="editor-title" type="text" value="{{.Title}}" />
<textarea name="editor-desc">{{.Desc}}</textarea>
<textarea name="editor-text">{{.Content}}</textarea>
<input name="uuid" type="hidden" value="{{.UUID}}" />
<h2>{{.Title}}</h2>
<p>{{.Desc}}</p>
<span>{{.Content}}</span>
<input name="id" type="hidden" value="{{.ID}}" />
<input type="submit" value="Veröffentlichen" hx-post="/publish-article/" hx-target="#page-content" />
<input type="submit" value="Ablehnen" hx-post="/reject-article/" hx-target="#page-content" />
</form>

View File

@ -1,8 +1,8 @@
{{define "page-content"}}
<form>
{{range .}}
<input required id="{{.UUID}}" name="uuid" type="radio" value="{{.UUID}}" />
<label for="{{.UUID}}">{{.Title}}</label>
<input required id="{{.ID}}" name="id" type="radio" value="{{.ID}}" />
<label for="{{.ID}}">{{.Title}}</label>
{{end}}
<input type="submit" value="Auswählen" hx-post="/review-article/" hx-target="#page-content" />
</form>