public void AddProduct(Product product) { using (var context = new AuctionModelContainer()) { context.setLazyFalse(); if (product.Categories.Count() == 0) { throw new ValidationException("The product must have at least one category setted!"); } ICollection<Product> products = this.GetProdctByNameAndDescription(product.Name, product.Description); foreach (Category category in product.Categories) { foreach (Product p in products) { Product productAux = this.GetProdctById(p.IdProduct); context.Products.Attach(productAux); context.Entry(productAux).Collection(pAux => pAux.Categories).Load(); foreach (Category pcateg in productAux.Categories) { if (category.Name.Equals(pcateg.Name)) throw new DuplicateException("The same product already exists - same name, description and category"); } } } foreach (Category categ in product.Categories) context.Categories.Attach(categ); context.Products.Add(product); context.SaveChanges(); } }
public void AddRoleToUser(User user,Role role) { using(var context = new AuctionModelContainer()) { context.setLazyFalse(); context.Users.Attach(user); context.Roles.Attach(role); context.Entry(user).Collection(u => u.Roles).Load(); user.Roles.Add(role); context.SaveChanges(); } }
public Auction GetAuctionById(int id) { using (var context = new AuctionModelContainer()) { context.setLazyFalse(); var auctionVar = (from auction in context.Auctions where auction.IdAuction.Equals(id) select auction).FirstOrDefault(); if (auctionVar == null) throw new EntityDoesNotExistException("Auction with id "+id+" does not exist"); context.Auctions.Attach(auctionVar); //context.Entry(auctionVar).Collection(pAux => pAux.Categories).Load(); context.Entry(auctionVar).Reference(pAux => pAux.Product).Load(); return auctionVar; } }
public void DropUser(String email) { User user = GetUserByEmail(email); if (user == null) throw new EntityDoesNotExistException("The user with email " + email + " does not exist."); using (var context = new AuctionModelContainer()) { context.setLazyFalse(); context.Users.Attach(user); context.Entry(user).Collection(u => u.Roles).Load(); if (user.Roles.Count > 0) throw new DependencyException("User with email " + email + " has roles asigned so it can't be droped."); context.Users.Remove(user); context.SaveChanges(); } }
public ICollection<Product> GetProdctByNameAndDescription(String name, String description) { using (var context = new AuctionModelContainer()) { context.setLazyFalse(); return context.Products. Where(product => product.Name.Equals(name) && product.Description.Equals(description)). ToList(); } }
public Product GetProdctById(int id) { using (var context = new AuctionModelContainer()) { context.setLazyFalse(); var prodVar = (from product in context.Products where product.IdProduct == id select product).FirstOrDefault(); if (prodVar != null) { context.Products.Attach(prodVar); context.Entry(prodVar).Collection(pAux => pAux.Categories).Load(); //context.Entry(auctionVar).Collection(pAux => pAux.Categories).Load(); context.Entry(prodVar).Reference(pAux => pAux.Auction).Load(); if (prodVar.Auction != null) { context.Entry(prodVar.Auction).Collection(p => p.ProductActions).Load(); context.Entry(prodVar.Auction).Reference(c => c.Currency).Load(); context.Entry(prodVar.Auction).Reference(c => c.User).Load(); } } return prodVar; } }
public Category GetCategoryById(int id) { using(var context = new AuctionModelContainer()) { context.setLazyFalse(); var categVar = (from category in context.Categories where category.IdCategory == id select category).FirstOrDefault(); if (categVar != null) { context.Categories.Attach(categVar); context.Entry(categVar).Collection(categ => categ.Products).Load(); } return categVar; } }
private ICollection<Category> getRoots() { using (var context = new AuctionModelContainer()) { context.setLazyFalse(); return (from category in context.Categories where category.IdParentCategory == null select category).ToList(); } }
public void RemoveRoleFromUser(User user,Role role) { using (var context = new AuctionModelContainer()) { context.setLazyFalse(); context.Users.Attach(user); context.Roles.Attach(role); context.Entry(user).Collection(u => u.Roles).Load(); context.Entry(role).Collection(r => r.Users).Load(); if (!user.Roles.Contains(role)) throw new EntityDoesNotExistException("User with email "+user.Email+" hasn't the role with name "+role.Name+" so it can't be remove."); user.Roles.Remove(role); context.Entry(user).State = EntityState.Modified; context.SaveChanges(); } }
public User GetUserById(int id) { using (var context = new AuctionModelContainer()) { context.setLazyFalse(); var userVar = (from user in context.Users where user.IdUser.Equals(id) select user).FirstOrDefault(); if (userVar == null) throw new EntityDoesNotExistException("User with "+id+" does not exist"); context.Users.Attach(userVar); context.Entry(userVar).Collection(user => user.Roles).Load(); context.Entry(userVar).Collection(user => user.Auctions).Load(); context.Entry(userVar).Collection(user => user.GivedRatings).Load(); return userVar; } }