diff --git a/common/mongo.go b/common/mongo.go new file mode 100644 index 0000000..97dd9cf --- /dev/null +++ b/common/mongo.go @@ -0,0 +1,19 @@ +package common + +import ( + "gopkg.in/mgo.v2" +) + +type mongo struct { + Posts *mgo.Collection +} + +var DB *mongo + +func ConnectDB() { + session, err := mgo.Dial("localhost") + if err != nil { + panic(err) + } + DB = &mongo{Posts: session.DB("blogdb").C("posts")} +} diff --git a/main.go b/main.go index b7ba384..d87aa3e 100644 --- a/main.go +++ b/main.go @@ -5,15 +5,19 @@ import ( "html/template" "net/http" + "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/common" "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers" "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models" "github.com/go-martini/martini" "github.com/martini-contrib/render" ) -var posts map[string]*models.Post - func index(rend render.Render) { + posts, err := models.Posts.FindAll() + if err != nil { + rend.Error(http.StatusBadRequest) + return + } rend.HTML(http.StatusOK, "index", posts) } @@ -23,10 +27,9 @@ func write(rend render.Render) { } func edit(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) { - id := params["id"] - post, found := posts[id] - if !found { - rend.Redirect("/") + post, err := models.Posts.FindOne(params["id"]) + if err != nil { + rend.Error(http.StatusNotFound) return } rend.HTML(http.StatusOK, "write", post) @@ -38,28 +41,28 @@ func savePost(rend render.Render, w http.ResponseWriter, r *http.Request) { contentMd := r.FormValue("content") contentHtml := helpers.MarkdownToHtml(contentMd) - var post *models.Post if id != "" { - post = posts[id] - post.Title = title - post.ContentHtml = contentHtml - post.ContentMd = contentMd + if err := models.Posts.Update(id, title, contentHtml, contentMd); err != nil { + rend.Error(http.StatusBadRequest) + return + } } else { - post := models.NewPost(helpers.GenerateId(), title, contentHtml, contentMd) - posts[post.Id] = post + + if _, err := models.Posts.Create(helpers.GenerateId(), title, contentHtml, contentMd); err != nil { + rend.Error(http.StatusBadRequest) + return + } } rend.Redirect("/") } func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) { - id := params["id"] - if id == "" { - http.NotFound(w, r) + if err := models.Posts.DeletebyId(params["id"]); err != nil { + rend.Error(http.StatusNotFound) + return } - delete(posts, id) - rend.Redirect("/") } @@ -74,7 +77,7 @@ func unescape(s string) interface{} { } func main() { - posts = make(map[string]*models.Post, 0) + common.ConnectDB() fmt.Println("Listening port 3000") m := martini.Classic() diff --git a/models/post.go b/models/post.go index 2fbdf2a..426180f 100644 --- a/models/post.go +++ b/models/post.go @@ -1,12 +1,60 @@ package models +import ( + "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/common" + "gopkg.in/mgo.v2/bson" +) + type Post struct { - Id string - Title string - ContentHtml string - ContentMd string + Id string `bson:"_id,omitempty"` + Title string `bson:"title"` + ContentHtml string `bson:"content_html"` + ContentMd string `bson:"content_md"` } -func NewPost(id, title, contentHtml, contentMd string) *Post { - return &Post{id, title, contentHtml, contentMd} +//Post +var Posts = new(posts) + +type posts struct{} + +func (posts) FindOne(id string) (*Post, error) { + var p *Post + return p, common.DB.Posts.Find(bson.M{"_id": id}).One(&p) +} + +func (posts) FindAll() ([]*Post, error) { + var ps []*Post + return ps, common.DB.Posts.Find(nil).All(&ps) +} + +func (posts) Create(id, title, contentHtml, contentMd string) (*Post, error) { + p := &Post{ + Id: id, + Title: title, + ContentMd: contentMd, + ContentHtml: contentHtml, + } + if err := common.DB.Posts.Insert(p); err != nil { + return nil, err + } + return p, nil + +} + +func (posts) Update(id, title, contentHtml, contentMd string) error { + if err := common.DB.Posts.UpdateId(id, + bson.M{"$set": bson.M{ + "title": title, + "content_html": contentHtml, + "content_md": contentMd, + }}); err != nil { + return err + } + return nil +} +func (posts) DeletebyId(id string) error { + if err := common.DB.Posts.RemoveId(id); err != nil { + return err + } + return nil } diff --git a/views/index.html b/views/index.html index e33fe57..52f2fb9 100644 --- a/views/index.html +++ b/views/index.html @@ -12,6 +12,7 @@
{{ $value.ContentHtml | unescape }}
+
diff --git a/views/layout.html b/views/layout.html index 8a918d1..919f402 100644 --- a/views/layout.html +++ b/views/layout.html @@ -21,8 +21,8 @@ diff --git a/views/write.html b/views/write.html index 5592fb0..7506b55 100644 --- a/views/write.html +++ b/views/write.html @@ -1,21 +1,21 @@
{{ if.Id }} - Delete + Usuń {{ end }}
- +
- +
- +
diff --git a/views/write1.html b/views/write1.html new file mode 100644 index 0000000..7506b55 --- /dev/null +++ b/views/write1.html @@ -0,0 +1,26 @@ +
+
+ {{ if.Id }} + Usuń + {{ end }} +
+
+
+ +
+ + +
+
+ + +
+ +
+
+
+ {{ .ContentHtml | unescape}} + +
+
+