11.12.2018
This commit is contained in:
parent
6e4b3bb045
commit
e0b131be8f
12
helpers/random.go
Normal file
12
helpers/random.go
Normal file
@ -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)
|
||||||
|
}
|
52
main.go
52
main.go
@ -5,7 +5,8 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"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
|
var posts map[string]*models.Post
|
||||||
@ -16,11 +17,12 @@ func index(w http.ResponseWriter, r *http.Request) {
|
|||||||
"views/write.html",
|
"views/write.html",
|
||||||
"views/header.html",
|
"views/header.html",
|
||||||
"views/footer.html",
|
"views/footer.html",
|
||||||
|
"views/index.html",
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, err.Error())
|
fmt.Fprintf(w, err.Error())
|
||||||
}
|
}
|
||||||
t.ExecuteTemplate(w, "write", nil)
|
t.ExecuteTemplate(w, "index", posts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func write(w http.ResponseWriter, r *http.Request) {
|
func write(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -28,11 +30,30 @@ func write(w http.ResponseWriter, r *http.Request) {
|
|||||||
"views/index.html",
|
"views/index.html",
|
||||||
"views/header.html",
|
"views/header.html",
|
||||||
"views/footer.html",
|
"views/footer.html",
|
||||||
|
"views/write.html",
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, err.Error())
|
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) {
|
func savePost(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -40,8 +61,27 @@ func savePost(w http.ResponseWriter, r *http.Request) {
|
|||||||
title := r.FormValue("title")
|
title := r.FormValue("title")
|
||||||
content := r.FormValue("content")
|
content := r.FormValue("content")
|
||||||
|
|
||||||
post := models.NewPost(id, title, content)
|
var post *models.Post
|
||||||
post[post.Id] = 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)
|
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.Handle("/bower_components/", http.StripPrefix("/bower_components/", http.FileServer(http.Dir("./bower_components/"))))
|
||||||
http.HandleFunc("/", index)
|
http.HandleFunc("/", index)
|
||||||
http.HandleFunc("/write", write)
|
http.HandleFunc("/write", write)
|
||||||
|
http.HandleFunc("/edit", edit)
|
||||||
http.HandleFunc("/savePost", savePost)
|
http.HandleFunc("/savePost", savePost)
|
||||||
|
http.HandleFunc("/deletePost", deletePost)
|
||||||
|
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type Psot struct {
|
type Post struct {
|
||||||
Id string
|
Id string
|
||||||
Title string
|
Title string
|
||||||
Content string
|
Content string
|
||||||
|
@ -2,17 +2,25 @@
|
|||||||
|
|
||||||
{{ template "header" }}
|
{{ template "header" }}
|
||||||
|
|
||||||
|
{{ range $key, $value := . }}
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-xs-2"></div>
|
<div class="col-xs-2"></div>
|
||||||
<div class="col-xs-2"></div>
|
<div class="col-xs-8">
|
||||||
|
<h1><a href ="/edit?id={{$value.Id}}">{{ $value.Title }}</a></h1>
|
||||||
|
</div>
|
||||||
<div class="col-xs-2"></div>
|
<div class="col-xs-2"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-xs-2"></div>
|
<div class="col-xs-2"></div>
|
||||||
|
<div class="col-xs-8">
|
||||||
|
{{ $value.Content }}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="col-xs-2"></div>
|
<div class="col-xs-2"></div>
|
||||||
<div class="col-xs-2"></div>
|
</div>
|
||||||
</div>
|
{{end}}
|
||||||
|
|
||||||
|
|
||||||
{{ template "footer" }}
|
{{ template "footer" }}
|
||||||
|
@ -5,20 +5,22 @@
|
|||||||
<div class="col-xs-4"></div>
|
<div class="col-xs-4"></div>
|
||||||
<div class="col-xs-4">
|
<div class="col-xs-4">
|
||||||
<form action="/savePost" method="POST" role="form">
|
<form action="/savePost" method="POST" role="form">
|
||||||
<input type="hidden" name="id"/>
|
<input type="hidden" name="id" value="{{.Id}}"/>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="title">Tytul</label>
|
<label for="title">Tytul</label>
|
||||||
<input type="text" class="form-control" id="title" name="title"/>
|
<input type="text" class="form-control" id="title" name="title" value ="{{.Title}}"/>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="content">Content</label>
|
<label for="content">Content</label>
|
||||||
<textarea name="content" id="content"></textarea>
|
<textarea name="content" id="content">{{.Content}}</textarea>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn btn-default">Submit</button>
|
<button type="submit" class="btn btn-default">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-xs-4">
|
<div class="col-xs-4">
|
||||||
<a href="deletePost">Usun</a>
|
{{ if.Id }}
|
||||||
|
<a href="deletePost?id={{.Id}}">Delete</a>
|
||||||
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user