First usable version
This commit is contained in:
parent
c5521ba101
commit
52056ef9a1
@ -6,6 +6,7 @@ import (
|
||||
|
||||
type Page struct {
|
||||
gorm.Model
|
||||
Code string
|
||||
Price uint
|
||||
Name string
|
||||
Body string
|
||||
Version uint
|
||||
}
|
||||
|
@ -3,7 +3,8 @@ package model
|
||||
// Store interface
|
||||
type Store interface {
|
||||
Open() error
|
||||
AddPage()
|
||||
GetPage() Page
|
||||
AddPage(string, string) error
|
||||
CreatePage(Page) error
|
||||
GetPage(string) (Page, error)
|
||||
GetAllPages() ([]Page, error)
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@ -26,15 +27,32 @@ func (s *StoreGorm) Open() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StoreGorm) AddPage() {
|
||||
s.db.Create(&Page{Code: "D55", Price: 200})
|
||||
func (s *StoreGorm) AddPage(name string, body string) error {
|
||||
var home Page
|
||||
home.Name = name
|
||||
home.Body = body
|
||||
home.Version = 1
|
||||
tx := s.db.Create(&home)
|
||||
if tx.Error != nil {
|
||||
return tx.Error
|
||||
}
|
||||
fmt.Println("Page created")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StoreGorm) GetPage() Page {
|
||||
func (s *StoreGorm) CreatePage(page Page) error {
|
||||
tx := s.db.Save(&page)
|
||||
return tx.Error
|
||||
}
|
||||
|
||||
func (s *StoreGorm) GetPage(name string) (Page, error) {
|
||||
var page Page
|
||||
s.db.First(&page)
|
||||
return page
|
||||
tx := s.db.Where("Name = ?", name).First(&page)
|
||||
if tx.Error != nil {
|
||||
return Page{}, tx.Error
|
||||
}
|
||||
fmt.Println(page)
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func (s *StoreGorm) GetAllPages() ([]Page, error) {
|
||||
|
@ -8,22 +8,85 @@ import (
|
||||
)
|
||||
|
||||
func WikiHome(c echo.Context) error {
|
||||
res, err := store.GetAllPages()
|
||||
var err error
|
||||
page, err := store.GetPage("WikiHome")
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
err := store.AddPage("WikiHome", "This is the home of the wiki")
|
||||
if err != nil {
|
||||
log.Panicln(err.Error())
|
||||
}
|
||||
}
|
||||
fmt.Printf("res: %T\n", page)
|
||||
return c.Render(http.StatusOK, "page.html", map[string]interface{}{
|
||||
"page": page,
|
||||
})
|
||||
}
|
||||
|
||||
func WikiPage(c echo.Context) error {
|
||||
page_id := c.Param("page")
|
||||
var err error
|
||||
page, err := store.GetPage(page_id)
|
||||
if err != nil {
|
||||
// If record not found, show the create
|
||||
if err.Error() == "record not found" {
|
||||
return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{
|
||||
"name": page_id,
|
||||
})
|
||||
}
|
||||
}
|
||||
fmt.Printf("res: %T\n", page)
|
||||
return c.Render(http.StatusOK, "page.html", map[string]interface{}{
|
||||
"page": page,
|
||||
})
|
||||
}
|
||||
|
||||
func WikiPageEdit(c echo.Context) error {
|
||||
page_id := c.Param("page")
|
||||
var err error
|
||||
page, err := store.GetPage(page_id)
|
||||
if err != nil {
|
||||
// If record not found, create and show the form
|
||||
if err.Error() == "record not found" {
|
||||
err := store.AddPage(page_id, "# "+page_id)
|
||||
if err != nil {
|
||||
log.Panicln(err.Error())
|
||||
}
|
||||
page, err = store.GetPage(page_id)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Printf("res: %T\n", page)
|
||||
return c.Render(http.StatusOK, "edit.html", map[string]interface{}{
|
||||
"page": page,
|
||||
})
|
||||
}
|
||||
|
||||
func WikiPagePostEdit(c echo.Context) error {
|
||||
page_id := c.Param("page")
|
||||
var err error
|
||||
page, err := store.GetPage(page_id)
|
||||
if err != nil {
|
||||
log.Panic(err.Error())
|
||||
}
|
||||
page.Body = c.FormValue("html")
|
||||
store.CreatePage(page)
|
||||
return c.Redirect(http.StatusMovedPermanently, "/"+page.Name)
|
||||
}
|
||||
|
||||
func WikiList(c echo.Context) error {
|
||||
pages, err := store.GetAllPages()
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
fmt.Printf("res: %T", res)
|
||||
return c.Render(http.StatusOK, "home.html", map[string]interface{}{
|
||||
"name": "HOME",
|
||||
"msg": "This is the home page.",
|
||||
"pages": res,
|
||||
fmt.Printf("res: %T\n", pages)
|
||||
return c.Render(http.StatusOK, "list.html", map[string]interface{}{
|
||||
"pages": pages,
|
||||
})
|
||||
}
|
||||
|
||||
func WikiAbout(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
return c.Render(http.StatusOK, "about.html", map[string]interface{}{
|
||||
"name": "ABOUT",
|
||||
"msg": "About Wikingo (id:)" + id,
|
||||
})
|
||||
return c.Render(http.StatusOK, "about.html", map[string]interface{}{})
|
||||
}
|
||||
|
@ -47,14 +47,21 @@ func Serve() {
|
||||
// Instantiate a template registry with an array of template set
|
||||
// Ref: https://gist.github.com/rand99/808e6e9702c00ce64803d94abff65678
|
||||
templates := make(map[string]*template.Template)
|
||||
templates["home.html"] = template.Must(template.ParseFiles("views/home.html", "views/base.html"))
|
||||
templates["page.html"] = template.Must(template.ParseFiles("views/page.html", "views/base.html"))
|
||||
templates["list.html"] = template.Must(template.ParseFiles("views/list.html", "views/base.html"))
|
||||
templates["edit.html"] = template.Must(template.ParseFiles("views/edit.html", "views/base.html"))
|
||||
templates["about.html"] = template.Must(template.ParseFiles("views/about.html", "views/base.html"))
|
||||
templates["notfound.html"] = template.Must(template.ParseFiles("views/notfound.html", "views/base.html"))
|
||||
e.Renderer = &TemplateRegistry{
|
||||
templates: templates,
|
||||
}
|
||||
|
||||
// Routes
|
||||
e.GET("/", WikiHome)
|
||||
e.GET("/:page", WikiPage)
|
||||
e.GET("/:page/edit", WikiPageEdit)
|
||||
e.POST("/:page/edit", WikiPagePostEdit)
|
||||
e.GET("/list", WikiList)
|
||||
e.GET("/about", WikiAbout)
|
||||
e.GET("/about/:id", WikiAbout)
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
{{define "title"}}
|
||||
Wikingo | {{index . "name"}}
|
||||
Wikingo - About
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<h1>Wikingo | {{index . "name"}}</h1>
|
||||
<h2>{{index . "msg"}}</h2>
|
||||
<h1>About Wikingo</h1>
|
||||
<h2>Personal Wiki written in Go</h2>
|
||||
<p>About Wikingo, Lorem ipsum...</p>
|
||||
{{end}}
|
||||
|
@ -1,17 +1,19 @@
|
||||
{{define "base.html"}}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{template "title" .}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="/">Home</a></li>
|
||||
<li><a href="/about">About</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
{{template "body" .}}
|
||||
</body>
|
||||
</html>
|
||||
{{ define "base.html" }}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{ template "title" . }}</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
|
||||
</head>
|
||||
<body>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="/">Home</a></li>
|
||||
<li><a href="/list">List</a></li>
|
||||
<li><a href="/about">About</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
{{ template "body" . }}
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
|
14
views/edit.html
Normal file
14
views/edit.html
Normal file
@ -0,0 +1,14 @@
|
||||
{{define "title"}}
|
||||
Wikingo - Edit {{ .page.Name }}
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<h1>{{ .page.Name }}</h1>
|
||||
<h2>Edit page: {{ .page.Name }}</h2>
|
||||
<form method="POST">
|
||||
<textarea name="html">{{ .page.Body }}</textarea>
|
||||
<hr />
|
||||
<button>Save</button>
|
||||
<a href="/{{ .page.Name }}">Cancel</a>
|
||||
</form>
|
||||
{{end}}
|
@ -1,13 +0,0 @@
|
||||
{{define "title"}}
|
||||
Wikingo | {{index . "name"}}
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<h1>Wikingo | {{index . "name"}}</h1>
|
||||
<h2>{{index . "msg"}}</h2>
|
||||
<ul>
|
||||
{{ range .pages }}
|
||||
<li>{{ .Code }}.- {{ .Price }}</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
{{end}}
|
12
views/list.html
Normal file
12
views/list.html
Normal file
@ -0,0 +1,12 @@
|
||||
{{define "title"}}
|
||||
Wikingo - List
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<h1>List</h1>
|
||||
<ul>
|
||||
{{ range .pages }}
|
||||
<li><a href="{{ .Name }}">{{ .Name }}</a>.- {{ .Body }}</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
{{end}}
|
12
views/notfound.html
Normal file
12
views/notfound.html
Normal file
@ -0,0 +1,12 @@
|
||||
{{define "title"}}
|
||||
Wikingo | {{index . "name"}}
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<h1>Page not found</h1>
|
||||
<h2>Actions:</h2>
|
||||
<ul>
|
||||
<li>Create <a href="{{ .name }}/edit">{{ .name }}</a></li>
|
||||
<li>Go to <a href="/">WikiHome</a></li>
|
||||
</ul>
|
||||
{{end}}
|
13
views/page.html
Normal file
13
views/page.html
Normal file
@ -0,0 +1,13 @@
|
||||
{{define "title"}}
|
||||
Wikingo - {{ .page.Name }}
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<h1>{{ .page.Name }}</h1>
|
||||
<h2>{{ .page.Name }}</h2>
|
||||
{{ .page.Body }}
|
||||
<hr />
|
||||
<a href="{{ .page.Name }}/edit">Edit</a>
|
||||
<p style="float: right">Last modified: {{ .page.UpdatedAt }}</p>
|
||||
<p style="float: right">*{{ .page.Version }}*</p>
|
||||
{{end}}
|
Loading…
Reference in New Issue
Block a user