01.02.2019

This commit is contained in:
Mikolaj 2019-02-01 13:06:00 +01:00
parent 9de6e5d0de
commit a88803ac6f
6 changed files with 95 additions and 16 deletions

View File

@ -6,3 +6,4 @@ textarea {
width: 100%; width: 100%;
height: 200px; height: 200px;
} }

11
main.go
View File

@ -38,21 +38,28 @@ func edit(rend render.Render, w http.ResponseWriter, r *http.Request, params mar
func savePost(rend render.Render, w http.ResponseWriter, r *http.Request) { func savePost(rend render.Render, w http.ResponseWriter, r *http.Request) {
id := r.FormValue("id") id := r.FormValue("id")
title := r.FormValue("title") title := r.FormValue("title")
if title == "" {
rend.Error(http.StatusNotFound)
return
} else {
contentMd := r.FormValue("content") contentMd := r.FormValue("content")
contentHtml := helpers.MarkdownToHtml(contentMd) contentHtml := helpers.MarkdownToHtml(contentMd)
contentCom := r.FormValue("content1")
if id != "" { if id != "" {
if err := models.Posts.Update(id, title, contentHtml, contentMd); err != nil { if err := models.Posts.Update(id, title, contentHtml, contentMd, contentCom); err != nil {
rend.Error(http.StatusBadRequest) rend.Error(http.StatusBadRequest)
return return
} }
} else { } else {
if _, err := models.Posts.Create(helpers.GenerateId(), title, contentHtml, contentMd); err != nil { if _, err := models.Posts.Create(helpers.GenerateId(), title, contentHtml, contentMd, contentCom); err != nil {
rend.Error(http.StatusBadRequest) rend.Error(http.StatusBadRequest)
return return
} }
} }
}
rend.Redirect("/") rend.Redirect("/")
} }

View File

@ -10,6 +10,7 @@ type Post struct {
Title string `bson:"title"` Title string `bson:"title"`
ContentHtml string `bson:"content_html"` ContentHtml string `bson:"content_html"`
ContentMd string `bson:"content_md"` ContentMd string `bson:"content_md"`
ContentCom string `bson:"content_com"`
} }
//Post //Post
@ -27,12 +28,13 @@ func (posts) FindAll() ([]*Post, error) {
return ps, common.DB.Posts.Find(nil).All(&ps) return ps, common.DB.Posts.Find(nil).All(&ps)
} }
func (posts) Create(id, title, contentHtml, contentMd string) (*Post, error) { func (posts) Create(id, title, contentHtml, contentMd, contentCom string) (*Post, error) {
p := &Post{ p := &Post{
Id: id, Id: id,
Title: title, Title: title,
ContentMd: contentMd, ContentMd: contentMd,
ContentHtml: contentHtml, ContentHtml: contentHtml,
ContentCom: contentCom,
} }
if err := common.DB.Posts.Insert(p); err != nil { if err := common.DB.Posts.Insert(p); err != nil {
return nil, err return nil, err
@ -41,12 +43,13 @@ func (posts) Create(id, title, contentHtml, contentMd string) (*Post, error) {
} }
func (posts) Update(id, title, contentHtml, contentMd string) error { func (posts) Update(id, title, contentHtml, contentMd, contentCom string) error {
if err := common.DB.Posts.UpdateId(id, if err := common.DB.Posts.UpdateId(id,
bson.M{"$set": bson.M{ bson.M{"$set": bson.M{
"title": title, "title": title,
"content_html": contentHtml, "content_html": contentHtml,
"content_md": contentMd, "content_md": contentMd,
"content_com": contentCom,
}}); err != nil { }}); err != nil {
return err return err
} }

45
test.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //Parse url parameters passed, then parse the response packet for the POST body (request body)
// attention: If you do not call ParseForm method, the following data can not be obtained form
fmt.Println(r.Form) // print information on server side.
fmt.Println("path", r.URL.Path)
fmt.Println("scheme", r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("val:", strings.Join(v, ""))
}
fmt.Fprintf(w, "Hello astaxie!") // write data to response
}
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //get request method
if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
} else {
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
}
}
func main() {
http.HandleFunc("/", sayhelloName) // setting router rule
http.HandleFunc("/login", login)
err := http.ListenAndServe(":9090", nil) // setting listening port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

View File

@ -8,11 +8,29 @@
</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"> <div class="col-xs-8">
{{ $value.ContentHtml | unescape }} {{ $value.ContentHtml | unescape }}
<div class="col-xs-8">
<br/><br/>Komentarz
<fieldset style="border: 2px solid; width: 450px; height: 160px; padding: 3;">{{ $value.ContentCom | unescape }}</fieldset>
<form action="/createCom" method="POST" role="form"></form>
<input type="hidden" name="id" value="{{.Id}}"/>
<div class="form-group">
</div> </div>
</form>
</div>
</div>
<div class="col-xs-2"></div> <div class="col-xs-2"></div>
</div> </div>

View File

@ -15,6 +15,11 @@
<label for="content">Treść</label> <label for="content">Treść</label>
<textarea name="content" id="content">{{.ContentMd}}</textarea> <textarea name="content" id="content">{{.ContentMd}}</textarea>
</div> </div>
<div class="form-group"><br>
<label for="content1">Kom</label>
<textarea name="content1" id="content1">{{.ContentCom}}</textarea>
</div>
<button type="submit" class="btn btn-default">Akceptuj</button> <button type="submit" class="btn btn-default">Akceptuj</button>
</form> </form>
</div> </div>