コード例 #1
0
ファイル: post.go プロジェクト: trigrass2/wego
func (this *NewPost) Post() error {
	if this.CheckActiveRedirect() {
		return nil
	}

	var err error
	form := post.PostForm{Locale: this.Locale}
	topicSlug := this.GetString("topic")
	if len(topicSlug) > 0 {
		topic, err := models.GetTopicBySlug(topicSlug)
		if err == nil {
			form.Category = topic.CategoryId
			form.Topic = topic.Id
			this.Data["Topic"] = topic
		} else {
			log.Error("Can not find topic by slug:", topicSlug)
		}
	} else {
		topicId, err := this.GetInt("Topic")
		if err == nil {
			topic, err := models.GetTopicById(topicId)
			if err == nil {
				form.Category = topic.CategoryId
				form.Topic = topic.Id
				this.Data["Topic"] = topic
			} else {
				log.Error("Can not find topic by id:", topicId)
			}
		} else {
			log.Error("Parse param Topic from request failed", err)
		}
	}
	if categorySlug := this.GetString("category"); categorySlug != "" {
		log.Debug("Find category slug:", categorySlug)
		category, err := models.GetCategoryBySlug(categorySlug)
		if err != nil {
			log.Error("Get category error", err)
		}
		this.Data["Category"] = &category
	}
	err = models.FindTopics(&form.Topics)
	if err != nil {
		return err
	}
	if !this.ValidFormSets(&form) {
		this.Redirect("/new")
		return nil
	}

	var post models.Post
	if err := form.SavePost(&post, &this.User); err == nil {
		this.JsStorage("deleteKey", "post/new")
		this.Redirect(post.Link())
		return nil
	}
	return this.Render("post/new.html", this.Data)
}
コード例 #2
0
ファイル: post.go プロジェクト: trigrass2/wego
func (this *NewPost) Get() error {
	if this.CheckActiveRedirect() {
		return nil
	}

	form := post.PostForm{Locale: this.Locale}
	topicSlug := this.GetString("topic")
	if len(topicSlug) > 0 {
		topic, err := models.GetTopicBySlug(topicSlug)
		if err != nil {
			this.Redirect(setting.AppUrl)
			return nil
		}

		form.Topic = topic.Id
		form.Category = topic.CategoryId

		err = models.FindTopicsByCategoryId(&form.Topics, topic.CategoryId)
		if err != nil {
			this.Redirect(setting.AppUrl)
			return nil
		}

		this.Data["Topic"] = topic
	} else {
		catSlug := this.GetString("category")
		if len(catSlug) > 0 {
			category, err := models.GetCategoryBySlug(catSlug)
			if err != nil {
				return err
			}

			form.Category = category.Id
			err = models.FindTopicsByCategoryId(&form.Topics, category.Id)
			if err != nil {
				return err
			}
			this.Data["Category"] = category
		} else {
			this.Redirect(setting.AppUrl)
			return nil
		}
	}

	this.SetFormSets(&form)
	return this.Render("post/new.html", this.Data)
}
コード例 #3
0
ファイル: post.go プロジェクト: trigrass2/wego
//Get the posts by category
func (this *Category) Get() error {
	//check category slug
	slug := this.Params().Get(":slug")
	cat, err := models.GetCategoryBySlug(slug)
	if err != nil {
		return err
	}

	//get posts by category slug, order by Created desc
	cnt, err := models.CountByExample(&models.Post{CategoryId: cat.Id})
	if err != nil {
		return err
	}

	pager := this.SetPaginator(setting.PostCountPerPage, cnt)
	posts, err := models.RecentPosts("hot", setting.PostCountPerPage, pager.Offset())
	if err != nil {
		return err
	}

	this.Data["Category"] = cat
	this.Data["Posts"] = posts

	//top nav bar data
	var cats []models.Category
	this.setCategories(&cats)
	var topics []models.Topic
	this.setTopicsOfCategory(&topics, cat)
	this.Data["CategorySlug"] = cat.Slug
	this.Data["SortSlug"] = ""
	var newBestPosts []models.Post
	this.setNewBestPostsOfCategory(&newBestPosts, cat)
	//most replys posts
	var mostReplysPosts []models.Post
	this.setMostReplysPostsOfCategory(&mostReplysPosts, cat)
	this.setSidebarBuilletinInfo()

	return this.Render("post/home.html", this.Data)
}