func UpdateArticle(article *entities.Article) error { var err error db := db.NewDB() db.Begin() sql := "update article set title = ? ,tags=?,categories=?, content=?, updated_at=now() where user_id = ? and id = ? " _, err = db.Raw(sql, []interface{}{article.Title, article.Tags, article.Categories, article.Content, article.UserId, article.Id}).Exec() if nil == err { var categories []entities.Category if len(article.Categories) > 0 { categoryNames := strings.Split(article.Categories, ",") categories = entities.NewCategories(article.UserId, categoryNames) } BatchSaveOrUpdateCategory(db, categories) } if nil == err { db.Commit() } else { db.Rollback() } return err }
func SaveArticle(article *entities.Article) error { var err error db := db.NewDB() db.Begin() bBuffer := bytes.NewBufferString("insert into article (user_id,title, tags,categories, content, created_at) ") bBuffer.WriteString("values(?,?,?,?,?,now())") _, err = db.Raw(bBuffer.String(), []interface{}{article.UserId, article.Title, article.Tags, article.Categories, article.Content}).Exec() if nil == err { var categories []entities.Category if len(article.Categories) > 0 { categoryNames := strings.Split(article.Categories, ",") categories = entities.NewCategories(article.UserId, categoryNames) } BatchSaveOrUpdateCategory(db, categories) } if nil == err { db.Commit() } else { db.Rollback() } return err }
func TestSaveCategories(t *testing.T) { categories := entities.NewCategories(2, []string{"go1", "go2"}) services.BatchSaveOrUpdateCategory(db.NewDB(), categories) }