func IncrLikeCount(articleId int64, userId int64) (int, error) { db := db.NewDB() err := db.Begin() hasLiked, err := HasLikeArticle(articleId, userId, db) if nil != err { db.Rollback() return 0, err } article, err := ArticleById(articleId) articleOwnerId := article.UserId if err != nil { db.Rollback() return 0, err } if articleOwnerId <= 0 { db.Rollback() return 0, errors.New(exception.NOT_EXIST_ARTICLE_ERROR.Error()) } var sql = "update article set like_count=like_count+? where user_id = ? and id = ? " var incrCount, valid int = 1, 1 if hasLiked { incrCount = -1 valid = 0 } _, err = db.Execute(sql, []interface{}{incrCount, articleOwnerId, articleId}) if nil == err { var articleLike = entities.ArticleLike{UserId: userId, ArticleId: articleId, Valid: valid, CreatedAt: time.Now()} err = SaveOrUpdate(articleLike, db) } if nil == err { sql = "update user set like_count=like_count+1 where id = ? " _, err = db.Execute(sql, []interface{}{articleOwnerId}) } if nil == err { err = db.Commit() } else { incrCount = 0 err = db.Rollback() } return incrCount, err }
func IncrViewCount(articleId int64, userId int64, ip string) (bool, error) { db := db.NewDB() err := db.Begin() hasViewed, err := HasViewArticle(articleId, userId, ip, db) if nil != err || hasViewed { db.Rollback() return false, err } article, err := ArticleById(articleId) articleOwnerId := article.UserId if err != nil { db.Rollback() return false, err } if articleOwnerId <= 0 { db.Rollback() return false, errors.New(exception.NOT_EXIST_ARTICLE_ERROR.Error()) } var sql = "update article set view_count=view_count+1 where user_id = ? and id = ? " _, err = db.Execute(sql, []interface{}{articleOwnerId, articleId}) if nil == err { var articleView = entities.ArticleView{UserId: userId, ArticleId: articleId, Ip: ip, CreatedAt: time.Now()} err = SaveArticleView(articleView, db) } if nil == err { sql = "update user set view_count=view_count+1 where id = ? " _, err = db.Execute(sql, []interface{}{articleOwnerId}) } if nil == err { err = db.Commit() } else { err = db.Rollback() } return nil == err, err }
func SaveOrUpdateOpenUser(openUser *entities.OpenUser) error { sql := bytes.NewBufferString("insert ignore into open_user(open_id,user_id,type,nick,head,sex,age,province,city,created_at) ") sql.WriteString("values(?,?,?,?,?,?,?,?,?,now()) ") sql.WriteString("on duplicate key update nick =?,head=?,sex=?,province=?,city=?") db := db.NewDB() params := []interface{}{openUser.OpenId, openUser.UserId, openUser.Type, openUser.Nick} params = append(params, openUser.Head, openUser.Sex, openUser.Age, openUser.Province, openUser.City) params = append(params, openUser.Nick, openUser.Head, openUser.Sex, openUser.Province, openUser.City) _, err := db.Execute(sql.String(), params) return err }
func TestTransaction(t *testing.T) { db := db.NewDB() err := db.Begin() var sql = "update article set view_count=view_count+1 where user_id = ? and id = ? " db.Execute(sql, []interface{}{1, 35120}) if err == nil { db.Commit() } else { db.Rollback() } }
func BindUserIdToOpenUser(openId string, openType int, userId int64) (int64, error) { sql := "update open_user set user_id = ? where open_id = ? and type = ? " db := db.NewDB() result, err := db.Execute(sql, []interface{}{userId, openId, openType}) return result, err }