From 10d8fceb77ba46e6595e6762f698f858f4edc6d1 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 25 Aug 2024 11:24:03 +0200 Subject: [PATCH 1/9] Fixed formatting errors in create_db.sql --- create_db.sql | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/create_db.sql b/create_db.sql index 1a0f873..1fa3d1a 100644 --- a/create_db.sql +++ b/create_db.sql @@ -11,13 +11,13 @@ CREATE TABLE users ( first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, role INT NOT NULL, - PRIMARY KEY(id) + PRIMARY KEY (id) ); CREATE TABLE issues ( id INT AUTO_INCREMENT, published BOOL NOT NULL, - PRIMARY KEY(id) + PRIMARY KEY (id) ); CREATE TABLE articles ( @@ -30,21 +30,21 @@ CREATE TABLE articles ( rejected BOOL NOT NULL, author_id INT NOT NULL, issue_id INT NOT NULL, - PRIMARY KEY(id), - FOREIGN KEY(author_id) REFERENCES users(id), - FOREIGN KEY(issue_id) REFERENCES issues(id) + PRIMARY KEY (id), + FOREIGN KEY (author_id) REFERENCES users (id), + FOREIGN KEY (issue_id) REFERENCES issues (id) ); CREATE TABLE tags ( id INT AUTO_INCREMENT, name VARCHAR(50) NOT NULL UNIQUE, - PRIMARY KEY(id) + PRIMARY KEY (id) ); CREATE TABLE articles_tags ( article_id INT, tag_id INT, - PRIMARY KEY(article_id, tag_id), - FOREIGN KEY(article_id) REFERENCES articles(id), - FOREIGN KEY(tag_id) REFERENCES tags(id) + PRIMARY KEY (article_id, tag_id), + FOREIGN KEY (article_id) REFERENCES articles (id), + FOREIGN KEY (tag_id) REFERENCES tags (id) ); From be467521d9b468389a6b938307196648549302b9 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 30 Aug 2024 15:17:14 +0200 Subject: [PATCH 2/9] Only provide link in item instead of the entire article via content --- cmd/backend/rss.go | 23 +++++++++-------------- cmd/calls/articles.go | 33 +++++++++++++++++++++++++++++++++ cmd/frontend/articles.go | 4 ++-- cmd/main.go | 1 + 4 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 cmd/calls/articles.go diff --git a/cmd/backend/rss.go b/cmd/backend/rss.go index c68395e..1807da0 100644 --- a/cmd/backend/rss.go +++ b/cmd/backend/rss.go @@ -50,11 +50,11 @@ func GetChannel(db *DB, title, link, description string) (*rss.Channel, error) { return channel, nil } -func GenerateRSS(db *DB, title, link, desc string) (*string, error) { +func GenerateRSS(c *Config, db *DB) (*string, error) { channel := &rss.Channel{ - Title: title, - Link: link, - Description: desc, + Title: c.Title, + Link: c.Link, + Description: c.Description, Items: make([]*rss.Item, 0), } @@ -89,18 +89,13 @@ func GenerateRSS(db *DB, title, link, desc string) (*string, error) { return nil, fmt.Errorf("error converting description to plain text for RSS feed: %v", err) } - articleContent, err := ConvertToHTML(article.Content) - if err != nil { - return nil, fmt.Errorf("error converting content to HTML for RSS feed: %v", err) - } - channel.Items = append(channel.Items, &rss.Item{ - Title: articleTitle, - Author: user.FirstName + " " + user.LastName, - PubDate: article.Created.Format(time.RFC1123Z), - Description: articleDescription, - Content: &rss.Content{Value: articleContent}, + Author: fmt.Sprint(user.FirstName, user.LastName), Categories: tagNames, + Description: articleDescription, + Link: fmt.Sprintf("http://%s/article/serve/%d", c.Domain, article.ID), + PubDate: article.Created.Format(time.RFC1123Z), + Title: articleTitle, }) } diff --git a/cmd/calls/articles.go b/cmd/calls/articles.go new file mode 100644 index 0000000..891f7a0 --- /dev/null +++ b/cmd/calls/articles.go @@ -0,0 +1,33 @@ +package calls + +import ( + "fmt" + "log" + "net/http" + "strconv" + + b "streifling.com/jason/cpolis/cmd/backend" +) + +func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if tokenIsVerified(w, r, c) { + idString := r.PathValue("id") + id, err := strconv.ParseInt(idString, 10, 64) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + article, err := db.GetArticle(id) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + fmt.Fprint(w, article.Content) + } + } +} diff --git a/cmd/frontend/articles.go b/cmd/frontend/articles.go index 5fc9ee6..dac02e8 100644 --- a/cmd/frontend/articles.go +++ b/cmd/frontend/articles.go @@ -363,7 +363,7 @@ func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return } - feed, err := b.GenerateRSS(db, c.Title, c.Link, c.Description) + feed, err := b.GenerateRSS(c, db) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -582,7 +582,7 @@ func DeleteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return } - feed, err := b.GenerateRSS(db, c.Title, c.Link, c.Description) + feed, err := b.GenerateRSS(c, db) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/cmd/main.go b/cmd/main.go index 7bb9a61..718ebec 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -49,6 +49,7 @@ func main() { http.FileServer(http.Dir(config.WebDir+"/static/")))) mux.HandleFunc("/", f.HomePage(config, db, store)) + mux.HandleFunc("GET /article/serve/{id}", c.ServeArticle(config, db)) mux.HandleFunc("GET /create-tag", f.CreateTag(config, store)) mux.HandleFunc("GET /create-user", f.CreateUser(config, store)) mux.HandleFunc("GET /delete-article/{id}", f.DeleteArticle(config, db, store)) From cddd88d2f633bad418a9f989f8870e489e987d04 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 30 Aug 2024 15:42:53 +0200 Subject: [PATCH 3/9] restructure routes --- cmd/main.go | 60 ++++++++++++------------- web/templates/add-user.html | 2 +- web/templates/current-articles.html | 2 +- web/templates/edit-self.html | 2 +- web/templates/edit-user.html | 2 +- web/templates/editor.html | 4 +- web/templates/first-user.html | 2 +- web/templates/hub.html | 20 ++++----- web/templates/published-articles.html | 2 +- web/templates/rework-article.html | 4 +- web/templates/to-be-deleted.html | 2 +- web/templates/to-be-published.html | 4 +- web/templates/unpublished-articles.html | 2 +- 13 files changed, 54 insertions(+), 54 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 718ebec..8a131f4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -49,42 +49,42 @@ func main() { http.FileServer(http.Dir(config.WebDir+"/static/")))) mux.HandleFunc("/", f.HomePage(config, db, store)) + mux.HandleFunc("GET /article/all-published", f.ShowPublishedArticles(config, db, store)) + mux.HandleFunc("GET /article/all-rejected", f.ShowRejectedArticles(config, db, store)) + mux.HandleFunc("GET /article/all-unpublished", f.ShowUnpublishedArticles(config, db, store)) + mux.HandleFunc("GET /article/delete/{id}", f.DeleteArticle(config, db, store)) + mux.HandleFunc("GET /article/publish/{id}", f.PublishArticle(config, db, store)) + mux.HandleFunc("GET /article/reject/{id}", f.RejectArticle(config, db, store)) + mux.HandleFunc("GET /article/review-deletion/{id}", f.ReviewArticleForDeletion(config, db, store)) + mux.HandleFunc("GET /article/review-rejected/{id}", f.ReviewRejectedArticle(config, db, store)) + mux.HandleFunc("GET /article/review-unpublished/{id}", f.ReviewUnpublishedArticle(config, db, store)) mux.HandleFunc("GET /article/serve/{id}", c.ServeArticle(config, db)) - mux.HandleFunc("GET /create-tag", f.CreateTag(config, store)) - mux.HandleFunc("GET /create-user", f.CreateUser(config, store)) - mux.HandleFunc("GET /delete-article/{id}", f.DeleteArticle(config, db, store)) - mux.HandleFunc("GET /delete-user/{id}", f.DeleteUser(config, db, store)) - mux.HandleFunc("GET /edit-self", f.EditSelf(config, db, store)) - mux.HandleFunc("GET /edit-user/{id}", f.EditUser(config, db, store)) + mux.HandleFunc("GET /article/write", f.WriteArticle(config, db, store)) mux.HandleFunc("GET /hub", f.ShowHub(config, db, store)) + mux.HandleFunc("GET /issue/publish", f.PublishLatestIssue(config, db, store)) + mux.HandleFunc("GET /issue/this", f.ShowCurrentArticles(config, db, store)) mux.HandleFunc("GET /logout", f.Logout(config, store)) mux.HandleFunc("GET /pdf/get-list", c.ServePDFList(config)) - mux.HandleFunc("GET /pdf/{id}", c.ServePDF(config)) - mux.HandleFunc("GET /pics/{pic}", f.ServeImage(config, store)) - mux.HandleFunc("GET /publish-article/{id}", f.PublishArticle(config, db, store)) - mux.HandleFunc("GET /publish-issue", f.PublishLatestIssue(config, db, store)) - mux.HandleFunc("GET /published-articles", f.ShowPublishedArticles(config, db, store)) - mux.HandleFunc("GET /reject-article/{id}", f.RejectArticle(config, db, store)) - mux.HandleFunc("GET /rejected-articles", f.ShowRejectedArticles(config, db, store)) - mux.HandleFunc("GET /review-article-for-deletion/{id}", f.ReviewArticleForDeletion(config, db, store)) - mux.HandleFunc("GET /review-rejected-article/{id}", f.ReviewRejectedArticle(config, db, store)) - mux.HandleFunc("GET /review-unpublished-article/{id}", f.ReviewUnpublishedArticle(config, db, store)) - mux.HandleFunc("GET /rss", c.ServeRSS(config)) - mux.HandleFunc("GET /show-all-users-edit", f.ShowAllUsers(config, db, store, "edit-user")) - mux.HandleFunc("GET /show-all-users-delete", f.ShowAllUsers(config, db, store, "delete-user")) - mux.HandleFunc("GET /this-issue", f.ShowCurrentArticles(config, db, store)) - mux.HandleFunc("GET /unpublished-articles", f.ShowUnpublishedArticles(config, db, store)) - mux.HandleFunc("GET /write-article", f.WriteArticle(config, db, store)) + mux.HandleFunc("GET /pdf/serve/{id}", c.ServePDF(config)) + mux.HandleFunc("GET /pic/serve/{pic}", f.ServeImage(config, store)) + mux.HandleFunc("GET /rss/serve", c.ServeRSS(config)) + mux.HandleFunc("GET /tag/create", f.CreateTag(config, store)) + mux.HandleFunc("GET /user/create", f.CreateUser(config, store)) + mux.HandleFunc("GET /user/delete/{id}", f.DeleteUser(config, db, store)) + mux.HandleFunc("GET /user/edit/{id}", f.EditUser(config, db, store)) + mux.HandleFunc("GET /user/edit/self", f.EditSelf(config, db, store)) + mux.HandleFunc("GET /user/show-all/delete", f.ShowAllUsers(config, db, store, "delete-user")) + mux.HandleFunc("GET /user/show-all/edit", f.ShowAllUsers(config, db, store, "edit-user")) - mux.HandleFunc("POST /add-first-user", f.AddFirstUser(config, db, store)) - mux.HandleFunc("POST /add-tag", f.AddTag(config, db, store)) - mux.HandleFunc("POST /add-user", f.AddUser(config, db, store)) + mux.HandleFunc("POST /article/resubmit/{id}", f.ResubmitArticle(config, db, store)) + mux.HandleFunc("POST /article/submit", f.SubmitArticle(config, db, store)) mux.HandleFunc("POST /login", f.Login(config, db, store)) - mux.HandleFunc("POST /resubmit-article/{id}", f.ResubmitArticle(config, db, store)) - mux.HandleFunc("POST /submit-article", f.SubmitArticle(config, db, store)) - mux.HandleFunc("POST /update-self", f.UpdateSelf(config, db, store)) - mux.HandleFunc("POST /update-user/{id}", f.UpdateUser(config, db, store)) - mux.HandleFunc("POST /upload-image", f.UploadImage(config, store)) + mux.HandleFunc("POST /pic/upload", f.UploadImage(config, store)) + mux.HandleFunc("POST /tag/add", f.AddTag(config, db, store)) + mux.HandleFunc("POST /user/add", f.AddUser(config, db, store)) + mux.HandleFunc("POST /user/add-first", f.AddFirstUser(config, db, store)) + mux.HandleFunc("POST /user/update/{id}", f.UpdateUser(config, db, store)) + mux.HandleFunc("POST /user/update/self", f.UpdateSelf(config, db, store)) log.Fatalln(http.ListenAndServe(config.Port, mux)) } diff --git a/web/templates/add-user.html b/web/templates/add-user.html index 10376e1..a3c9cf7 100644 --- a/web/templates/add-user.html +++ b/web/templates/add-user.html @@ -45,7 +45,7 @@
- +
diff --git a/web/templates/current-articles.html b/web/templates/current-articles.html index c0a25e4..991622e 100644 --- a/web/templates/current-articles.html +++ b/web/templates/current-articles.html @@ -11,7 +11,7 @@
- +
{{end}} diff --git a/web/templates/edit-self.html b/web/templates/edit-self.html index f9beb0f..ee4bdf0 100644 --- a/web/templates/edit-self.html +++ b/web/templates/edit-self.html @@ -35,7 +35,7 @@
-
diff --git a/web/templates/edit-user.html b/web/templates/edit-user.html index 819f6df..d998f7b 100644 --- a/web/templates/edit-user.html +++ b/web/templates/edit-user.html @@ -49,7 +49,7 @@
-
diff --git a/web/templates/editor.html b/web/templates/editor.html index 2dccc55..043fec8 100644 --- a/web/templates/editor.html +++ b/web/templates/editor.html @@ -29,7 +29,7 @@
- +
@@ -46,7 +46,7 @@ var formData = new FormData(); formData.append('article-image', file); - fetch('/upload-image', { + fetch('/pic/upload', { method: 'POST', body: formData }) diff --git a/web/templates/first-user.html b/web/templates/first-user.html index dace772..9f045a6 100644 --- a/web/templates/first-user.html +++ b/web/templates/first-user.html @@ -26,7 +26,7 @@
- +
diff --git a/web/templates/hub.html b/web/templates/hub.html index 5b29fa7..c63eca6 100644 --- a/web/templates/hub.html +++ b/web/templates/hub.html @@ -6,10 +6,10 @@

Autor

- - + + RSS Feed - +
{{end}} @@ -18,10 +18,10 @@

Redakteur

- - +
{{end}} @@ -30,8 +30,8 @@

Herausgeber

- - + +
{{end}} @@ -40,9 +40,9 @@

Administrator

- - - + + +
{{end}} diff --git a/web/templates/published-articles.html b/web/templates/published-articles.html index b2d04cf..4a12863 100644 --- a/web/templates/published-articles.html +++ b/web/templates/published-articles.html @@ -3,7 +3,7 @@
{{range .}} - diff --git a/web/templates/rework-article.html b/web/templates/rework-article.html index a833587..70d0252 100644 --- a/web/templates/rework-article.html +++ b/web/templates/rework-article.html @@ -30,7 +30,7 @@
-
@@ -48,7 +48,7 @@ var formData = new FormData(); formData.append('article-image', file); - fetch('/upload-image', { + fetch('/pic/upload', { method: 'POST', body: formData }) diff --git a/web/templates/to-be-deleted.html b/web/templates/to-be-deleted.html index 47f753d..32967a2 100644 --- a/web/templates/to-be-deleted.html +++ b/web/templates/to-be-deleted.html @@ -28,7 +28,7 @@
-
diff --git a/web/templates/to-be-published.html b/web/templates/to-be-published.html index 7820093..e1607a7 100644 --- a/web/templates/to-be-published.html +++ b/web/templates/to-be-published.html @@ -28,9 +28,9 @@
- - +
diff --git a/web/templates/unpublished-articles.html b/web/templates/unpublished-articles.html index c189e87..9013e3d 100644 --- a/web/templates/unpublished-articles.html +++ b/web/templates/unpublished-articles.html @@ -3,7 +3,7 @@
{{range .}} - From c52e35bf0be243c242f151f04beaa946f6ff873f Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 30 Aug 2024 15:44:12 +0200 Subject: [PATCH 4/9] delete RSS button from hub --- web/templates/hub.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/web/templates/hub.html b/web/templates/hub.html index c63eca6..0c02910 100644 --- a/web/templates/hub.html +++ b/web/templates/hub.html @@ -8,7 +8,6 @@
- RSS Feed
@@ -39,7 +38,7 @@ {{if eq . 0}}

Administrator

-
+
From 2b2ab0d4283b49842bf9c5608dbcc0a5ee327de3 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 30 Aug 2024 15:58:09 +0200 Subject: [PATCH 5/9] small bug fixes --- cmd/main.go | 4 ++-- web/templates/add-tag.html | 2 +- web/templates/hub.html | 2 +- web/templates/rejected-articles.html | 2 +- web/templates/show-all-users.html | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 8a131f4..0b60d29 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -73,8 +73,8 @@ func main() { mux.HandleFunc("GET /user/delete/{id}", f.DeleteUser(config, db, store)) mux.HandleFunc("GET /user/edit/{id}", f.EditUser(config, db, store)) mux.HandleFunc("GET /user/edit/self", f.EditSelf(config, db, store)) - mux.HandleFunc("GET /user/show-all/delete", f.ShowAllUsers(config, db, store, "delete-user")) - mux.HandleFunc("GET /user/show-all/edit", f.ShowAllUsers(config, db, store, "edit-user")) + mux.HandleFunc("GET /user/show-all/delete", f.ShowAllUsers(config, db, store, "delete")) + mux.HandleFunc("GET /user/show-all/edit", f.ShowAllUsers(config, db, store, "edit")) mux.HandleFunc("POST /article/resubmit/{id}", f.ResubmitArticle(config, db, store)) mux.HandleFunc("POST /article/submit", f.SubmitArticle(config, db, store)) diff --git a/web/templates/add-tag.html b/web/templates/add-tag.html index e79865a..29532af 100644 --- a/web/templates/add-tag.html +++ b/web/templates/add-tag.html @@ -4,7 +4,7 @@
- +
diff --git a/web/templates/hub.html b/web/templates/hub.html index 0c02910..e5f4b87 100644 --- a/web/templates/hub.html +++ b/web/templates/hub.html @@ -7,7 +7,7 @@

Autor

- +
diff --git a/web/templates/rejected-articles.html b/web/templates/rejected-articles.html index 34fb75b..fab22e5 100644 --- a/web/templates/rejected-articles.html +++ b/web/templates/rejected-articles.html @@ -4,7 +4,7 @@
{{range .RejectedArticles}} {{if index $.MyIDs .ID}} - diff --git a/web/templates/show-all-users.html b/web/templates/show-all-users.html index 044433d..4f86f33 100644 --- a/web/templates/show-all-users.html +++ b/web/templates/show-all-users.html @@ -3,7 +3,7 @@
{{range .Users}} - - -
+
+ + +
+ + + {{end}} diff --git a/web/templates/editor.html b/web/templates/editor.html index 4d95b0b..ebbabbf 100644 --- a/web/templates/editor.html +++ b/web/templates/editor.html @@ -6,15 +6,17 @@
-
+ +
-
- + +
+ +
-
Tags @@ -46,7 +48,7 @@ var formData = new FormData(); formData.append('article-image', file); - fetch('/image/upload', { + fetch('/article/upload-image', { method: 'POST', body: formData }) diff --git a/web/templates/hub.html b/web/templates/hub.html index e5f4b87..4599a43 100644 --- a/web/templates/hub.html +++ b/web/templates/hub.html @@ -38,7 +38,7 @@ {{if eq . 0}}

Administrator

-
+
diff --git a/web/templates/rework-article.html b/web/templates/rework-article.html index e7d18a9..72dfa3a 100644 --- a/web/templates/rework-article.html +++ b/web/templates/rework-article.html @@ -6,15 +6,17 @@
-
+ +
-
- - + +
+ + +
-
Tags @@ -37,6 +39,8 @@