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
- [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

View file

@ -41,15 +41,20 @@ func (ph PageHandler) edit(w http.ResponseWriter, r *http.Request, title string)
views.Template(w, "edit", p)
}
func (ph PageHandler) editor(w http.ResponseWriter, r *http.Request, title string) {
log.Println(r.Body)
}
// func (ph PageHandler) editor(w http.ResponseWriter, r *http.Request) {
// log.Println(r.Body)
// views.Template(w, "editor", nil)
// }
func (ph PageHandler) save(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
if !ph.Ctx.Pages.Exists(title) {
// p, err := models.NewPage(0, title, []byte(body))
p := models.NewPage(0, title, []byte(body))
// if err != nil {
// log.Fatal(err)
// }
ph.Ctx.Pages.Add(*p)
} else {
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("/view/", makeHandler(ph.view))
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("/new/", ph.new)
http.HandleFunc("/contact/", hh.contact)

View file

@ -28,7 +28,7 @@ func (ps PageStore) CreateTable() {
// GetAll retrieves all the pages from the database.
func (ps PageStore) GetAll() ([]models.Page, error) {
var id int
var id uint
var title string
var body []byte
@ -43,7 +43,11 @@ func (ps PageStore) GetAll() ([]models.Page, error) {
if err != nil {
log.Fatal(err) // too severe
}
// p, err := 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)
}
return pages, nil
@ -51,7 +55,7 @@ func (ps PageStore) GetAll() ([]models.Page, error) {
// Get retrieves the page identified by 'id' from database
func (ps PageStore) Get(t string) (models.Page, error) {
var id int
var id uint
var title string
var body []byte
t = strings.Title(t)
@ -60,11 +64,20 @@ func (ps PageStore) Get(t string) (models.Page, error) {
case sql.ErrNoRows:
log.Println("No entry returned")
return models.Page{}, err
// return nil, err
case 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:
log.Fatal(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
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()))
if err != nil {
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
func (ps PageStore) Delete(id int) {
func (ps PageStore) Delete(id uint) {
res, err := ps.db.Exec(QueryDelete, id)
if err != nil {
log.Fatal(err) // too severe

View file

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

View file

@ -1,19 +1,19 @@
package models
import (
"io/ioutil"
"errors"
"strings"
)
// A Page own a wiki page data and has a title and a body.
type Page struct {
id int
id uint
title string
body []byte
}
// ID exposes Page's title field
func (p Page) ID() int {
func (p Page) ID() uint {
return p.id
}
@ -43,13 +43,24 @@ func BlankPage() *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{
id: id,
title: strings.Title(title),
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.
// func (p *Page) Save() error {
// 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.
func LoadPage(title string) (*Page, error) {
fileName := "data/files/" + title + ".txt"
body, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
return &Page{title: title, body: body}, nil
}
// func LoadPage(title string) (*Page, error) {
// fileName := "data/files/" + title + ".txt"
// body, err := ioutil.ReadFile(fileName)
// if err != nil {
// return nil, err
// }
// return &Page{title: title, body: body}, nil
// }

View file

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