Skip to content
This repository has been archived by the owner on Feb 7, 2020. It is now read-only.

Commit

Permalink
Using more OO friendly method.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOnly92 committed Jun 20, 2011
1 parent 17134bd commit c0add0e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
46 changes: 24 additions & 22 deletions bloody.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,29 @@ func main() {
h.SetSession(mSession)
blogConfig = PreferenceInit()
web.Config.StaticDir = config.Get("staticdir")
web.Get("/", index)
web.Get("/post/list", listPosts)
web.Get("/post/([A-Za-z0-9]+)", readPost)
web.Get("/page/([a-z0-9\\-]+)\\.html", readPage)
web.Get("/admin", adminIndexGet)
web.Get("/admin/preferences", adminPreferencesGet)
web.Post("/admin/preferences", adminPreferencesPost)
web.Get("/admin/post/new", newPostGet)
web.Post("/admin/post/new", newPostPost)
web.Get("/admin/post/list", listPost)
web.Post("/admin/post/list", adminBulkActions)
web.Get("/admin/post/edit/(.*)", editPostGet)
web.Post("/admin/post/edit/(.*)", editPostPost)
web.Get("/admin/post/del/(.*)", delPost)
web.Get("/admin/login", adminLoginGet)
web.Post("/admin/login", adminLoginPost)
web.Get("/admin/page/new", adminNewPageGet)
web.Post("/admin/page/new", adminNewPagePost)
web.Get("/admin/page/list", adminListPagesGet)
web.Get("/admin/page/edit/(.*)", adminEditPageGet)
web.Post("/admin/page/edit/(.*)", adminEditPagePost)
web.Get("/admin/page/del/(.*)", adminDelPage)
i := &Index{}
a := &Admin{}
web.Get("/", web.MethodHandler(i, "Index"))
web.Get("/post/list", web.MethodHandler(i, "ListPosts"))
web.Get("/post/([A-Za-z0-9]+)", web.MethodHandler(i, "ReadPost"))
web.Get("/page/([a-z0-9\\-]+)\\.html", web.MethodHandler(i, "ReadPage"))
web.Get("/admin", web.MethodHandler(a, "IndexGet"))
web.Get("/admin/preferences", web.MethodHandler(a, "PreferencesGet"))
web.Post("/admin/preferences", web.MethodHandler(a, "PreferencesPost"))
web.Get("/admin/post/new", web.MethodHandler(a, "NewPostGet"))
web.Post("/admin/post/new", web.MethodHandler(a, "NewPostPost"))
web.Get("/admin/post/list", web.MethodHandler(a, "ListPost"))
web.Post("/admin/post/list", web.MethodHandler(a, "BulkActions"))
web.Get("/admin/post/edit/(.*)", web.MethodHandler(a, "EditPostGet"))
web.Post("/admin/post/edit/(.*)", web.MethodHandler(a, "EditPostPost"))
web.Get("/admin/post/del/(.*)", web.MethodHandler(a, "DelPost"))
web.Get("/admin/login", web.MethodHandler(a, "LoginGet"))
web.Post("/admin/login", web.MethodHandler(a, "LoginPost"))
web.Get("/admin/page/new", web.MethodHandler(a, "NewPageGet"))
web.Post("/admin/page/new", web.MethodHandler(a, "NewPagePost"))
web.Get("/admin/page/list", web.MethodHandler(a, "ListPagesGet"))
web.Get("/admin/page/edit/(.*)", web.MethodHandler(a, "EditPageGet"))
web.Post("/admin/page/edit/(.*)", web.MethodHandler(a, "EditPagePost"))
web.Get("/admin/page/del/(.*)", web.MethodHandler(a, "DelPage"))
web.Run(config.Get("host")+":"+config.Get("port"))
}
38 changes: 20 additions & 18 deletions controllers/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
//"reflect"
)

func adminIndexGet(ctx *web.Context) string {
type Admin struct {}

func (c *Admin) IndexGet(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] != nil {
Expand All @@ -24,7 +26,7 @@ func adminIndexGet(ctx *web.Context) string {
return ""
}

func adminLoginGet(ctx *web.Context) string {
func (c *Admin) LoginGet(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] != nil {
Expand All @@ -35,7 +37,7 @@ func adminLoginGet(ctx *web.Context) string {
return render(output, "Login")
}

func adminLoginPost(ctx *web.Context) {
func (c *Admin) LoginPost(ctx *web.Context) {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] != nil {
Expand All @@ -51,7 +53,7 @@ func adminLoginPost(ctx *web.Context) {
ctx.Redirect(302, "/admin/post/list")
}

func adminPreferencesGet(ctx *web.Context) string {
func (c *Admin) PreferencesGet(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -63,7 +65,7 @@ func adminPreferencesGet(ctx *web.Context) string {
return render(output, "Blog Preferernces")
}

func adminPreferencesPost(ctx *web.Context) {
func (c *Admin) PreferencesPost(ctx *web.Context) {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -76,7 +78,7 @@ func adminPreferencesPost(ctx *web.Context) {
ctx.Redirect(302, "/admin/preferences")
}

func newPostGet(ctx *web.Context) string {
func (c *Admin) NewPostGet(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -87,7 +89,7 @@ func newPostGet(ctx *web.Context) string {
return render(output, "New Post")
}

func newPostPost(ctx *web.Context) {
func (c *Admin) NewPostPost(ctx *web.Context) {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -99,7 +101,7 @@ func newPostPost(ctx *web.Context) {
ctx.Redirect(302, "/admin/post/list")
}

func listPost(ctx *web.Context) string {
func (c *Admin) ListPost(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -117,7 +119,7 @@ func listPost(ctx *web.Context) string {
return render(output, "List Posts")
}

func editPostGet(ctx *web.Context, postId string) string {
func (c *Admin) EditPostGet(ctx *web.Context, postId string) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -138,7 +140,7 @@ func editPostGet(ctx *web.Context, postId string) string {
return render(output, "Edit Post")
}

func editPostPost(ctx *web.Context, postId string) {
func (c *Admin) EditPostPost(ctx *web.Context, postId string) {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -151,7 +153,7 @@ func editPostPost(ctx *web.Context, postId string) {
ctx.Redirect(302, "/admin/post/list")
}

func delPost(ctx *web.Context, postId string) {
func (c *Admin) DelPost(ctx *web.Context, postId string) {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -164,7 +166,7 @@ func delPost(ctx *web.Context, postId string) {
ctx.Redirect(302, "/admin/post/list")
}

func adminBulkActions(ctx *web.Context) {
func (c *Admin) BulkActions(ctx *web.Context) {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -182,7 +184,7 @@ func adminBulkActions(ctx *web.Context) {
ctx.Redirect(302, "/admin/post/list")
}

func adminNewPageGet(ctx *web.Context) string {
func (c *Admin) NewPageGet(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -193,7 +195,7 @@ func adminNewPageGet(ctx *web.Context) string {
return render(output, "New Page")
}

func adminNewPagePost(ctx *web.Context) {
func (c *Admin) NewPagePost(ctx *web.Context) {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -205,7 +207,7 @@ func adminNewPagePost(ctx *web.Context) {
ctx.Redirect(302, "/admin/page/list")
}

func adminListPagesGet(ctx *web.Context) string {
func (c *Admin) ListPagesGet(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -219,7 +221,7 @@ func adminListPagesGet(ctx *web.Context) string {
return render(output, "List Pages")
}

func adminEditPageGet(ctx *web.Context, id string) string {
func (c *Admin) EditPageGet(ctx *web.Context, id string) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -233,7 +235,7 @@ func adminEditPageGet(ctx *web.Context, id string) string {
return render(output, "Edit Post")
}

func adminEditPagePost(ctx *web.Context, id string) {
func (c *Admin) EditPagePost(ctx *web.Context, id string) {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand All @@ -246,7 +248,7 @@ func adminEditPagePost(ctx *web.Context, id string) {
ctx.Redirect(302, "/admin/page/list")
}

func adminDelPage(ctx *web.Context, id string) {
func (c *Admin) DelPage(ctx *web.Context, id string) {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
Expand Down
10 changes: 6 additions & 4 deletions controllers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"./session"
)

func index() string {
type Index struct {}

func (c *Index) Index() string {
p := PostModelInit()
results := p.FrontPage()

output := mustache.RenderFile("templates/post.mustache", map[string][]map[string]string {"posts":results})
return render(output, "")
}

func readPost(ctx *web.Context, postId string) string {
func (c *Index) ReadPost(ctx *web.Context, postId string) string {
p := PostModelInit()
result := p.Get(postId)

Expand Down Expand Up @@ -47,7 +49,7 @@ func readPost(ctx *web.Context, postId string) string {
return render(output, result.Title)
}

func readPage(pageSlug string) string {
func (c *Index) ReadPage(pageSlug string) string {
p := PageModelInit()
result := p.GetBySlug(pageSlug)

Expand All @@ -60,7 +62,7 @@ func readPage(pageSlug string) string {
return render(output, result.Title)
}

func listPosts(ctx *web.Context) string {
func (c *Index) ListPosts(ctx *web.Context) string {
page := 1
if temp, exists := ctx.Params["page"]; exists {
page, _ = strconv.Atoi(temp)
Expand Down

0 comments on commit c0add0e

Please sign in to comment.