func (c *appContext) postHandler(w http.ResponseWriter, r *http.Request) { params := context.Get(r, "params").(httprouter.Params) name := params.ByName("name") // Get the Post post, err := models.GetPost(c.db, name) if err != nil { fmt.Println(err) } // Get the Posts Taxonomies taxonomies, err := models.GetPostTaxonomies(c.db, post.PostId) if err != nil { fmt.Println(err) } // Loop Taxonomies and add them to the Post struct for _, t := range taxonomies { // If Category if 1 == t.Type { post.Categories = append(post.Categories, t) } // If Tag if 2 == t.Type { post.Tags = append(post.Tags, t) } } // Get the Post Comments comments, err := models.GetPostComments(c.db, post.PostId) if err != nil { fmt.Println(err) } post.Comments = comments page := Page{ Name: "Blog", Title: fmt.Sprintf("%s - Blog", post.Title), Description: fmt.Sprintf("The blog article: %s.", post.Title), UrlPath: r.URL.Path, Time: time.Now(), } app := App{ Name: c.config.AppName, Version: c.config.AppVersion, Url: c.config.AppUrl, } user := context.Get(r, "user").(models.User) data := struct { Page Page App App Post models.Post User models.User }{ page, app, post, user, } render(w, "postPage", data) }
func (c *appContext) cpPostEditHandler(w http.ResponseWriter, r *http.Request) { params := context.Get(r, "params").(httprouter.Params) id, _ := strconv.Atoi(params.ByName("id")) page := Page{ Name: "Edit Post", Title: "Edit Post", Description: "A free Dota 2 stats website.", UrlPath: r.URL.Path, } app := App{ Name: c.config.AppName, Version: c.config.AppVersion, Url: c.config.AppUrl, } user := context.Get(r, "user").(models.User) post, err := models.GetPostById(c.db, id) if err != nil { fmt.Println(err) } // Get the Posts Taxonomies ptaxs, err := models.GetPostTaxonomies(c.db, post.PostId) if err != nil { fmt.Println(err) } // Loop Taxonomies and add them to the Post struct for _, t := range ptaxs { // If Category if 1 == t.Type { post.Categories = append(post.Categories, t) } // If Tag if 2 == t.Type { post.Tags = append(post.Tags, t) } } taxonomies, err := models.GetAllTaxonomies(c.db) if err != nil { fmt.Println(err) } var categories []models.Taxonomy var tags []models.Taxonomy // Loop Taxonomies and add them to the Post struct for _, t := range taxonomies { // If Category if 1 == t.Type { categories = append(categories, t) } // If Tag if 2 == t.Type { tags = append(tags, t) } } data := struct { Page Page App App User models.User Post models.Post Categories []models.Taxonomy Tags []models.Taxonomy }{ page, app, user, post, categories, tags, } renderCp(w, "postEditPage", data) }