コード例 #1
0
        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();

            }
        }
コード例 #2
0
        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();
            }
        }
コード例 #3
0
        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;
            }
        }
コード例 #4
0
        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();
            }
        }
コード例 #5
0
 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();
     }
 }
コード例 #6
0
 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;
     }
 }
コード例 #7
0
 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;
     }
 }
コード例 #8
0
 private ICollection<Category> getRoots()
 {
     using (var context = new AuctionModelContainer())
     {
         context.setLazyFalse();
         return (from category in context.Categories
                         where category.IdParentCategory == null
                         select category).ToList();
     }
 }
コード例 #9
0
        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();
            }
        }
コード例 #10
0
        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;
            }
        }