aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-09 12:29:35 +0200
committerRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-09 12:30:10 +0200
commit01679e4633eac76ce43d9d07dfa57b2548c97cc5 (patch)
treec5b0a3bb560050bc2125d17bde6b3bf4afd32a33 /main.go
parentcffe6351a9f8e2777b6d4c078c89bd8f215de603 (diff)
Change CreatedAt to LastUpdated
- The field now changes when the idea is edited.
Diffstat (limited to 'main.go')
-rw-r--r--main.go44
1 files changed, 26 insertions, 18 deletions
diff --git a/main.go b/main.go
index 6e56248..7497dc9 100644
--- a/main.go
+++ b/main.go
@@ -1,5 +1,18 @@
package main
+// ideez is a web application that allows people to post their ideas, it is meant to help
+// brainstorming.
+
+// ToDo's
+// - [ ] Create a Server out of this so you can run multiple instances
+// - [ ] change CreatedAt to be Last Updated
+// - [x] edit a post
+// - [x] Store ideas to a file (encoder/gob)
+// - [x] Change the date format printing
+// - [x] outsource removing the posts to a separate cli tool
+// - [x] Add a post
+// - [x] Remove a post
+
import (
_ "embed"
"encoding/gob"
@@ -34,24 +47,15 @@ var (
var Ideas []Idea
-// ToDo's
-// - [ ] Create a Server out of this so you can run multiple instances
-// - [x] edit a post
-// - [x] Store ideas to a file (encoder/gob)
-// - [x] Change the date format printing
-// - [x] outsource removing the posts to a separate cli tool
-// - [x] Add a post
-// - [x] Remove a post
-
// Represents an idea
-// CreatedAt is a formatted date string
+// LastUpdated is a formatted date string
// out of 5 rating of the idea
// formatted time string with DateLayout
type Idea struct {
- Title string
- Text string
- Author string
- CreatedAt string
+ Title string
+ Text string
+ Author string
+ LastUpdated string
}
// Data passed to the ideas_html template
@@ -179,10 +183,10 @@ func main() {
mux.HandleFunc("POST /idea/create/", func(w http.ResponseWriter, r *http.Request) {
i := Idea{
- Title: r.FormValue("title"),
- Author: r.FormValue("author"),
- CreatedAt: time.Now().Format(DateLayout),
- Text: r.FormValue("text"),
+ Title: r.FormValue("title"),
+ Author: r.FormValue("author"),
+ LastUpdated: time.Now().Format(DateLayout),
+ Text: r.FormValue("text"),
}
if i.Title == "" || i.Author == "" || i.Text == "" {
tmpl.Execute(w, PageData{Ideas, "All fields are required"})
@@ -201,6 +205,8 @@ func main() {
http.Redirect(w, r, "/ideas/", http.StatusMovedPermanently)
})
+ // A page to edit the idea, this page should lead to POST /idea/edit for confirming the edit. If
+ // the user cancels they should be redirected to the start page. This is done in the html.
mux.HandleFunc("GET /idea/edit/", func(w http.ResponseWriter, r *http.Request) {
t := r.URL.Query().Get("t")
if t == "" {
@@ -217,6 +223,7 @@ func main() {
tmpl.Execute(w, PageData{Ideas, "No idea with title '" + t + "'."})
})
+ // Perform the edit action on the idea
mux.HandleFunc("POST /idea/edit/", func(w http.ResponseWriter, r *http.Request) {
t := r.FormValue("title")
if t == "" {
@@ -237,6 +244,7 @@ func main() {
i.Title = r.FormValue("title")
i.Text = r.FormValue("text")
+ i.LastUpdated = time.Now().Format(DateLayout)
log.Printf("Edited '%s'\n", i.Title)
http.Redirect(w, r, "/ideas/", http.StatusMovedPermanently)