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%;
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) {
id := r.FormValue("id")
title := r.FormValue("title")
if title == "" {
rend.Error(http.StatusNotFound)
return
} else {
contentMd := r.FormValue("content")
contentHtml := helpers.MarkdownToHtml(contentMd)
contentCom := r.FormValue("content1")
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)
return
}
} 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)
return
}
}
}
rend.Redirect("/")
}

View File

@ -10,6 +10,7 @@ type Post struct {
Title string `bson:"title"`
ContentHtml string `bson:"content_html"`
ContentMd string `bson:"content_md"`
ContentCom string `bson:"content_com"`
}
//Post
@ -27,12 +28,13 @@ func (posts) FindAll() ([]*Post, error) {
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{
Id: id,
Title: title,
ContentMd: contentMd,
ContentHtml: contentHtml,
ContentCom: contentCom,
}
if err := common.DB.Posts.Insert(p); err != nil {
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,
bson.M{"$set": bson.M{
"title": title,
"content_html": contentHtml,
"content_md": contentMd,
"content_com": contentCom,
}}); err != nil {
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 class="row">
<div class="col-xs-2"></div>
<div class="col-xs-2">
</div>
<div class="col-xs-8">
{{ $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>
</form>
</div>
</div>
<div class="col-xs-2"></div>
</div>

View File

@ -15,6 +15,11 @@
<label for="content">Treść</label>
<textarea name="content" id="content">{{.ContentMd}}</textarea>
</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>
</form>
</div>