public static ArticleDTO SaveArticle(ArticleDTO a) { GSMEF.Article EA = Article.ConvertFromDTO(a); using (var ctx = new GSMDBEntities()) { if (EA.Id > 0) { ctx.Entry(EA).State = System.Data.Entity.EntityState.Modified; foreach (var child in EA.Ventes) { ctx.Entry(child).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); } } else { EA = Article.ConvertFromDTO(a); ctx.Articles.Add(EA); a.Id = EA.Id; //because of DTOs we need to firm upthe FKs foreach (var child in a.VentesDto) { child.ArticleId = a.Id; } } } return a; }
public void TestFetchArticle() { using (var ctx = new GSMDBEntities()) { Article art = ctx.Articles.Where(a => a.Désignation.Equals("05 55 26 33 20")).FirstOrDefault(); Assert.IsTrue(art != null); } }
public static ArticleDTO GetArticle(int id, bool deepLoad) { ArticleDTO a = new ArticleDTO(); using (GSMDBEntities ctx = new GSMDBEntities()) { var result = ctx.Articles.Where(x => x.Id.Equals(id)).SingleOrDefault(); a = Article.ConvertToDTO(result, deepLoad); } return a; }
public static bool DeleteVente(int articleID, int venteID) { using (var ctx = new GSMDBEntities()) { var venteToRemove = ctx.Ventes.Where(v => v.Id.Equals(venteID) && v.Article.Id.Equals(articleID)).SingleOrDefault(); ctx.Ventes.Remove(venteToRemove); return (ctx.SaveChanges() > 0); } }
public void TestAjouterArticle() { using (var ctx = new GSMDBEntities()) { Article art = new Article(); art.Désignation = "05 55 26 33 20"; art.Type = "Puce Oooredoo"; ctx.Articles.Add(art); int affectedRecords = ctx.SaveChanges(); Assert.IsTrue(affectedRecords > 0); } }
public void TestAddVente() { using (var ctx = new GSMDBEntities()) { var art = ctx.Articles.Where(a => a.Désignation.Equals("05 55 26 33 20")).SingleOrDefault(); Vente v = new Vente() { ArticleId = art.Id, DateVente = DateTime.Now.AddDays(-1), MontantVente = 10000 }; art.Ventes.Add(v); int count = ctx.SaveChanges(); Assert.IsTrue(count != 0 && v.Id != 0); } }