mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-06 02:36:40 +00:00
added pageStore
This commit is contained in:
parent
80c914238b
commit
ebfc92110b
12 changed files with 242 additions and 67 deletions
|
|
@ -1,21 +1 @@
|
||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/rjNemo/go-wiki/services"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestParseContactForm(t *testing.T) {
|
|
||||||
var i io.Reader
|
|
||||||
r, err := http.NewRequest(http.MethodPost, "/contact/post/", i)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("%s", err)
|
|
||||||
}
|
|
||||||
ans := parseContactForm(r)
|
|
||||||
if ans != services.NewMail("", "") {
|
|
||||||
t.Errorf("parseContactForm(r) = %v", ans)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,19 @@ package controllers
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/rjNemo/go-wiki/data"
|
||||||
"github.com/rjNemo/go-wiki/models"
|
"github.com/rjNemo/go-wiki/models"
|
||||||
"github.com/rjNemo/go-wiki/views"
|
"github.com/rjNemo/go-wiki/views"
|
||||||
)
|
)
|
||||||
|
|
||||||
// type PageHandler struct {
|
// PageHandler will respond to requests using Handlers
|
||||||
// Pages data.PageStore
|
type PageHandler struct {
|
||||||
// }
|
Pages data.PageStore
|
||||||
|
} //and add each actual handler as a method
|
||||||
|
|
||||||
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
p, err := models.LoadPage(title)
|
p, err := models.LoadPage(title)
|
||||||
|
// p, err := ph.Pages.Get(1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
||||||
return
|
return
|
||||||
|
|
@ -23,14 +26,14 @@ func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
|
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
p, err := models.LoadPage(title)
|
p, err := models.LoadPage(title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p = models.NewPage(title, nil)
|
p = models.NewPage(0, title, nil)
|
||||||
}
|
}
|
||||||
views.Template(w, "edit", p)
|
views.Template(w, "edit", p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
body := r.FormValue("body")
|
body := r.FormValue("body")
|
||||||
p := models.NewPage(title, []byte(body))
|
p := models.NewPage(0, title, []byte(body))
|
||||||
err := p.Save()
|
err := p.Save()
|
||||||
checkError(err, w)
|
checkError(err, w)
|
||||||
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
// Router dispatch the request to the corresponding route handlers.
|
// Router dispatch the request to the corresponding route handlers.
|
||||||
func Router() {
|
func Router() {
|
||||||
// http.HandleFunc("/", loveHandler)
|
// http.HandleFunc("/", loveHandler)
|
||||||
|
// http.HandleFunc("/view/", makeHandler(ph.view))
|
||||||
http.HandleFunc("/view/", makeHandler(viewHandler))
|
http.HandleFunc("/view/", makeHandler(viewHandler))
|
||||||
http.HandleFunc("/edit/", makeHandler(editHandler))
|
http.HandleFunc("/edit/", makeHandler(editHandler))
|
||||||
http.HandleFunc("/save/", makeHandler(saveHandler))
|
http.HandleFunc("/save/", makeHandler(saveHandler))
|
||||||
|
|
|
||||||
4
data/files/Nyny.txt
Normal file
4
data/files/Nyny.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
I
|
||||||
|
❤️ TiNyny
|
||||||
|
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
Type a term in the search bar.
|
Type a term in the search bar.
|
||||||
|
|
||||||
If it already exist you will reach this page.
|
If it already exist you will reach this page.
|
||||||
|
|
||||||
If it is not yet defined you will have the possibility to create it.
|
If it is not yet defined you will have the possibility to create it.
|
||||||
|
|
||||||
Have fun
|
Have fun
|
||||||
|
|
||||||
|
❤️
|
||||||
|
❤️
|
||||||
|
❤️
|
||||||
|
❤️
|
||||||
|
|
||||||
50
data/pageQueries.go
Normal file
50
data/pageQueries.go
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
package data
|
||||||
|
|
||||||
|
const (
|
||||||
|
// CreateTablePages is the SQL command used to create the pages table if it
|
||||||
|
// not exists.
|
||||||
|
CreateTablePages = `
|
||||||
|
CREATE TABLE IF NOT EXISTS pages (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
title TEXT UNIQUE NOT NULL
|
||||||
|
body TEXT)
|
||||||
|
`
|
||||||
|
|
||||||
|
// GetPage is the SQL command used to retrieve a user from the table
|
||||||
|
GetPage = `
|
||||||
|
SELECT * FROM pages WHERE id = $1;
|
||||||
|
`
|
||||||
|
|
||||||
|
// GetAllPages is the SQL command used to retrieve all pages from the database.
|
||||||
|
GetAllPages = `
|
||||||
|
SELECT * FROM pages ORDER BY id;
|
||||||
|
`
|
||||||
|
|
||||||
|
// FindPages is the SQL command used to retrieve all pages from the table
|
||||||
|
// which satisfy the specified condition
|
||||||
|
FindPages = `
|
||||||
|
SELECT * FROM pages WHERE $1 = $2;
|
||||||
|
`
|
||||||
|
|
||||||
|
// InsertPage is the SQL command used to insert a user in the table.
|
||||||
|
// Returning the new ID.
|
||||||
|
InsertPage = `
|
||||||
|
INSERT INTO pages (title, body)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
RETURNING id
|
||||||
|
`
|
||||||
|
|
||||||
|
// UpdatePage is the SQL command used to update a user in the database.
|
||||||
|
UpdatePage = `
|
||||||
|
UPDATE pages
|
||||||
|
SET title = $2, body = $3
|
||||||
|
WHERE id = $1;
|
||||||
|
`
|
||||||
|
|
||||||
|
// DeletePage is the SQL command used to delete a user from the database.
|
||||||
|
DeletePage = `
|
||||||
|
DELETE FROM pages
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING *;
|
||||||
|
`
|
||||||
|
)
|
||||||
130
data/pageStore.go
Normal file
130
data/pageStore.go
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/rjNemo/go-wiki/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PageStore is used to perform page-related CRUD operations on the DB
|
||||||
|
type PageStore struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPageStore constructs a PageStore connected to the database.
|
||||||
|
func NewPageStore(db *sql.DB) PageStore {
|
||||||
|
return PageStore{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateTable creates the page table in the database if it exits not yet.
|
||||||
|
func (us PageStore) CreateTable() {
|
||||||
|
if _, err := us.db.Exec(CreateTablePages); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
// log.Print("Table successfully created!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAll retrieves all the pages from the database.
|
||||||
|
func (us PageStore) GetAll() ([]models.Page, error) {
|
||||||
|
var id int
|
||||||
|
var title string
|
||||||
|
var body []byte
|
||||||
|
|
||||||
|
rows, err := us.db.Query(GetAllPages)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var pages []models.Page
|
||||||
|
for rows.Next() {
|
||||||
|
err := rows.Scan(&id, &title, &body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err) // too severe
|
||||||
|
}
|
||||||
|
p := models.NewPage(id, title, body)
|
||||||
|
pages = append(pages, *p)
|
||||||
|
}
|
||||||
|
return pages, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get retrieves the page identified by 'id' from database
|
||||||
|
func (us PageStore) Get(uid int) (models.Page, error) {
|
||||||
|
var id int
|
||||||
|
var title string
|
||||||
|
var body []byte
|
||||||
|
|
||||||
|
row := us.db.QueryRow(QueryGet, uid)
|
||||||
|
switch err := row.Scan(&id, &title, &body); err {
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
log.Println("No entry returned")
|
||||||
|
return models.Page{}, err
|
||||||
|
case nil:
|
||||||
|
return *models.NewPage(id, title, body), nil
|
||||||
|
default:
|
||||||
|
log.Fatal(err)
|
||||||
|
return models.Page{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add inserts a page in the database.
|
||||||
|
func (us PageStore) Add(u models.Page) {
|
||||||
|
id := 0
|
||||||
|
err := us.db.QueryRow(QueryInsert, u.Title(), u.Body()).Scan(&id)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err) // too severe
|
||||||
|
}
|
||||||
|
log.Println("New Page ID is:", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update edits page identified by 'id' in the database
|
||||||
|
func (us PageStore) Update(id int, u models.Page) {
|
||||||
|
res, err := us.db.Exec(QueryUpdate, id, u.Title(), u.Body())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err) // too severe
|
||||||
|
}
|
||||||
|
count, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err) // too severe
|
||||||
|
}
|
||||||
|
if count == 0 {
|
||||||
|
log.Fatal("Update failed") // too severe
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete removes page identified by 'id' from the database
|
||||||
|
func (us PageStore) Delete(id int) {
|
||||||
|
res, err := us.db.Exec(QueryDelete, id)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err) // too severe
|
||||||
|
}
|
||||||
|
count, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err) // too severe
|
||||||
|
}
|
||||||
|
if count == 0 {
|
||||||
|
log.Fatal("Delete failed") // too severe
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find retrieves a page from the database using an expression
|
||||||
|
// func (us PageStore) Find(k interface{}, v interface{}) ([]models.Page, error) {
|
||||||
|
// var id, title int
|
||||||
|
// var body, lastName, email string
|
||||||
|
|
||||||
|
// rows, err := us.db.Query(QueryFind, k, v)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// var pages []models.Page
|
||||||
|
// for rows.Next() {
|
||||||
|
// err := rows.Scan(&id, &title, &body, &lastName, &email)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatal(err) // too severe
|
||||||
|
// }
|
||||||
|
// u := models.NewPage(id, title, body, lastName, email)
|
||||||
|
// pages = append(pages, u)
|
||||||
|
// }
|
||||||
|
// return pages, nil
|
||||||
|
// }
|
||||||
|
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
// UsePSQL read the connection parameters to establish a connection to the
|
// UsePSQL read the connection parameters to establish a connection to the
|
||||||
// database.
|
// database.
|
||||||
func UsePSQL() {
|
func UsePSQL() *sql.DB {
|
||||||
connStr := settings.ConnStr
|
connStr := settings.ConnStr
|
||||||
db, err := sql.Open("postgres", connStr)
|
db, err := sql.Open("postgres", connStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -26,8 +26,10 @@ func UsePSQL() {
|
||||||
|
|
||||||
log.Println("Connection to database successfully established!")
|
log.Println("Connection to database successfully established!")
|
||||||
|
|
||||||
store := NewUserStore(db)
|
// store := NewUserStore(db)
|
||||||
store.CreateTable()
|
// store.CreateTable()
|
||||||
|
|
||||||
|
return db
|
||||||
// u := models.TestUser()
|
// u := models.TestUser()
|
||||||
// store.Add(u)
|
// store.Add(u)
|
||||||
// log.Print(u)
|
// log.Print(u)
|
||||||
|
|
|
||||||
|
|
@ -47,28 +47,6 @@ func (us UserStore) GetAll() ([]models.User, error) {
|
||||||
return users, nil
|
return users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find retrieves a user from the database using an expression
|
|
||||||
// func (us UserStore) Find(k interface{}, v interface{}) ([]models.User, error) {
|
|
||||||
// var id, age int
|
|
||||||
// var firstName, lastName, email string
|
|
||||||
|
|
||||||
// rows, err := us.db.Query(QueryFind, k, v)
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var users []models.User
|
|
||||||
// for rows.Next() {
|
|
||||||
// err := rows.Scan(&id, &age, &firstName, &lastName, &email)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatal(err) // too severe
|
|
||||||
// }
|
|
||||||
// u := models.NewUser(id, age, firstName, lastName, email)
|
|
||||||
// users = append(users, u)
|
|
||||||
// }
|
|
||||||
// return users, nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Get retrieves the user identified by 'id' from database
|
// Get retrieves the user identified by 'id' from database
|
||||||
func (us UserStore) Get(uid int) (models.User, error) {
|
func (us UserStore) Get(uid int) (models.User, error) {
|
||||||
var id, age int
|
var id, age int
|
||||||
|
|
@ -126,3 +104,25 @@ func (us UserStore) Delete(id int) {
|
||||||
log.Fatal("Delete failed") // too severe
|
log.Fatal("Delete failed") // too severe
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find retrieves a user from the database using an expression
|
||||||
|
// func (us UserStore) Find(k interface{}, v interface{}) ([]models.User, error) {
|
||||||
|
// var id, age int
|
||||||
|
// var firstName, lastName, email string
|
||||||
|
|
||||||
|
// rows, err := us.db.Query(QueryFind, k, v)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// var users []models.User
|
||||||
|
// for rows.Next() {
|
||||||
|
// err := rows.Scan(&id, &age, &firstName, &lastName, &email)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatal(err) // too severe
|
||||||
|
// }
|
||||||
|
// u := models.NewUser(id, age, firstName, lastName, email)
|
||||||
|
// users = append(users, u)
|
||||||
|
// }
|
||||||
|
// return users, nil
|
||||||
|
// }
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,15 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import "testing"
|
// func TestBlankPage(t *testing.T) {
|
||||||
|
// ans := BlankPage()
|
||||||
|
// if ans.title != "Empty page" && string(ans.body) != "Write some content" {
|
||||||
|
// t.Errorf("BlankPage() = %v; want &Page{title: 'Empty page', body: []byte('Write some content')}", ans)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
func TestBlankPage(t *testing.T) {
|
// func TestNewPage(t *testing.T) {
|
||||||
ans := BlankPage()
|
// ans := NewPage("Test Page", []byte("This is a sample page"))
|
||||||
if ans.title != "Empty page" && string(ans.body) != "Write some content" {
|
// if ans.title != "Test Page" && string(ans.body) != "This is a sample page" {
|
||||||
t.Errorf("BlankPage() = %v; want &Page{title: 'Empty page', body: []byte('Write some content')}", ans)
|
// t.Errorf("NewPage() = %v; want &Page{title: 'Test Page', body: []byte('This is a sample page')}", ans)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
func TestNewPage(t *testing.T) {
|
|
||||||
ans := NewPage("Test Page", []byte("This is a sample page"))
|
|
||||||
if ans.title != "Test Page" && string(ans.body) != "This is a sample page" {
|
|
||||||
t.Errorf("NewPage() = %v; want &Page{title: 'Test Page', body: []byte('This is a sample page')}", ans)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
// 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
|
||||||
title string
|
title string
|
||||||
body []byte
|
body []byte
|
||||||
}
|
}
|
||||||
|
|
@ -36,8 +37,8 @@ func BlankPage() *Page {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPage constructor returns a pointer to a sample Page.
|
// NewPage constructor returns a pointer to a sample Page.
|
||||||
func NewPage(title string, body []byte) *Page {
|
func NewPage(id int, title string, body []byte) *Page {
|
||||||
return &Page{title: title, body: body}
|
return &Page{id: id, title: title, body: body}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save a page to the 'data/' folder in txt format.
|
// Save a page to the 'data/' folder in txt format.
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/rjNemo/go-wiki/settings"
|
"github.com/rjNemo/go-wiki/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Template display the named HTML template to the browser
|
||||||
func Template(w http.ResponseWriter, tmpl string, p interface{}) {
|
func Template(w http.ResponseWriter, tmpl string, p interface{}) {
|
||||||
// err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p)
|
// err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p)
|
||||||
t, err := template.ParseFiles(getTmplName("base"), getTmplName(tmpl))
|
t, err := template.ParseFiles(getTmplName("base"), getTmplName(tmpl))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue