func ListTopicsOfCat(topics *[]models.Topic, cat *models.Category) (int64, error) { var list orm.ParamsList var where string if cat != nil { where = " WHERE category_id = ?" } sql := fmt.Sprintf(`SELECT topic_id FROM post%s GROUP BY topic_id ORDER BY COUNT(topic_id) DESC LIMIT 8`, where) rs := orm.NewOrm().Raw(sql) if cat != nil { rs = rs.SetArgs(cat.Id) } cnt, err := rs.ValuesFlat(&list) if err != nil { beego.Error("models.ListTopicsOfCat ", err) return 0, err } if cnt > 0 { nums, err := models.Topics().Filter("Id__in", list).All(topics) if err != nil { beego.Error("models.ListTopicsOfCat ", err) return 0, err } return nums, err } return 0, nil }
// view for list model data func (this *TopicAdminRouter) List() { var topics []models.Topic qs := models.Topics().RelatedSel() if err := this.SetObjects(qs, &topics); err != nil { this.Data["Error"] = err beego.Error(err) } }
func (form *TopicAdminForm) Valid(v *validation.Validation) { qs := models.Topics() if models.CheckIsExist(qs, "Name", form.Name, form.Id) { v.SetError("Name", "admin.field_need_unique") } if models.CheckIsExist(qs, "Slug", form.Slug, form.Id) { v.SetError("Slug", "admin.field_need_unique") } }
// view for delete object func (this *CategoryAdminRouter) Delete() { if this.FormOnceNotMatch() { return } // check whether there are topics under the category qs := models.Topics().Filter("Category__Id", this.object.Id) cnt, _ := qs.Count() if cnt > 0 { this.FlashRedirect("/admin/category", 302, "DeleteNotAllowed") return } else { // delete object if err := this.object.Delete(); err == nil { this.FlashRedirect("/admin/category", 302, "DeleteSuccess") return } else { beego.Error(err) this.Data["Error"] = err } } }
// Get implemented Get method for HomeRouter. func (this *PostListRouter) Navs() { slug := this.GetString(":slug") switch slug { case "favs", "follow": if this.CheckLoginRedirect() { return } } this.Data["CategorySlug"] = slug this.TplName = fmt.Sprintf("post/navs/%s.html", slug) pers := 25 var posts []models.Post switch slug { case "recent": qs := models.Posts() qs = this.postsFilter(qs) cnt, _ := models.CountObjects(qs) pager := this.SetPaginator(pers, cnt) qs = qs.OrderBy("-Updated").Limit(pers, pager.Offset()).RelatedSel() models.ListObjects(qs, &posts) var cats []models.Category this.setCategories(&cats) case "best": qs := models.Posts().Filter("IsBest", true) qs = this.postsFilter(qs) cnt, _ := models.CountObjects(qs) pager := this.SetPaginator(pers, cnt) qs = qs.OrderBy("-Created").Limit(pers, pager.Offset()).RelatedSel() models.ListObjects(qs, &posts) var cats []models.Category this.setCategories(&cats) case "cold": qs := models.Posts().Filter("Replys", 0) qs = this.postsFilter(qs) cnt, _ := models.CountObjects(qs) pager := this.SetPaginator(pers, cnt) qs = qs.OrderBy("-Created").Limit(pers, pager.Offset()).RelatedSel() models.ListObjects(qs, &posts) var cats []models.Category this.setCategories(&cats) case "favs": var topicIds orm.ParamsList nums, _ := models.FollowTopics().Filter("User", &this.User.Id).OrderBy("-Created").ValuesFlat(&topicIds, "Topic") if nums > 0 { qs := models.Posts().Filter("Topic__in", topicIds) qs = this.postsFilter(qs) cnt, _ := models.CountObjects(qs) pager := this.SetPaginator(pers, cnt) qs = qs.OrderBy("-Created").Limit(pers, pager.Offset()).RelatedSel() models.ListObjects(qs, &posts) var topics []models.Topic nums, _ = models.Topics().Filter("Id__in", topicIds).Limit(8).All(&topics) this.Data["Topics"] = topics this.Data["TopicsMore"] = nums >= 8 } case "follow": var userIds orm.ParamsList nums, _ := this.User.FollowingUsers().OrderBy("-Created").ValuesFlat(&userIds, "FollowUser") if nums > 0 { qs := models.Posts().Filter("User__in", userIds) qs = this.postsFilter(qs) cnt, _ := models.CountObjects(qs) pager := this.SetPaginator(pers, cnt) qs = qs.OrderBy("-Created").Limit(pers, pager.Offset()).RelatedSel() models.ListObjects(qs, &posts) } } this.Data["Posts"] = posts }
func ListTopicsOfCategory(topics *[]models.Topic, cat *models.Category) (int64, error) { return models.Topics().Filter("Category__id", cat.Id).OrderBy("-order").All(topics) }
func ListTopics(topics *[]models.Topic) (int64, error) { return models.Topics().OrderBy("-Followers").All(topics) }
func (this *TopicAdminRouter) ObjectQs() orm.QuerySeter { return models.Topics().RelatedSel() }