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

View File

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

View File

@ -45,7 +45,7 @@ func (s *StoreGorm) CreatePage(page Page) error {
newversion.Name = page.Name newversion.Name = page.Name
newversion.Body = page.Body newversion.Body = page.Body
newversion.Version = page.Version + 1 newversion.Version = page.Version + 1
tx := s.db.Save(newversion) tx := s.db.Create(&newversion)
return tx.Error return tx.Error
} }
@ -59,6 +59,16 @@ func (s *StoreGorm) GetPage(name string) (Page, error) {
return page, nil 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) { func (s *StoreGorm) GetAllPages() ([]Page, error) {
var pages []Page var pages []Page
tx := s.db.Find(&pages) tx := s.db.Find(&pages)

View File

@ -6,6 +6,8 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/oscarmlage/wikingo/model"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2" "github.com/russross/blackfriday/v2"
@ -16,9 +18,24 @@ func WikiHome(c echo.Context) error {
} }
func WikiPage(c echo.Context) error { func WikiPage(c echo.Context) error {
page_id := c.Param("page") var page model.Page
var err error var err error
page, err := store.GetPage(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 err != nil {
// If record not found, show the create // If record not found, show the create
if err.Error() == "record not found" { if err.Error() == "record not found" {
@ -27,6 +44,7 @@ func WikiPage(c echo.Context) error {
}) })
} }
} }
}
fmt.Printf("res: %T\n", page) fmt.Printf("res: %T\n", page)
body := strings.ReplaceAll(page.Body, "\r\n", "\n") body := strings.ReplaceAll(page.Body, "\r\n", "\n")
unsafe := blackfriday.Run([]byte(body)) unsafe := blackfriday.Run([]byte(body))
@ -38,9 +56,17 @@ func WikiPage(c echo.Context) error {
} }
func WikiPageEdit(c echo.Context) error { func WikiPageEdit(c echo.Context) error {
page_id := c.Param("page") var page model.Page
var err error 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 err != nil {
// If record not found, create and show the form // If record not found, create and show the form
if err.Error() == "record not found" { if err.Error() == "record not found" {

View File

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

View File

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

View File

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

View File

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