From e0b131be8f131b0187de931ba4bbf5f7e3801dbb Mon Sep 17 00:00:00 2001 From: Mikolaj Date: Tue, 11 Dec 2018 20:42:44 +0100 Subject: [PATCH] 11.12.2018 --- helpers/random.go | 12 +++++++++++ main.go | 52 ++++++++++++++++++++++++++++++++++++++++++----- models/post.go | 2 +- views/index.html | 14 ++++++++++--- views/write.html | 10 +++++---- 5 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 helpers/random.go diff --git a/helpers/random.go b/helpers/random.go new file mode 100644 index 0000000..6def825 --- /dev/null +++ b/helpers/random.go @@ -0,0 +1,12 @@ +package helpers + +import ( + "crypto/rand" + "fmt" +) + +func GenerateId() string { + b := make([]byte, 8) + rand.Read(b) + return fmt.Sprintf("%x", b) +} diff --git a/main.go b/main.go index d71eca3..4413b63 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,8 @@ import ( "html/template" "net/http" - "github.com/blog/models" + "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers" + "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models" ) var posts map[string]*models.Post @@ -16,11 +17,12 @@ func index(w http.ResponseWriter, r *http.Request) { "views/write.html", "views/header.html", "views/footer.html", + "views/index.html", ) if err != nil { fmt.Fprintf(w, err.Error()) } - t.ExecuteTemplate(w, "write", nil) + t.ExecuteTemplate(w, "index", posts) } func write(w http.ResponseWriter, r *http.Request) { @@ -28,11 +30,30 @@ func write(w http.ResponseWriter, r *http.Request) { "views/index.html", "views/header.html", "views/footer.html", + "views/write.html", ) if err != nil { fmt.Fprintf(w, err.Error()) } - t.ExecuteTemplate(w, "index", nil) + t.ExecuteTemplate(w, "write", nil) +} + +func edit(w http.ResponseWriter, r *http.Request) { + t, err := template.ParseFiles( + "views/index.html", + "views/header.html", + "views/footer.html", + "views/write.html", + ) + if err != nil { + fmt.Fprintf(w, err.Error()) + } + id := r.FormValue("id") + post, found := posts[id] + if !found { + http.NotFound(w, r) + } + t.ExecuteTemplate(w, "write", post) } func savePost(w http.ResponseWriter, r *http.Request) { @@ -40,8 +61,27 @@ func savePost(w http.ResponseWriter, r *http.Request) { title := r.FormValue("title") content := r.FormValue("content") - post := models.NewPost(id, title, content) - post[post.Id] = post + var post *models.Post + if id != "" { + post = posts[id] + post.Title = title + post.Content = content + } else { + post := models.NewPost(helpers.GenerateId(), title, content) + posts[post.Id] = post + } + + http.Redirect(w, r, "/", 302) +} + +func deletePost(w http.ResponseWriter, r *http.Request) { + id := r.FormValue("id") + if id == "" { + http.NotFound(w, r) + } + + delete(posts, id) + http.Redirect(w, r, "/", 302) } @@ -52,7 +92,9 @@ func main() { http.Handle("/bower_components/", http.StripPrefix("/bower_components/", http.FileServer(http.Dir("./bower_components/")))) http.HandleFunc("/", index) http.HandleFunc("/write", write) + http.HandleFunc("/edit", edit) http.HandleFunc("/savePost", savePost) + http.HandleFunc("/deletePost", deletePost) http.ListenAndServe(":8080", nil) } diff --git a/models/post.go b/models/post.go index 4275b29..5af6fae 100644 --- a/models/post.go +++ b/models/post.go @@ -1,6 +1,6 @@ package models -type Psot struct { +type Post struct { Id string Title string Content string diff --git a/views/index.html b/views/index.html index 9fa2bb0..facad39 100644 --- a/views/index.html +++ b/views/index.html @@ -2,17 +2,25 @@ {{ template "header" }} +{{ range $key, $value := . }} +
-
+
+
+ {{ $value.Content }} +
+
-
-
+ +{{end}} {{ template "footer" }} diff --git a/views/write.html b/views/write.html index 3677cce..208d103 100644 --- a/views/write.html +++ b/views/write.html @@ -5,20 +5,22 @@
- +
- +
- +
- Usun + {{ if.Id }} + Delete + {{ end }}