Compare commits

..

3 Commits
main ... devel

Author SHA1 Message Date
77a64d5179 Bug fix 2025-01-17 19:37:44 +01:00
fb842d203e Fix bug 2025-01-17 19:35:01 +01:00
9c0c7361a0 Serve articles via uuid 2025-01-17 19:07:55 +01:00
3 changed files with 31 additions and 5 deletions

View File

@ -115,6 +115,31 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
return article, nil
}
func (db *DB) GetArticleByUUID(u uuid.UUID) (*Article, error) {
query := `
SELECT id, title, created, banner_link, summary, published, creator_id, issue_id, edited_id, clicks, is_in_issue, auto_generated
FROM articles
WHERE uuid = ?
`
row := db.QueryRow(query, u.String())
article := new(Article)
var created []byte
var err error
if err := row.Scan(&article.ID, &article.Title, &created, &article.BannerLink, &article.Summary, &article.Published, &article.CreatorID, &article.IssueID, &article.EditedID, &article.Clicks, &article.IsInIssue, &article.AutoGenerated); err != nil {
return nil, fmt.Errorf("error scanning article row: %v", err)
}
article.UUID = u
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) GetCertainArticles(attribute string, value bool) ([]*Article, error) {
query := fmt.Sprintf(`
SELECT id, title, created, banner_link, summary, creator_id, issue_id, clicks, published, rejected, is_in_issue, auto_generated, uuid

View File

@ -7,6 +7,7 @@ import (
"os"
"strconv"
"github.com/google/uuid"
b "streifling.com/jason/cpolis/cmd/backend"
)
@ -37,15 +38,15 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
return
}
idString := r.PathValue("id")
id, err := strconv.ParseInt(idString, 10, 64)
uuidString := r.PathValue("uuid")
uuid, err := uuid.Parse(uuidString)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
article, err := db.GetArticle(id)
article, err := db.GetArticleByUUID(uuid)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -56,7 +57,7 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
return
}
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md")
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
contentBytes, err := os.ReadFile(articleAbsName)
if err != nil {
log.Println(err)

View File

@ -52,7 +52,7 @@ func main() {
mux.HandleFunc("GET /article/review-edit/{id}", f.ReviewArticle(config, db, sessions, "allow-edit", "Artikel bearbeiten", "Bearbeiten erlauben"))
mux.HandleFunc("GET /article/review-rejected/{id}", f.ReviewRejectedArticle(config, db, sessions))
mux.HandleFunc("GET /article/review-unpublished/{id}", f.ReviewArticle(config, db, sessions, "publish", "Artikel veröffentlichen", "Veröffentlichen"))
mux.HandleFunc("GET /article/serve/{id}", c.ServeArticle(config, db))
mux.HandleFunc("GET /article/serve/{uuid}", c.ServeArticle(config, db))
mux.HandleFunc("GET /article/serve/{id}/clicks", c.ServeClicks(db))
mux.HandleFunc("GET /article/write", f.WriteArticle(config, db, sessions))
mux.HandleFunc("GET /atom/serve", c.ServeAtomFeed(config))