diff options
author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-10-09 12:29:35 +0200 |
---|---|---|
committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-10-09 12:30:10 +0200 |
commit | 01679e4633eac76ce43d9d07dfa57b2548c97cc5 (patch) | |
tree | c5b0a3bb560050bc2125d17bde6b3bf4afd32a33 | |
parent | cffe6351a9f8e2777b6d4c078c89bd8f215de603 (diff) |
Change CreatedAt to LastUpdated
- The field now changes when the idea is edited.
-rw-r--r-- | main.go | 44 | ||||
-rw-r--r-- | t_idea/edit.html | 2 | ||||
-rw-r--r-- | t_idea/index.html | 2 |
3 files changed, 28 insertions, 20 deletions
@@ -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) diff --git a/t_idea/edit.html b/t_idea/edit.html index 725a111..7af6b0b 100644 --- a/t_idea/edit.html +++ b/t_idea/edit.html @@ -34,7 +34,7 @@ input[name="title"] { <form action="/idea/edit/" method="post"> <input name="title" type="text" value="{{.Title}}"><br> <textarea name="text" rows=6 required>{{.Text}}</textarea> - <p class="creation">by <span class="author">{{.Author}}</span> on <span class="date">{{.CreatedAt}}</span></p> + <p class="creation">by <span class="author">{{.Author}}</span> on <span class="date">{{.LastUpdated}}</span></p> <input type="submit" value="confirm"> </form> <form action="/ideas/" method="get"> diff --git a/t_idea/index.html b/t_idea/index.html index 97b68dd..4b1d6c0 100644 --- a/t_idea/index.html +++ b/t_idea/index.html @@ -29,7 +29,7 @@ <div class="idea"> <h2 class="title">{{.Title}}</h2> <pre class="text">{{.Text}}</pre> - <p class="creation">by <span class="author">{{.Author}}</span> on <span class="date">{{.CreatedAt}}</span></p> + <p class="creation">by <span class="author">{{.Author}}</span> on <span class="date">{{.LastUpdated}}</span></p> <form action="/idea/delete/" method="post"> <input type="hidden" name="title" value="{{.Title}}"> <input type="submit" value="delete"> |