Add: Page versioning "MVP"

This commit is contained in:
Óscar M. Lage 2022-03-28 17:14:01 +02:00
parent 00a49ae585
commit dba37137ff
8 changed files with 55 additions and 14 deletions

View File

@ -8,6 +8,7 @@
- [ ] Makefile helper
- [ ] Reload if files changes [idea](https://medium.com/@olebedev/live-code-reloading-for-golang-web-projects-in-19-lines-8b2e8777b1ea#.gok9azrg4)
- [ ] CI (gofmt, golint, more)?
- [x] Page versioning in a really basic way
## Bootstrap
> Basic elements

View File

@ -6,5 +6,6 @@ type Store interface {
AddPage(string, string) error
CreatePage(Page) error
GetPage(string) (Page, error)
GetPageVersion(string, string) (Page, error)
GetAllPages() ([]Page, error)
}

View File

@ -45,7 +45,7 @@ func (s *StoreGorm) CreatePage(page Page) error {
newversion.Name = page.Name
newversion.Body = page.Body
newversion.Version = page.Version + 1
tx := s.db.Save(newversion)
tx := s.db.Create(&newversion)
return tx.Error
}
@ -59,6 +59,16 @@ func (s *StoreGorm) GetPage(name string) (Page, error) {
return page, nil
}
func (s *StoreGorm) GetPageVersion(name string, version string) (Page, error) {
var page Page
tx := s.db.Where("Name = ?", name).Where("Version = ?", version).First(&page)
if tx.Error != nil {
return Page{}, tx.Error
}
fmt.Println(page)
return page, nil
}
func (s *StoreGorm) GetAllPages() ([]Page, error) {
var pages []Page
tx := s.db.Find(&pages)

View File

@ -6,6 +6,8 @@ import (
"net/http"
"strings"
"github.com/oscarmlage/wikingo/model"
"github.com/labstack/echo/v4"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
@ -16,15 +18,31 @@ func WikiHome(c echo.Context) error {
}
func WikiPage(c echo.Context) error {
page_id := c.Param("page")
var page model.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,
})
page_id := c.Param("page")
version := c.Param("version")
if version != "" {
fmt.Printf("version: %s", version)
page, err = store.GetPageVersion(page_id, version)
if err != nil {
// If record not found, show 404
if err.Error() == "record not found" {
return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{
"name": page_id,
})
}
}
} else {
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)
@ -38,9 +56,17 @@ func WikiPage(c echo.Context) error {
}
func WikiPageEdit(c echo.Context) error {
page_id := c.Param("page")
var page model.Page
var err error
page, err := store.GetPage(page_id)
page_id := c.Param("page")
version := c.Param("version")
if version != "" {
fmt.Printf("version; %s\n", version)
page, err = store.GetPageVersion(page_id, version)
} else {
fmt.Println("No version")
page, err = store.GetPage(page_id)
}
if err != nil {
// If record not found, create and show the form
if err.Error() == "record not found" {

View File

@ -64,8 +64,11 @@ func Serve() {
// Routes
e.GET("/", WikiHome)
e.GET("/:page", WikiPage)
e.GET("/:page/:version", WikiPage)
e.GET("/:page/edit", WikiPageEdit)
e.GET("/:page/:version/edit", WikiPageEdit)
e.POST("/:page/edit", WikiPagePostEdit)
e.POST("/:page/:version/edit", WikiPagePostEdit)
e.GET("/list", WikiList)
e.GET("/about", WikiAbout)
e.GET("/about/:id", WikiAbout)

View File

@ -9,6 +9,6 @@
<textarea name="html">{{ .page.Body }}</textarea>
<hr />
<button>Save</button>
<a href="/{{ .page.Name }}">Cancel</a>
<a href="/{{ .page.Name }}/{{ .page.Version }}">Cancel</a>
</form>
{{end}}

View File

@ -6,7 +6,7 @@
<h1>List</h1>
<ul>
{{ range .pages }}
<li><a href="{{ .Name }}">{{ .Name }}, v{{ .Version }}</a>.- {{ .Body }}</li>
<li><a href="{{ .Name }}/{{ .Version }}">{{ .Name }}, v{{ .Version }}</a>.- {{ .Body }}</li>
{{ end }}
</ul>
{{end}}

View File

@ -6,7 +6,7 @@
<h1>{{ .page.Name }}</h1>
{{ .html | safeHTML }}
<hr />
<a href="{{ .page.Name }}/edit">Edit</a>
<a href="/{{ .page.Name }}/{{ .page.Version }}/edit">Edit</a>
<p style="float: right">Last modified: {{ .page.UpdatedAt }}</p>
<p style="float: right">*{{ .page.Version }}*</p>
{{end}}