func (p *socialAuther) LoginUser(ctx *tango.Context, session *session.Session, uid int) (string, error) { user := models.User{} if err := models.GetById(int64(uid), &user); err == nil { auth.LoginUser(&user, ctx, session, true) } return auth.GetLoginRedirect(ctx), nil }
func UserFollow(user *models.User, theUser *models.User) { if err := models.GetById(theUser.Id, theUser); err != nil { var mutual bool tFollow := models.Follow{UserId: theUser.Id, FollowUserId: user.Id} if err := models.GetByExample(&tFollow); err == nil { mutual = true } follow := models.Follow{UserId: user.Id, FollowUserId: theUser.Id, Mutual: mutual} if err := models.Insert(&follow); err == nil && mutual { tFollow.Mutual = mutual models.UpdateById(tFollow.Id, &tFollow, "mutual") } if nums, err := models.Count(&models.Follow{UserId: user.Id}); err == nil { user.Following = int(nums) models.UpdateById(user.Id, user, "following") } if nums, err := models.Count(&models.Follow{FollowUserId: theUser.Id}); err == nil { theUser.Followers = int(nums) models.UpdateById(theUser.Id, theUser, "followers") } } }
// query object and set to template func (this *ModelAdminRouter) QueryObject() bool { id, _ := utils.StrTo(this.Params().Get(":id")).Int() if id <= 0 { this.NotFound() return false } var app ModelFinder if a, ok := this.Ctx.Action().(ModelFinder); ok { app = a } else { panic("ModelAdmin AppController need implement ModelFinder") } object := app.Object() // query object if err := models.GetById(int64(id), object); err != nil { this.NotFound() if err != models.ErrNotExist { log.Error("SetObject: ", err) } return false } else { this.Data["Object"] = object } return true }
//Topic Home Page func (this *Topic) Get() error { //check topic slug slug := this.Params().Get(":slug") topic, err := models.GetTopicBySlug(slug) if err != nil { return err } //get topic category var category models.Category err = models.GetById(topic.CategoryId, &category) if err != nil { return err } //get posts by topic cnt, err := models.CountByExample(&models.Post{TopicId: topic.Id}) if err != nil { return err } pager := this.SetPaginator(setting.PostCountPerPage, cnt) posts, err := models.FindPosts(setting.PostCountPerPage, pager.Offset()) if err != nil { return err } this.Data["Posts"] = posts this.Data["Topic"] = &topic this.Data["Category"] = &category //check whether added it into favorite list var hasFavorite bool if this.IsLogin { hasFavorite, _ = models.HasUserFollowTopic(int64(this.User.Id), topic.Id) } this.Data["HasFavorite"] = hasFavorite //new best post var newBestPosts []models.Post this.setNewBestPostsOfTopic(&newBestPosts, topic) //most replys posts var mostReplysPosts []models.Post this.setMostReplysPostsOfTopic(&mostReplysPosts, topic) this.setSidebarBuilletinInfo() return this.Render("post/topic.html", this.Data) }
func (this *Post) Post() { if this.CheckActiveRedirect() { return } if !this.IsAjax() { return } result := map[string]interface{}{ "success": false, } action := this.GetString("action") switch action { case "toggle-best": if this.User.IsAdmin { if postId, err := this.GetInt("post"); err == nil { //set post best var post models.Post if err := models.GetById(postId, &post); err == nil { post.IsBest = !post.IsBest if models.UpdateById(post.Id, post, "is_best") == nil { result["success"] = true } } } else { this.Logger.Error("post value is not int:", this.GetString("post")) } } case "toggle-fav": if postId, err := this.GetInt("post"); err == nil { var post models.Post if err := models.GetById(postId, &post); err == nil { var favoritePost = models.FavoritePost{ PostId: post.Id, UserId: this.User.Id, } if err := models.GetByExample(&favoritePost); err == nil { //toogle IsFav favoritePost.IsFav = !favoritePost.IsFav if models.UpdateById(favoritePost.Id, favoritePost, "is_fav") == nil { //update user fav post count if favoritePost.IsFav { this.User.FavPosts += 1 } else { this.User.FavPosts -= 1 } if models.UpdateById(this.User.Id, this.User, "fav_posts") == nil { result["success"] = true } } } else if err == models.ErrNotExist { favoritePost = models.FavoritePost{ UserId: this.User.Id, PostId: post.Id, IsFav: true, } if models.Insert(favoritePost) == nil { //update user fav post count this.User.FavPosts += 1 if models.UpdateById(this.User.Id, this.User, "fav_posts") == nil { result["success"] = true } } } else { this.Logger.Error("Get favorite post err:", err) } } } } this.Data["json"] = result this.ServeJson(this.Data) }