Esempio n. 1
0
func TestUpdateNormal(t *testing.T) {
	BeforeEach()

	oldTitle := "New Article 2"
	newTitle := "New Article 2.1"
	pretty := utils.SanitizeTitle(newTitle)
	article := models.Article{Titre: oldTitle}
	article, _ = articleService.Add(article)

	article.Titre = newTitle
	err := articleService.Update(string(article.ID.Hex()), article)

	if err != nil {
		t.Fatalf("Mustn't return error: %s", err)
	}

	article, err = articleService.GetByPretty(pretty)

	if err != nil {
		t.Fatalf("Mustn't return error: %s", err)
	}

	if article.Titre != newTitle {
		t.Fatalf("Must update title %s to %s but %s return", oldTitle, newTitle, article.Titre)
	}

	articleService.Delete(article)
}
Esempio n. 2
0
//Update modifie un article existant
func (a *ArticleService) Update(id string, article models.Article) (err error) {
	mongo := a.MongoService.Mongo.Copy()
	defer mongo.Close()
	pretty := utils.SanitizeTitle(article.Titre)
	articleCollection := mongo.Database.C("article")
	err = articleCollection.Update(bson.M{"_id": bson.ObjectIdHex(id)}, bson.M{"$set": bson.M{"titre": article.Titre, "pretty": pretty, "image": article.Image, "vignette": article.Vignette, "texte": article.Texte, "status": article.Status, "modified": article.Modified, "tags": article.Tags, "userref": bson.M{"$ref": article.UserRef.Collection, "$id": article.UserRef.Id}}})
	return
}
Esempio n. 3
0
//Add ajoute un nouvel article
func (a *ArticleService) Add(article models.Article) (models.Article, error) {
	mongo := a.MongoService.Mongo.Copy()
	defer mongo.Close()
	id := bson.NewObjectId()
	pretty := utils.SanitizeTitle(article.Titre)
	articleCollection := mongo.Database.C("article")
	articleCollection.Insert(&models.Article{ID: id, Titre: article.Titre, Pretty: pretty, Texte: article.Texte, Tags: article.Tags, Image: article.Image, Vignette: article.Vignette, Status: article.Status, Create: article.Create, Modified: article.Create, UserRef: mgo.DBRef{Collection: "user", Id: article.UserRef.Id}})

	err := articleCollection.FindId(id).One(&article)

	if err != nil {
		return models.Article{}, err
	}

	return article, nil
}