31.12.2018

This commit is contained in:
Mikolaj 2018-12-31 15:42:05 +01:00
parent cc9f05820c
commit fa36ccc8d5
7 changed files with 128 additions and 31 deletions

19
common/mongo.go Normal file
View File

@ -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")}
}

41
main.go
View File

@ -5,15 +5,19 @@ import (
"html/template" "html/template"
"net/http" "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/helpers"
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models" "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models"
"github.com/go-martini/martini" "github.com/go-martini/martini"
"github.com/martini-contrib/render" "github.com/martini-contrib/render"
) )
var posts map[string]*models.Post
func index(rend render.Render) { func index(rend render.Render) {
posts, err := models.Posts.FindAll()
if err != nil {
rend.Error(http.StatusBadRequest)
return
}
rend.HTML(http.StatusOK, "index", posts) 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) { func edit(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
id := params["id"] post, err := models.Posts.FindOne(params["id"])
post, found := posts[id] if err != nil {
if !found { rend.Error(http.StatusNotFound)
rend.Redirect("/")
return return
} }
rend.HTML(http.StatusOK, "write", post) 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") contentMd := r.FormValue("content")
contentHtml := helpers.MarkdownToHtml(contentMd) contentHtml := helpers.MarkdownToHtml(contentMd)
var post *models.Post
if id != "" { if id != "" {
post = posts[id] if err := models.Posts.Update(id, title, contentHtml, contentMd); err != nil {
post.Title = title rend.Error(http.StatusBadRequest)
post.ContentHtml = contentHtml return
post.ContentMd = contentMd }
} else { } 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("/") rend.Redirect("/")
} }
func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) { func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
id := params["id"] if err := models.Posts.DeletebyId(params["id"]); err != nil {
if id == "" { rend.Error(http.StatusNotFound)
http.NotFound(w, r) return
} }
delete(posts, id)
rend.Redirect("/") rend.Redirect("/")
} }
@ -74,7 +77,7 @@ func unescape(s string) interface{} {
} }
func main() { func main() {
posts = make(map[string]*models.Post, 0) common.ConnectDB()
fmt.Println("Listening port 3000") fmt.Println("Listening port 3000")
m := martini.Classic() m := martini.Classic()

View File

@ -1,12 +1,60 @@
package models package models
import (
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/common"
"gopkg.in/mgo.v2/bson"
)
type Post struct { type Post struct {
Id string Id string `bson:"_id,omitempty"`
Title string Title string `bson:"title"`
ContentHtml string ContentHtml string `bson:"content_html"`
ContentMd string ContentMd string `bson:"content_md"`
} }
func NewPost(id, title, contentHtml, contentMd string) *Post { //Post
return &Post{id, title, contentHtml, contentMd} 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
} }

View File

@ -13,6 +13,7 @@
{{ $value.ContentHtml | unescape }} {{ $value.ContentHtml | unescape }}
</div> </div>
<div class="col-xs-2"></div> <div class="col-xs-2"></div>
</div> </div>
{{end}} {{end}}

View File

@ -21,8 +21,8 @@
</div> </div>
<div class="collapse navbar-collapse"> <div class="collapse navbar-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li class="active"><a href="/">Home</a></li> <li class="active"><a href="/">Strona Głowna</a></li>
<li><a href="/write">New Post</a></li> <li><a href="/write">Nowy Post</a></li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@ -1,21 +1,21 @@
<div class="row"> <div class="row">
<div class="col-xs-2"> <div class="col-xs-2">
{{ if.Id }} {{ if.Id }}
<a href="/deletePost/{{.Id}}">Delete</a> <a href="/deletePost/{{.Id}}">Usuń</a>
{{ end }} {{ end }}
</div> </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" value="{{.Id}}"/> <input type="hidden" name="id" value="{{.Id}}"/>
<div class="form-group"> <div class="form-group">
<label for="title">Tytul</label> <label for="title">Tytuł</label>
<input type="text" class="form-control" id="title" name="title" value ="{{.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">Treść</label>
<textarea name="content" id="content">{{.ContentMd}}</textarea> <textarea name="content" id="content">{{.ContentMd}}</textarea>
</div> </div>
<button type="submit" class="btn btn-default">Submit</button> <button type="submit" class="btn btn-default">Akceptuj</button>
</form> </form>
</div> </div>
<div class="col-xs-6" id="md"> <div class="col-xs-6" id="md">

26
views/write1.html Normal file
View File

@ -0,0 +1,26 @@
<div class="row">
<div class="col-xs-2">
{{ if.Id }}
<a href="/deletePost/{{.Id}}">Usuń</a>
{{ end }}
</div>
<div class="col-xs-4">
<form action="/savePost" method="POST" role="form">
<input type="hidden" name="id" value="{{.Id}}"/>
<div class="form-group">
<label for="title">Tytuł</label>
<input type="text" class="form-control" id="title" name="title" value ="{{.Title}}"/>
</div>
<div class="form-group">
<label for="content">Treść</label>
<textarea name="content" id="content">{{.ContentMd}}</textarea>
</div>
<button type="submit" class="btn btn-default">Akceptuj</button>
</form>
</div>
<div class="col-xs-6" id="md">
{{ .ContentHtml | unescape}}
</div>
</div>