// 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 }
func (this *UserRouter) getFollows(user *models.User, following bool) []map[string]interface{} { var follow models.Follow if following { follow.UserId = user.Id } else { follow.FollowUserId = user.Id } nums, _ := models.Count(&follow) limit := 20 pager := this.SetPaginator(limit, nums) var follows []*models.Follow models.ORM().Limit(limit, pager.Offset()).Find(&follows, &follow) if len(follows) == 0 { return nil } ids := make([]int, 0, len(follows)) for _, follow := range follows { if following { ids = append(ids, int(follow.FollowUserId)) } else { ids = append(ids, int(follow.UserId)) } } var fids = make(map[int]bool) models.ORM().In("follow_user_id", ids).Iterate(&models.Follow{UserId: this.User.Id}, func(idx int, bean interface{}) error { tid, _ := utils.StrTo(utils.ToStr(bean.(*models.Follow).Id)).Int() if tid > 0 { fids[tid] = true } return nil }) users := make([]map[string]interface{}, 0, len(follows)) for _, follow := range follows { IsFollowed := false var u *models.User if following { u = follow.FollowUser() } else { u = follow.User() } if fids != nil { IsFollowed = fids[int(u.Id)] } users = append(users, map[string]interface{}{ "User": u, "IsFollowed": IsFollowed, }) } return users }
func (this *Users) Post() { result := map[string]interface{}{ "success": false, } defer func() { this.Data["json"] = result this.ServeJson(this.Data) }() if !this.IsAjax() { return } action := this.GetString("action") if this.IsLogin { switch action { case "get-follows": var data = make([][]interface{}, 0) models.ORM().Iterate(&models.Follow{UserId: this.User.Id}, func(idx int, bean interface{}) error { followUser := bean.(*models.Follow).FollowUser() if followUser != nil { data = append(data, []interface{}{followUser.NickName, followUser.UserName}) } return nil }) result["success"] = true result["data"] = data case "follow", "unfollow": id, err := utils.StrTo(this.GetString("user")).Int() if err == nil && id != int(this.User.Id) { fuser := models.User{Id: int64(id)} if action == "follow" { auth.UserFollow(&this.User, &fuser) } else { auth.UserUnFollow(&this.User, &fuser) } result["success"] = true } } } }
func (m *Image) DecodeToken(token string) error { number := utils.NumberDecode(token, setting.ImageLinkAlphabets) if len(number) < 9 { return fmt.Errorf("token `%s` too short <- `%s`", token, number) } if t, err := utils.DateParse(number[:8], "ymds"); err != nil { return fmt.Errorf("token `%s` date parse error <- `%s`", token, number) } else { m.Created = t } var err error m.Id, err = utils.StrTo(number[8:]).Int64() if err != nil { return fmt.Errorf("token `%s` id parse error <- `%s`", token, err) } return nil }