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"
"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()

View File

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

View File

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

View File

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

View File

@ -1,21 +1,21 @@
<div class="row">
<div class="col-xs-2">
{{ if.Id }}
<a href="/deletePost/{{.Id}}">Delete</a>
<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">Tytul</label>
<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">Content</label>
<label for="content">Treść</label>
<textarea name="content" id="content">{{.ContentMd}}</textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
<button type="submit" class="btn btn-default">Akceptuj</button>
</form>
</div>
<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>