This commit is contained in:
Ruidy Nemausat 2020-03-19 09:04:54 +01:00
parent 334da43936
commit 0e78a9dc8a
7 changed files with 62 additions and 30 deletions

View file

@ -38,7 +38,7 @@ Add additional notes about how to deploy this on a live system
- [Go](https://golang.org/) - Build simple, reliable, and efficient software - [Go](https://golang.org/) - Build simple, reliable, and efficient software
- [Bootstrap](https://getbootstrap.com/) - The most popular HTML, CSS, and JS library in the world - [Bootstrap](https://getbootstrap.com/) - The most popular HTML, CSS, and JS library in the world
- [Quilljs](https://quilljs.com/) - Your powerful rich text editor <!-- - [Quilljs](https://quilljs.com/) - Your powerful rich text editor -->
## Contributing ## Contributing

View file

@ -41,15 +41,20 @@ func (ph PageHandler) edit(w http.ResponseWriter, r *http.Request, title string)
views.Template(w, "edit", p) views.Template(w, "edit", p)
} }
func (ph PageHandler) editor(w http.ResponseWriter, r *http.Request, title string) { // func (ph PageHandler) editor(w http.ResponseWriter, r *http.Request) {
log.Println(r.Body) // log.Println(r.Body)
} // views.Template(w, "editor", nil)
// }
func (ph PageHandler) save(w http.ResponseWriter, r *http.Request, title string) { func (ph PageHandler) save(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body") body := r.FormValue("body")
if !ph.Ctx.Pages.Exists(title) { if !ph.Ctx.Pages.Exists(title) {
// p, err := models.NewPage(0, title, []byte(body))
p := models.NewPage(0, title, []byte(body)) p := models.NewPage(0, title, []byte(body))
// if err != nil {
// log.Fatal(err)
// }
ph.Ctx.Pages.Add(*p) ph.Ctx.Pages.Add(*p)
} else { } else {
p, err := ph.Ctx.Pages.Get(title) p, err := ph.Ctx.Pages.Get(title)

View file

@ -16,7 +16,7 @@ func Router(ctx data.Context) {
http.HandleFunc("/index/", ph.index) http.HandleFunc("/index/", ph.index)
http.HandleFunc("/view/", makeHandler(ph.view)) http.HandleFunc("/view/", makeHandler(ph.view))
http.HandleFunc("/edit/", makeHandler(ph.edit)) http.HandleFunc("/edit/", makeHandler(ph.edit))
http.HandleFunc("/editor/", makeHandler(ph.editor)) // http.HandleFunc("/editor/", ph.editor)
http.HandleFunc("/save/", makeHandler(ph.save)) http.HandleFunc("/save/", makeHandler(ph.save))
http.HandleFunc("/new/", ph.new) http.HandleFunc("/new/", ph.new)
http.HandleFunc("/contact/", hh.contact) http.HandleFunc("/contact/", hh.contact)

View file

@ -28,7 +28,7 @@ func (ps PageStore) CreateTable() {
// GetAll retrieves all the pages from the database. // GetAll retrieves all the pages from the database.
func (ps PageStore) GetAll() ([]models.Page, error) { func (ps PageStore) GetAll() ([]models.Page, error) {
var id int var id uint
var title string var title string
var body []byte var body []byte
@ -43,7 +43,11 @@ func (ps PageStore) GetAll() ([]models.Page, error) {
if err != nil { if err != nil {
log.Fatal(err) // too severe log.Fatal(err) // too severe
} }
// p, err := models.NewPage(id, title, body)
p := models.NewPage(id, title, body) p := models.NewPage(id, title, body)
// if err != nil {
// log.Printf("Error loading page id=%d: %s", id, err)
// }
pages = append(pages, *p) pages = append(pages, *p)
} }
return pages, nil return pages, nil
@ -51,7 +55,7 @@ func (ps PageStore) GetAll() ([]models.Page, error) {
// Get retrieves the page identified by 'id' from database // Get retrieves the page identified by 'id' from database
func (ps PageStore) Get(t string) (models.Page, error) { func (ps PageStore) Get(t string) (models.Page, error) {
var id int var id uint
var title string var title string
var body []byte var body []byte
t = strings.Title(t) t = strings.Title(t)
@ -60,11 +64,20 @@ func (ps PageStore) Get(t string) (models.Page, error) {
case sql.ErrNoRows: case sql.ErrNoRows:
log.Println("No entry returned") log.Println("No entry returned")
return models.Page{}, err return models.Page{}, err
// return nil, err
case nil: case nil:
return *models.NewPage(id, title, body), nil return *models.NewPage(id, title, body), nil
// p, err := models.NewPage(id, title, body)
// if err != nil {
// return *p, nil
// }
// log.Fatal(err)
// return models.Page{}, err
// return nil, err
default: default:
log.Fatal(err) log.Fatal(err)
return models.Page{}, err return models.Page{}, err
// return nil, err
} }
} }
@ -79,7 +92,7 @@ func (ps PageStore) Add(u models.Page) {
} }
// Update edits page identified by 'id' in the database // Update edits page identified by 'id' in the database
func (ps PageStore) Update(id int, u models.Page) { func (ps PageStore) Update(id uint, u models.Page) {
res, err := ps.db.Exec(UpdatePage, id, u.Title(), string(u.Body())) res, err := ps.db.Exec(UpdatePage, id, u.Title(), string(u.Body()))
if err != nil { if err != nil {
log.Fatal(err) // too severe log.Fatal(err) // too severe
@ -94,7 +107,7 @@ func (ps PageStore) Update(id int, u models.Page) {
} }
// Delete removes page identified by 'id' from the database // Delete removes page identified by 'id' from the database
func (ps PageStore) Delete(id int) { func (ps PageStore) Delete(id uint) {
res, err := ps.db.Exec(QueryDelete, id) res, err := ps.db.Exec(QueryDelete, id)
if err != nil { if err != nil {
log.Fatal(err) // too severe log.Fatal(err) // too severe

View file

@ -11,6 +11,7 @@ import (
func main() { func main() {
log.Println("*** Go-wiki v0.1 ©2020 ***") log.Println("*** Go-wiki v0.1 ©2020 ***")
// connect to db // connect to db
db, err := data.NewDB(settings.ConnStr) db, err := data.NewDB(settings.ConnStr)
if err != nil { if err != nil {
@ -24,12 +25,11 @@ func main() {
ctx.Pages.CreateTable() ctx.Pages.CreateTable()
ctx.Users.CreateTable() ctx.Users.CreateTable()
log.Printf("Start Go-wiki server on http://localhost:%s", settings.Port)
port := ":" + settings.Port
// create handlers around context // create handlers around context
controllers.Router(ctx) controllers.Router(ctx)
// startServer // startServer
log.Printf("Start Go-wiki server on http://localhost:%s", settings.Port)
port := ":" + settings.Port
log.Fatal(http.ListenAndServe(port, nil)) log.Fatal(http.ListenAndServe(port, nil))
} }

View file

@ -1,19 +1,19 @@
package models package models
import ( import (
"io/ioutil" "errors"
"strings" "strings"
) )
// A Page own a wiki page data and has a title and a body. // A Page own a wiki page data and has a title and a body.
type Page struct { type Page struct {
id int id uint
title string title string
body []byte body []byte
} }
// ID exposes Page's title field // ID exposes Page's title field
func (p Page) ID() int { func (p Page) ID() uint {
return p.id return p.id
} }
@ -43,13 +43,24 @@ func BlankPage() *Page {
} }
// NewPage constructor returns a pointer to a sample Page. // NewPage constructor returns a pointer to a sample Page.
func NewPage(id int, title string, body []byte) *Page { // func NewPage(id uint, title string, body []byte) (*Page, error) {
func NewPage(id uint, title string, body []byte) *Page {
// if id < 0 {
// return nil, errNegID
// }
// if title == "" {
// return nil, errBlankTitle
// }
return &Page{ return &Page{
id: id, id: id,
title: strings.Title(title), title: strings.Title(title),
body: body} body: body}
// , nil
} }
var errBlankTitle = errors.New("The title must not be blank")
var errNegID = errors.New("The ID must be unsigned")
// Save a page to the 'data/' folder in txt format. // Save a page to the 'data/' folder in txt format.
// func (p *Page) Save() error { // func (p *Page) Save() error {
// fileName := "data/files/" + p.title + ".txt" // fileName := "data/files/" + p.title + ".txt"
@ -57,11 +68,11 @@ func NewPage(id int, title string, body []byte) *Page {
// } // }
// LoadPage reads a saved page data and returns a pointer to the Page. // LoadPage reads a saved page data and returns a pointer to the Page.
func LoadPage(title string) (*Page, error) { // func LoadPage(title string) (*Page, error) {
fileName := "data/files/" + title + ".txt" // fileName := "data/files/" + title + ".txt"
body, err := ioutil.ReadFile(fileName) // body, err := ioutil.ReadFile(fileName)
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
return &Page{title: title, body: body}, nil // return &Page{title: title, body: body}, nil
} // }

View file

@ -12,7 +12,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<form onsubmit="save()"> <form id="saveForm">
<input class="btn btn-primary" type="submit" value="Save changes" /> <input class="btn btn-primary" type="submit" value="Save changes" />
</form> </form>
</div> </div>
@ -26,21 +26,24 @@
theme: "snow" theme: "snow"
}); });
save = async () => { saveForm = document.getElementById("saveForm");
// e.preventDefault();
saveForm.addEventListener("submit", async e => {
e.preventDefault();
console.log("send to backend"); console.log("send to backend");
let text = quill.getContents(); let text = quill.getContents();
console.log(text); console.log(text);
response = await fetch("http://localhost:8080/editor", { response = await fetch("http://localhost:8080/editor", {
method: "POST", method: "post",
body: JSON.stringify(text), body: JSON.stringify(text),
headers: { headers: {
Accept: "application/json", Accept: "application/json",
"Content-Type": "application/json" "Content-Type": "application/json"
} }
}).then(r => console.log(r.json())); }); //.then(r => console.log(r.json()));
// .then(r => (r));
// return await response.json(); // return await response.json();
}; });
</script> </script>
{{end}} {{end}}