PageRenderTime 36ms CodeModel.GetById 12ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/index.go

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