Esempio n. 1
0
func (c *appContext) cpPostNewHandler(w http.ResponseWriter, r *http.Request) {

	page := Page{
		Name:        "New Post",
		Title:       "New 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)

	var post models.Post

	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, "postNewPage", data)

}
Esempio n. 2
0
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)

}