diff --git a/model/page.go b/model/page.go index 8a5f716..b635f65 100644 --- a/model/page.go +++ b/model/page.go @@ -6,6 +6,7 @@ import ( type Page struct { gorm.Model - Code string - Price uint + Name string + Body string + Version uint } diff --git a/model/store.go b/model/store.go index 1f7984d..14347ae 100644 --- a/model/store.go +++ b/model/store.go @@ -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) } diff --git a/model/store_gorm.go b/model/store_gorm.go index 4303785..c780ae0 100644 --- a/model/store_gorm.go +++ b/model/store_gorm.go @@ -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) { diff --git a/server/app.go b/server/app.go index 2d3f561..e1245f9 100644 --- a/server/app.go +++ b/server/app.go @@ -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{}{}) } diff --git a/server/server.go b/server/server.go index ef8e2ba..1192ba5 100644 --- a/server/server.go +++ b/server/server.go @@ -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) diff --git a/views/about.html b/views/about.html index 238c552..78d96b9 100644 --- a/views/about.html +++ b/views/about.html @@ -1,9 +1,9 @@ {{define "title"}} - Wikingo | {{index . "name"}} + Wikingo - About {{end}} {{define "body"}} -
About Wikingo, Lorem ipsum...
{{end}} diff --git a/views/base.html b/views/base.html index 95f378f..30373fc 100644 --- a/views/base.html +++ b/views/base.html @@ -1,17 +1,19 @@ -{{define "base.html"}} - - - -Last modified: {{ .page.UpdatedAt }}
+*{{ .page.Version }}*
+{{end}}