func (form *CategoryAdminForm) Valid(v *validation.Validation) { category := models.Category{Name: form.Name} if models.GetByExample(&category); category.Id != int64(form.Id) { v.SetError("Name", "admin.field_need_unique") } category = models.Category{Slug: form.Slug} if models.GetByExample(&category); category.Id != int64(form.Id) { v.SetError("Slug", "admin.field_need_unique") } }
func (form *TopicAdminForm) Valid(v *validation.Validation) { topic := models.Topic{Name: form.Name} if models.GetByExample(&topic); topic.Id != int64(form.Id) { v.SetError("Name", "admin.field_need_unique") } topic = models.Topic{Slug: form.Slug} if models.GetByExample(&topic); topic.Id != int64(form.Id) { v.SetError("Slug", "admin.field_need_unique") } }
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") } } }
// check if exist user by username or email func HasUser(user *models.User, username string) bool { var userTemp models.User if strings.IndexRune(username, '@') == -1 { userTemp.UserName = username } else { userTemp.Email = username } err := models.GetByExample(&userTemp) if err == nil { *user = userTemp return true } return false }
func QiniuImage(ctx *tango.Context) { var imageName = ctx.Params().Get(":path") var imageKey string var imageSize string if i := strings.IndexRune(imageName, '.'); i == -1 { return } else { imageSize = imageName[i+1:] if j := strings.IndexRune(imageSize, '.'); j == -1 { imageSize = "full" } else { imageSize = imageSize[:j] } imageKey = imageName[:i] } var image = models.Image{ Token: imageKey, } err := models.GetByExample(&image) if err != nil { return } var imageWidth = image.Width var imageHeight = image.Height var zoomRatio = setting.ImageSizeMiddle / imageWidth if imageWidth > setting.ImageSizeMiddle { imageWidth = setting.ImageSizeMiddle } imageHeight *= zoomRatio var imageUrl = utils.GetQiniuPublicDownloadUrl(setting.QiniuPostDomain, imageKey) var zoomImageUrl = utils.GetQiniuZoomViewUrl(imageUrl, imageWidth, imageHeight) resp, err := http.Get(zoomImageUrl) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return } ctx.ResponseWriter.Write(body) }
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) }