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" "database/sql"
"fmt" "fmt"
"log" "log"
"time"
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
"golang.org/x/crypto/bcrypt" "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 { func (db *DB) UpdateAttribute(table string, id int64, attribute string, val interface{}) error {
query := ` query := fmt.Sprintf(`
UPDATE ? UPDATE %s
SET ? = ? SET %s = ?
WHERE id = ? WHERE id = ?
` `, table, attribute)
if _, err := db.Exec(query, table, attribute, val, id); err != nil { if _, err := db.Exec(query, val, id); err != nil {
return fmt.Errorf("error updating article in DB: %v", err) return fmt.Errorf("error updating article in DB: %v", err)
} }
return nil return nil
@ -192,24 +193,21 @@ func (db *DB) AddTag(tagName string) error {
return nil return nil
} }
func (db *DB) GetTagList() ([]Tag, error) { func (db *DB) GetTagList() ([]*Tag, error) {
query := "SELECT id, name FROM tags" query := "SELECT id, name FROM tags"
rows, err := db.Query(query) rows, err := db.Query(query)
if err != nil { if err != nil {
return nil, fmt.Errorf("error querying tags: %v", err) return nil, fmt.Errorf("error querying tags: %v", err)
} }
var tagList []Tag tagList := make([]*Tag, 0)
for rows.Next() { for rows.Next() {
var tag Tag tag := new(Tag)
if err = rows.Scan(&tag.ID, &tag.Name); err != nil { if err = rows.Scan(&tag.ID, &tag.Name); err != nil {
return nil, fmt.Errorf("error scanning tag row: %v", err) return nil, fmt.Errorf("error scanning tag row: %v", err)
} }
tagList = append(tagList, tag) tagList = append(tagList, tag)
} }
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating through rows: %v", err)
}
return tagList, nil return tagList, nil
} }
@ -243,17 +241,26 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
row := db.QueryRow(query, id) row := db.QueryRow(query, id)
article := new(Article) 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 { &article.Content, &article.Published, &article.AuthorID); err != nil {
return nil, fmt.Errorf("error scanning article row: %v", err) 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 return article, nil
} }
func (db *DB) GetUnpublishedArticles() ([]Article, error) { func (db *DB) GetUnpublishedArticles() ([]*Article, error) {
query := ` query := `
SELECT id, title, created, description, content, published, author_id SELECT id, title, created, description, content, author_id
FROM articles FROM articles
WHERE published = ? WHERE published = ?
` `
@ -262,14 +269,22 @@ func (db *DB) GetUnpublishedArticles() ([]Article, error) {
return nil, fmt.Errorf("error querying articles: %v", err) return nil, fmt.Errorf("error querying articles: %v", err)
} }
var articleList []Article articleList := make([]*Article, 0)
for rows.Next() { for rows.Next() {
var article Article article := new(Article)
if err = rows.Scan(&article.ID, &article.Title, &article.Created, var created []byte
&article.Desc, &article.Content, &article.Published,
&article.AuthorID); err != nil { 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) 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) articleList = append(articleList, article)
} }
@ -300,3 +315,28 @@ func (db *DB) WriteArticleTags(articleID int64, tagIDs []int64) error {
} }
return nil 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) log.Fatalln(err)
} }
defer logFile.Close() defer logFile.Close()
log.SetOutput(logFile) // log.SetOutput(logFile)
db, err := data.OpenDB("cpolis") db, err := data.OpenDB("cpolis")
if err != nil { if err != nil {

View File

@ -132,16 +132,16 @@ func ReviewArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc {
return return
} }
session, err := s.Get(r, "cookie") // session, err := s.Get(r, "cookie")
if err != nil { // if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") // tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." // msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) // 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 = 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) 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{ c.Add(&rss.Item{
Title: article.Title, Title: article.Title,
Author: session.Values["name"].(string), Author: session.Values["name"].(string),
PubDate: article.Created.Format(time.RFC1123Z), PubDate: article.Created.Format(time.RFC1123Z),
Description: article.Desc, Description: article.Desc,
Content: &rss.Content{Value: article.Content}, Content: &rss.Content{Value: article.Content},
// Categories: article.Tags, Categories: tagNames,
}) })
c.Save("tmp/rss.gob") c.Save("tmp/rss.gob")

View File

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

View File

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