diff --git a/cmd/backend/articles.go b/cmd/backend/articles.go index 23a4e0b..00e7861 100644 --- a/cmd/backend/articles.go +++ b/cmd/backend/articles.go @@ -115,6 +115,32 @@ 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 uuidString string + 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, &uuidString); 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 diff --git a/cmd/calls/articles.go b/cmd/calls/articles.go index 4b5d518..071f010 100644 --- a/cmd/calls/articles.go +++ b/cmd/calls/articles.go @@ -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) diff --git a/cmd/main.go b/cmd/main.go index 1ce834f..c5aab1c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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))