public int Save(Objects.Company company) { if (company.CompanyId == -1) context.Companies.Add(company.ToEntity()); else { Entity.Company cc = context.Companies.Find(company.CompanyId); if (cc != null) { cc.CompanyDescription = company.CompanyDescription; cc.CompanyIsActive = company.CompanyIsActive; cc.CompanyLogo = company.CompanyLogo; cc.CompanyName = company.CompanyName; } } return context.SaveChanges(); }
public int Save(Objects.Meta meta) { if (meta.MetaId == -1) { Entity.Meta entity = meta.ToEntity(); context.Metas.Add(entity); if (context.SaveChanges() > 0) { foreach (Objects.MetaOption o in meta.Options) entity.MetaOptions.Add(o.ToEntity()); return context.SaveChanges(); } return -1; } else { Entity.Meta entity = context.Metas.Find(meta.MetaId); if (entity != null) { entity.MetaNameEn = meta.MetaNameEn; entity.MetaNameEs = meta.MetaNameEs; context.SaveChanges(); StringBuilder sb = new StringBuilder(); sb.Append("<root>"); foreach (var op in meta.Options) { op.MetaId = entity.MetaId; sb.Append(op.Xml); } sb.Append("</root>"); context.Database.ExecuteSqlCommand("EXEC dbo.usp_upd_MetaOptions @x", new System.Data.SqlClient.SqlParameter("@x", sb.ToString())); return 1; } return -1; } }
public List<string> Save(Objects.Product product, List<Objects.ProductImage> images, List<Objects.ProductMeta> metas) { if (product.ProductId == -1) { Entity.Product entity = product.ToEntity(); context.Products.Add(entity); if (context.SaveChanges() > 0) { if(images != null) foreach (var i in images) { i.ProductId = entity.ProductId; entity.ProductImages.Add(i.ToEntity()); } if(metas != null) foreach (var m in metas) { m.ProductId = entity.ProductId; entity.ProductMetas.Add(m.ToEntity()); } int rt = context.SaveChanges(); if (rt == 0) return new List<string>(); else return new List<string>(new string[] { "Failed" }); } } else { Entity.Product entity = context.Products.Find(product.ProductId); if (entity != null) { entity.CategoryId = product.CategoryId; entity.CompanyId = product.CompanyId; entity.ProductDescriptionEn = product.ProductDescriptionEn; entity.ProductDescriptionEs = product.ProductDescriptionEs; entity.ProductIsActive = product.ProductIsActive; entity.ProductNameEn = product.ProductNameEn; entity.ProductNameEs = product.ProductNameEs; entity.ProductPrice = product.ProductPrice; entity.ProductRetailPrice = product.ProductRetailPrice; context.SaveChanges(); //Clear images entity.ProductImages.Clear(); context.SaveChanges(); if (images != null) foreach (var i in images) { i.ProductId = entity.ProductId; entity.ProductImages.Add(i.ToEntity()); } context.SaveChanges(); //Do Metas proc StringBuilder sb = new StringBuilder(); sb.Append("<root>"); foreach(var m in metas) { m.ProductId = entity.ProductId; sb.Append(m.Xml); } sb.Append("</root>"); var value = context.Database.SqlQuery<string>("EXEC usp_upd_ProductMeta @x", new System.Data.SqlClient.SqlParameter("@x", sb.ToString())); return value.ToList(); } } return new List<string>(new string[] { "Failed" }); }
public int SaveOption(Objects.MetaOption option) { if (option.MetaOptionId == -1) { context.MetaOptions.Add(option.ToEntity()); return context.SaveChanges(); } else { Entity.MetaOption entity = context.MetaOptions.Find(option.MetaOptionId); if (entity != null) { entity.MetaOptionNameEn = option.MetaOptionNameEn; entity.MetaOptionNameEs = option.MetaOptionNameEs; entity.MetaOptionValue = option.MetaOptionValue; return context.SaveChanges(); } return -1; } }