2018-12-08 23:15:39 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2018-12-10 19:42:28 +01:00
|
|
|
|
2018-12-11 20:42:44 +01:00
|
|
|
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers"
|
|
|
|
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models"
|
2018-12-13 18:13:47 +01:00
|
|
|
"github.com/go-martini/martini"
|
|
|
|
"github.com/martini-contrib/render"
|
2018-12-08 23:15:39 +01:00
|
|
|
)
|
|
|
|
|
2018-12-10 19:42:28 +01:00
|
|
|
var posts map[string]*models.Post
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
func index(rend render.Render) {
|
|
|
|
rend.HTML(http.StatusOK, "index", posts)
|
|
|
|
|
2018-12-10 19:42:28 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
func write(rend render.Render) {
|
|
|
|
rend.HTML(http.StatusOK, "write", &models.Post{})
|
2018-12-11 20:42:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
func edit(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
|
|
|
|
id := params["id"]
|
2018-12-11 20:42:44 +01:00
|
|
|
post, found := posts[id]
|
|
|
|
if !found {
|
2018-12-13 18:13:47 +01:00
|
|
|
rend.Redirect("/")
|
|
|
|
return
|
2018-12-11 20:42:44 +01:00
|
|
|
}
|
2018-12-13 18:13:47 +01:00
|
|
|
rend.HTML(http.StatusOK, "write", post)
|
2018-12-08 23:15:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
func savePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
|
2018-12-10 19:42:28 +01:00
|
|
|
id := r.FormValue("id")
|
|
|
|
title := r.FormValue("title")
|
|
|
|
content := r.FormValue("content")
|
|
|
|
|
2018-12-11 20:42:44 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
rend.Redirect("/")
|
2018-12-11 20:42:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
|
|
|
|
id := params["id"]
|
2018-12-11 20:42:44 +01:00
|
|
|
if id == "" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(posts, id)
|
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
rend.Redirect("/")
|
2018-12-10 19:42:28 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 23:15:39 +01:00
|
|
|
func main() {
|
2018-12-10 19:42:28 +01:00
|
|
|
posts = make(map[string]*models.Post, 0)
|
2018-12-08 23:15:39 +01:00
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
fmt.Println("Listening port 3000")
|
|
|
|
m := martini.Classic()
|
|
|
|
|
|
|
|
m.Use(render.Renderer(render.Options{
|
|
|
|
Directory: "views",
|
|
|
|
Layout: "layout",
|
|
|
|
Extensions: []string{".html"},
|
|
|
|
Charset: "UTF-8",
|
|
|
|
IndentJSON: true,
|
|
|
|
}))
|
|
|
|
|
|
|
|
staticOptions := martini.StaticOptions{Prefix: "bower_components"}
|
|
|
|
m.Use(martini.Static("bower_components", staticOptions))
|
|
|
|
m.Get("/", index)
|
|
|
|
m.Get("/write", write)
|
|
|
|
m.Get("/edit/:id", edit)
|
|
|
|
m.Post("/savePost", savePost)
|
|
|
|
m.Get("/deletePost/:id", deletePost)
|
2018-12-08 23:15:39 +01:00
|
|
|
|
2018-12-13 18:13:47 +01:00
|
|
|
m.Run()
|
2018-12-08 23:15:39 +01:00
|
|
|
}
|