PageRenderTime 32ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/index.go

http://github.com/TheOnly92/bloody.go
Go | 119 lines | 95 code | 22 blank | 2 comment | 15 complexity | 2ff126a072a74d283baca85f4686e84d MD5 | raw file
  1. package main
  2. import (
  3. "mustache"
  4. "time"
  5. "strconv"
  6. "web"
  7. "session"
  8. )
  9. type Index struct {}
  10. func (c *Index) Index() string {
  11. p := PostModelInit()
  12. results := p.FrontPage()
  13. output := mustache.RenderFile("templates/post.mustache", map[string][]map[string]string {"posts":results})
  14. return render(output, "")
  15. }
  16. func (c *Index) RSS(ctx *web.Context) string {
  17. p := PostModelInit()
  18. results := p.RSS()
  19. ctx.ContentType("xml")
  20. return mustache.RenderFile("templates/rss.mustache", map[string][]map[string]string {"posts":results})
  21. }
  22. func (c *Index) NewComment(ctx *web.Context, postId string) {
  23. p := PostModelInit()
  24. p.InsertComment(postId,ctx.Params["comment"],ctx.Params["author"])
  25. ctx.Redirect(302,"/post/"+postId)
  26. }
  27. func (c *Index) ReadPost(ctx *web.Context, postId string) string {
  28. p := PostModelInit()
  29. result := p.RenderPost(postId)
  30. viewVars := make(map[string]interface{})
  31. viewVars["Title"] = result.Title
  32. viewVars["Content"] = result.Content
  33. viewVars["Date"] = time.Unix(result.Created, 0).Format(blogConfig.Get("dateFormat"))
  34. viewVars["Id"] = objectIdHex(result.Id.String())
  35. // To be used within the {{Comments}} blog
  36. viewVars["PostId"] = objectIdHex(result.Id.String())
  37. if result.Status == 0 {
  38. sessionH := session.Start(ctx, h)
  39. defer sessionH.Save()
  40. if sessionH.Data["logged"] == nil {
  41. ctx.Redirect(302, "/")
  42. return ""
  43. }
  44. }
  45. if blogConfig.Get("enableComment") != "" {
  46. viewVars["EnableComment"] = true
  47. } else {
  48. viewVars["EnableComment"] = false
  49. }
  50. // Render comments
  51. comments := make([]map[string]string,0)
  52. for i, v := range result.Comments {
  53. comments = append(comments, map[string]string{
  54. "Number": strconv.Itoa(i+1),
  55. "Date": time.Unix(v.Created, 0).Format(blogConfig.Get("dateFormat")),
  56. "Id": v.Id[0:9],
  57. "RealId": v.Id,
  58. "Content": v.Content,
  59. "Author": v.Author})
  60. }
  61. viewVars["Comments"] = comments
  62. if next, exists := p.GetNextId(objectIdHex(result.Id.String())); exists {
  63. viewVars["Next"] = next
  64. }
  65. if last, exists := p.GetLastId(objectIdHex(result.Id.String())); exists {
  66. viewVars["Last"] = last
  67. }
  68. sessionH := session.Start(ctx, h)
  69. defer sessionH.Save()
  70. viewVars["Admin"] = false
  71. if sessionH.Data["logged"] != nil {
  72. viewVars["Admin"] = true
  73. }
  74. output := mustache.RenderFile("templates/view-post.mustache", viewVars)
  75. return render(output, result.Title)
  76. }
  77. func (c *Index) ReadPage(pageSlug string) string {
  78. p := PageModelInit()
  79. result := p.GetBySlug(pageSlug)
  80. viewVars := make(map[string]string)
  81. viewVars["Title"] = result.Title
  82. viewVars["Content"] = result.Content
  83. viewVars["Date"] = time.Unix(result.Created, 0).Format(blogConfig.Get("dateFormat"))
  84. output := mustache.RenderFile("templates/view-page.mustache", viewVars)
  85. return render(output, result.Title)
  86. }
  87. func (c *Index) ListPosts(ctx *web.Context) string {
  88. page := 1
  89. if temp, exists := ctx.Params["page"]; exists {
  90. page, _ = strconv.Atoi(temp)
  91. }
  92. p := PostModelInit()
  93. results := p.PostListing(page)
  94. totPages := p.TotalPages()
  95. output := mustache.RenderFile("templates/post-listing.mustache", map[string]interface{} {"Posts": results, "Pagination": pagination(page, totPages)})
  96. return render(output, "Post Listing")
  97. }