Esempio n. 1
0
        public virtual Guid Add(TEntity obj)
        {
            _db.Set <TEntity>().Add(obj);
            _db.SaveChanges();

            return(obj.Id);
        }
Esempio n. 2
0
        public override void Update(Game obj)
        {
            Game game = _db.Games.Include(_ => _.GameDevelopers)
                        .ThenInclude(_ => _.Developer)
                        .Include(_ => _.GameGenres)
                        .ThenInclude(_ => _.Genre)
                        .Include(_ => _.GamePlatforms)
                        .ThenInclude(_ => _.Platform)
                        .Include(_ => _.GamePublishers)
                        .ThenInclude(_ => _.Publisher).FirstOrDefault(x => x.Id == obj.Id);

            if (game is null)
            {
                throw new ArgumentException();
            }

            // var result = _db.GameDevelopers.FromSql($"SELECT FROM gameDevelopers WHERE GameId = {obj.Id}").ToList();

            game.ChangeName(obj.Name);
            game.ChangePrice(obj.Price);
            game.ChangeReleaseDate(obj.ReleaseDate);
            game.ChangeDescription(obj.Description);
            game.ChangeShortDescription(obj.ShortDescription);
            game.ChangeDevelopersList(obj.GameDevelopers);
            game.ChangePublishersList(obj.GamePublishers);
            game.ChangeGenresList(obj.GameGenres);
            game.ChangePlatformsList(obj.GamePlatforms);
            _db.SaveChanges();
        }
Esempio n. 3
0
        public void CreateOrderDB(int selectProd, int selectCust, int citySelect, int quant)
        {
            //string connectionString = SecretConfiguration.configurationString;
            string connectionString = "";

            DbContextOptions <GameStoreContext> options = new DbContextOptionsBuilder <GameStoreContext>()
                                                          .UseSqlServer(connectionString)
                                                          .Options;

            using var context = new GameStoreContext(options);

            LocationData locDat = new LocationData();

            Orders          orders       = new Orders();
            OrderedProducts orderedProds = new OrderedProducts();

            orders.CustomerId = selectCust;
            orders.Quantity   = quant;
            orders.OrderDate  = DateTime.Now;

            context.Orders.Add(orders);
            context.SaveChanges();

            orderedProds.CustomerId = selectCust;
            orderedProds.OrderId    = (int)orders.OrderId;
            orderedProds.ProductId  = selectProd;
            orderedProds.LocationId = citySelect;

            context.OrderedProducts.Add(orderedProds);
            context.SaveChanges();

            locDat.InventoryUpdate((int)orderedProds.LocationId, (int)orderedProds.ProductId, (int)orders.Quantity);

            //Add Decrementing Functionality To The Inventory Of The Location The Order is Created From.1
        }
Esempio n. 4
0
        public void CreateOrderDB(int selectProd, int selectCust, int citySelect, int quant, string dateString)
        {
            string connectionString = SecretConfiguration.configurationString;

            DbContextOptions <GameStoreContext> options = new DbContextOptionsBuilder <GameStoreContext>()
                                                          .UseSqlServer(connectionString)
                                                          .Options;

            using var context = new GameStoreContext(options);

            Orders          orders       = new Orders();
            OrderedProducts orderedProds = new OrderedProducts();

            /*Random random = new Random();
             * orders.OrderId = random.Next(10000, 99999);*/

            orders.CustomerId = selectCust;
            orders.Quantity   = quant;
            orders.OrderDate  = DateTime.ParseExact(dateString, "MM/dd/yyyy", null);

            context.Orders.Add(orders);
            context.SaveChanges();

            orderedProds.CustomerId = selectCust;
            orderedProds.OrderId    = (int)orders.OrderId;
            orderedProds.ProductId  = selectProd;
            orderedProds.LocationId = citySelect;

            context.OrderedProducts.Add(orderedProds);
            context.SaveChanges();
        }
Esempio n. 5
0
        public string ReplenishBalance(int enteredSum)
        {
            Account account = db.Accounts.FirstOrDefault(obj => obj.Login == User.Identity.Name);

            account.Balance        += enteredSum;
            db.Entry(account).State = EntityState.Modified;
            db.SaveChanges();
            return("ok");
        }
Esempio n. 6
0
        public void AlterarGenero(GeneroModel genero)
        {
            var oldGenero = _context.Genero.Find(genero.Id);

            oldGenero.Id    = genero.Id;
            oldGenero.Nome  = genero.Nome;
            oldGenero.Jogos = genero.Jogos;
            _context.Attach(genero).State = EntityState.Modified;
            _context.SaveChanges();
        }
Esempio n. 7
0
        public void AddProductsDB(string productName, string productType, int storeLocation, int inventoryForLoc, int storeCount, List <int> storeLocationList, List <int> storeInventoryList)
        {
            string connectionString = SecretConfiguration.configurationString;

            DbContextOptions <GameStoreContext> options = new DbContextOptionsBuilder <GameStoreContext>()
                                                          .UseSqlServer(connectionString)
                                                          .Options;

            using var context = new GameStoreContext(options);

            Products prod = new Products();

            prod.ProductName = productName;
            prod.ProductType = productType;

            context.Products.Add(prod);
            context.SaveChanges();

            for (int i = 0; i < storeCount; i++)
            {
                Locations tempLoc = new Locations();
                Inventory tempInv = new Inventory();

                tempLoc.LocationId = storeLocationList[i];

                var foundName = context.Locations.FirstOrDefault(p => p.LocationId == storeLocationList[i]);

                tempLoc.City = foundName.City;

                if (foundName is null)
                {
                    //context.Locations.Add(tempLoc);
                    //context.SaveChanges();
                    //context.Products.Add(prod);
                    //context.SaveChanges();
                }
                else
                {
                    tempLoc.LocationId = foundName.LocationId;
                    tempInv.LocationId = tempLoc.LocationId;
                    //tempInv.ProductId = prod.ProductId;
                }
                tempInv.ProductId = prod.ProductId;

                tempInv.Inventory1 = storeInventoryList[i];

                context.Inventory.Add(tempInv);
            }

            context.SaveChanges();
        }
        public void AddProductsDB(Products products, List <string> locs, List <string> inv, int storeCount)
        {
            //string connectionString = SecretConfiguration.configurationString;
            string connectionString = "";

            DbContextOptions <GameStoreContext> options = new DbContextOptionsBuilder <GameStoreContext>()
                                                          .UseSqlServer(connectionString)
                                                          .Options;

            using var context = new GameStoreContext(options);

            Products prod = new Products();

            prod.ProductName = products.ProductName;
            prod.ProductType = products.ProductType;
            prod.ProductUrl  = products.ProductUrl;

            context.Products.Add(prod);
            context.SaveChanges();

            for (int i = 0; i < storeCount; i++)
            {
                Locations tempLoc = new Locations();
                Inventory tempInv = new Inventory();


                tempLoc.LocationId = int.Parse(locs[i]);

                var foundName = context.Locations.FirstOrDefault(p => p.LocationId == tempLoc.LocationId);

                tempLoc.City = foundName.City;

                if (foundName is null)
                {
                }
                else
                {
                    tempLoc.LocationId = foundName.LocationId;
                    tempInv.LocationId = tempLoc.LocationId;
                }
                tempInv.ProductId = prod.ProductId;

                tempInv.Inventory1 = int.Parse(inv[i]);

                context.Inventory.Add(tempInv);
            }

            context.SaveChanges();
        }
        public void Edit(
            int id,
            string title,
            string description,
            string image,
            decimal price,
            double size,
            string videoId,
            DateTime releaseDate)
        {
            using (var db = new GameStoreContext())
            {
                var game = db
                           .Games
                           .FirstOrDefault(g => g.Id == id);

                game.Title       = title;
                game.Description = description;
                game.Image       = image;
                game.Price       = price;
                game.Size        = size;
                game.VideoId     = videoId;
                game.ReleaseDate = releaseDate;

                db.SaveChanges();
            }
        }
Esempio n. 10
0
        public bool Create(string email, string name, string password)
        {
            using (var db = new GameStoreContext())
            {
                if (db.Users.Any(u => u.Email == email))
                {
                    return(false);
                }

                var isAdmin = !db.Users.Any();

                var user = new User
                {
                    Email    = email,
                    Name     = name,
                    Password = password,
                    IsAdmin  = isAdmin
                };

                db.Add(user);
                db.SaveChanges();
            }

            return(true);
        }
        public async Task RemoveComments_WhenProductID_IsValid()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <GameStoreContext>()
                          .UseInMemoryDatabase("RemoveComments_WhenProductID_IsValid").Options;


            var validUser = new Account
            {
                UserName  = $"TestUsername",
                FirstName = "FirstName",
                LastName  = "LastName"
            };

            var commentToAdd = new Comment
            {
                Text = "TestDescription",
            };

            using (var curContext = new GameStoreContext(options))
            {
                curContext.Accounts.Add(validUser);
                commentToAdd.AccountId = validUser.Id;
                curContext.Comments.Add(commentToAdd);
                curContext.SaveChanges();

                //Act
                var sut = new CommentService(curContext);
                await sut.RemoveComment(commentToAdd.Id);

                //Assert

                Assert.IsTrue(!curContext.Comments.Any(comment => comment.IsDeleted == false));
            }
        }
        public async Task Return_True_IfItExists()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <GameStoreContext>()
                          .UseInMemoryDatabase("Return_True_IfItExists").Options;

            var product = new Product
            {
                Name                 = "Test",
                Description          = "test description",
                ShoppingCartProducts = new List <ShoppingCartProducts>(),
                Comments             = new List <Comment>()
            };

            //Act
            using (var curContext = new GameStoreContext(options))
            {
                curContext.Products.Add(product);
                curContext.SaveChanges();
                var sut    = new ProductsService(curContext);
                var result = await sut.ProductExistsAsync(product.Id);

                Assert.IsTrue(result);
            }
        }
        public async Task ReturnComments_WhenAccountId_IsValid()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <GameStoreContext>()
                          .UseInMemoryDatabase("ReturnComments_WhenAccountId_IsValid").Options;



            var validUser = new Account
            {
                UserName  = $"TestUsername",
                FirstName = "FirstName",
                LastName  = "LastName"
            };
            var commentToAdd = new Comment
            {
                Text = "TestDescription",
            };

            using (var curContext = new GameStoreContext(options))
            {
                curContext.Accounts.Add(validUser);
                commentToAdd.AccountId = validUser.Id;
                curContext.Comments.Add(commentToAdd);
                curContext.SaveChanges();
                //Act
                var sut    = new CommentService(curContext);
                var result = await sut.GetCommentsByUserAsync(validUser.Id);

                //Assert
                Assert.IsTrue(result.First() == commentToAdd);
            }
        }
        //ToDO
        public async void ThrowException_WhenInput_AlreadyExists()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <GameStoreContext>()
                          .UseInMemoryDatabase("ThrowException_WhenInput_AlreadyExists").Options;

            var productToAdd = new Product
            {
                Name                 = "Test",
                Description          = "test description",
                ShoppingCartProducts = new List <ShoppingCartProducts>(),
                Comments             = new List <Comment>()
            };

            //Act
            using (var curContext = new GameStoreContext(options))
            {
                var sut = new ProductsService(curContext);
                curContext.Products.Add(productToAdd);

                curContext.SaveChanges();
            }

            //Act
            using (var curContext = new GameStoreContext(options))
            {
                var sut = new ProductsService(curContext);
                await sut.AddProductAsync(productToAdd);
            }
        }
        public void AddProduct_WhenInput_IsValid()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <GameStoreContext>()
                          .UseInMemoryDatabase("AddProduct_WhenInput_IsValid").Options;

            var productToAdd = new Product
            {
                Name                 = "Test",
                Description          = "test description",
                ShoppingCartProducts = new List <ShoppingCartProducts>(),
                Comments             = new List <Comment>()
            };

            //Act
            using (var curContext = new GameStoreContext(options))
            {
                var sut = new ProductsService(curContext);
                curContext.Products.Add(productToAdd);
                curContext.SaveChanges();
            }


            //Assert
            using (var curContext = new GameStoreContext(options))
            {
                Assert.IsTrue(curContext.Products.Count() == 1);
            }
        }
        public bool Create(
            string title,
            string description,
            string image,
            decimal price,
            double size,
            string videoId,
            DateTime releaseDate)
        {
            using (var db = new GameStoreContext())
            {
                if (db.Games.Any(g => g.Title == title))
                {
                    return(false);
                }

                var game = new Game
                {
                    Title       = title,
                    Description = description,
                    Image       = image,
                    Price       = price,
                    Size        = size,
                    VideoId     = videoId,
                    ReleaseDate = releaseDate
                };

                db.Add(game);
                db.SaveChanges();
            }

            return(true);
        }
Esempio n. 17
0
 public ActionResult <IEnumerable <string> > Get()
 {
     _db.Publishers.Add(new DAL.Models.Publisher()
     {
         Name = "blizzard"
     });
     _db.SaveChanges();
     return(new string[] { "value1", "value2" });
 }
Esempio n. 18
0
 public void AddToCart(Game game)
 {
     gameStoreContext.ShopCartItems.Add(new ShopCartItem
     {
         ShopCartId = ShopCartId,
         game       = game,
         Price      = (int)game.Price
     });
     gameStoreContext.SaveChanges();
 }
        public override Guid Add(ShoppingCart cart)
        {
            if (_db.ShoppingCarts.Where(_ => _.Id == cart.Id && _.Active).Count() > 0)
            {
                throw new Exception("There is already an active shopping cart for this user");
            }
            else
            {
                _db.Set <ShoppingCart>().Add(cart);
            }

            _db.SaveChanges();
            return(cart.Id);
        }
Esempio n. 20
0
        public async Task ReturnCart_AccountId_IsValid()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <GameStoreContext>()
                          .UseInMemoryDatabase("ReturnCart_AccountId_IsValid").Options;



            var validUser = new Account
            {
                UserName  = $"TestUsername",
                FirstName = "FirstName",
                LastName  = "LastName"
            };
            var cart = new ShoppingCart
            {
                ShoppingCartProducts = new List <ShoppingCartProducts>()
            };

            var cartProd = new ShoppingCartProducts()
            {
            };

            var product = new Product
            {
                Name                 = "Test",
                Description          = "test description",
                ShoppingCartProducts = new List <ShoppingCartProducts>(),
                Comments             = new List <Comment>()
            };


            using (var curContext = new GameStoreContext(options))
            {
                curContext.Accounts.Add(validUser);
                curContext.ShoppingCarts.Add(cart);
                validUser.ShoppingCart = cart;
                cartProd.Product       = product;
                cartProd.ShoppingCart  = cart;
                cart.ShoppingCartProducts.Add(cartProd);
                curContext.SaveChanges();

                //Act
                var sut    = new ShoppingCartsService(curContext);
                var result = await sut.GetUserCartAsync(validUser.Id);

                Assert.AreEqual(cart, result);
            }
        }
Esempio n. 21
0
        public void AddCustomerDB(Customers customers)
        {
            //string connectionString = SecretConfiguration.configurationString;
            string connectionString = "";

            DbContextOptions <GameStoreContext> options = new DbContextOptionsBuilder <GameStoreContext>()
                                                          .UseSqlServer(connectionString)
                                                          .Options;

            using var context = new GameStoreContext(options);

            context.Customers.Add(customers);

            context.SaveChanges();
        }
Esempio n. 22
0
 public string Post([FromBody] Account account)
 {
     if (db.Accounts.FirstOrDefault(obj => obj.Login == account.Login) == null)
     {
         account.Balance = null;
         AccountType accountType = db.AccountTypes.FirstOrDefault(obj => obj.Id == account.AccountTypeId);
         if (accountType.Type == "Client")
         {
             account.Balance = 2000;
         }
         db.Accounts.Add(account);
         db.SaveChanges();
         return("ok");
     }
     return("Login already used");
 }
        public async Task AddComment_WhenInput_IsValid()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <GameStoreContext>()
                          .UseInMemoryDatabase("AddComment_WhenInput_IsValid").Options;

            var commentToAdd = new Comment
            {
                Account   = new Account(),
                Product   = new Product(),
                Text      = "TestDescription",
                TimeStamp = DateTime.Now
            };

            var validUser = new Account
            {
                UserName  = $"TestUsername",
                FirstName = "FirstName",
                LastName  = "LastName"
            };

            var product = new Product
            {
                Name                 = "Test",
                Description          = "test description",
                ShoppingCartProducts = new List <ShoppingCartProducts>(),
                Comments             = new List <Comment>()
            };

            using (var curContext = new GameStoreContext(options))
            {
                curContext.Products.Add(product);
                curContext.Accounts.Add(validUser);
                curContext.SaveChanges();
                //Act
                var sut = new CommentService(curContext);
                var foo = await sut.AddCommentToProductAsync(product.Id, validUser.Id, "TestDescription");

                //Assert

                Assert.IsTrue(curContext.Comments.Count() == 1);
                Assert.IsTrue(curContext.Comments.FirstOrDefault().Text == "TestDescription");
                Assert.IsTrue(curContext.Comments.Include(c => c.Account).FirstOrDefault().Account.Id ==
                              validUser.Id);
            }
        }
Esempio n. 24
0
        public async Task RemoveComments_WhenProductID_IsValid()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <GameStoreContext>()
                          .UseInMemoryDatabase("RemoveComments_WhenProductID_IsValid").Options;



            var product = new Product
            {
                Name                 = "Test",
                Description          = "test description",
                ShoppingCartProducts = new List <ShoppingCartProducts>(),
                Comments             = new List <Comment>()
            };

            var commentToAdd = new Comment
            {
                Account = new Account(),
                Text    = "TestDescription",
            };

            using (var curContext = new GameStoreContext(options))
            {
                curContext.Products.Add(product);
                product.Comments.Add(commentToAdd);

                commentToAdd.ProductId = product.Id;
                commentToAdd.Product   = product;



                curContext.Comments.Add(commentToAdd);
                curContext.SaveChanges();
                //Act
                var sut = new CommentService(curContext);
                await sut.RemoveCommentsFromProductAsync(product.Id);

                //Assert


                Assert.IsTrue(product.Comments.Any());

                Assert.IsTrue(!product.Comments.Any(comment => comment.IsDeleted == false));
            }
        }
Esempio n. 25
0
        public void createOrder(Order order)
        {
            order.OrderTime = DateTime.Now;
            gameStoreContext.Orders.Add(order);

            var items = shopCart.listShopItems;

            foreach (var el in items)
            {
                var orderDetail = new OrderDetail()
                {
                    GameId  = el.game.Id,
                    OrderId = order.Id,
                    Price   = (int)el.game.Price
                };
                gameStoreContext.Orders.Add(order);
            }
            gameStoreContext.SaveChanges();
        }
Esempio n. 26
0
        public async Task SaveProductImageAsync(string root, string filename, Stream stream, int productId)
        {
            var product = await this.FindProductAsync(productId);

            if (product == null)
            {
                throw new Exception("Product Not Found");
            }

            var imageName = Guid.NewGuid() + Path.GetExtension(filename);
            var path      = Path.Combine(root, imageName);

            using (var fileStream = File.Create(path))
            {
                await stream.CopyToAsync(fileStream);
            }

            product.ProductImageName = imageName;
            storeContext.SaveChanges();
        }
Esempio n. 27
0
        public async Task SaveAvatarImageAsync(string root, string filename, Stream stream, string userId)
        {
            var user = storeContext.Users.Find(userId);

            if (user == null)
            {
                throw new Exception("User Not Found");
            }

            var imageName = Guid.NewGuid() + Path.GetExtension(filename);
            var path      = Path.Combine(root, imageName);

            using (var fileStream = File.Create(path))
            {
                await stream.CopyToAsync(fileStream);
            }

            user.AvatarImageName = imageName;
            storeContext.SaveChanges();
        }
Esempio n. 28
0
        public void AddCustomerDB(string fullName)
        {
            string connectionString = SecretConfiguration.configurationString;

            DbContextOptions <GameStoreContext> options = new DbContextOptionsBuilder <GameStoreContext>()
                                                          .UseSqlServer(connectionString)
                                                          .Options;

            using var context = new GameStoreContext(options);

            Customers newCust = new Customers();

            string[] name = fullName.Split(' ');

            newCust.FirstName = name[0];
            newCust.LastName  = name[1];

            context.Customers.Add(newCust);

            context.SaveChanges();
        }
        public void InventoryUpdate(int location, int product, int amount)
        {
            //string connectionString = SecretConfiguration.configurationString;
            string connectionString = "";

            DbContextOptions <GameStoreContext> options = new DbContextOptionsBuilder <GameStoreContext>()
                                                          .UseSqlServer(connectionString)
                                                          .Options;

            using var context = new GameStoreContext(options);

            var foundName = context.Inventory.FirstOrDefault(p => p.LocationId == location && p.ProductId == product);

            if (foundName is null)
            {
                return;
            }

            foundName.Inventory1 -= amount;

            context.Inventory.Update(foundName);
            context.SaveChanges();
        }
Esempio n. 30
0
        public async Task FindProduct_GivenNameMatching_Predicate()
        {
            var options = new DbContextOptionsBuilder <GameStoreContext>()
                          .UseInMemoryDatabase("NotFindProduct_WhenPredicate_IsFalse").Options;
            var product1 = new Product
            {
                Name                 = "Banana",
                Description          = "test description",
                ShoppingCartProducts = new List <ShoppingCartProducts>(),
                Comments             = new List <Comment>()
            };


            var product2 = new Product
            {
                Name                 = "Test",
                Description          = "test description",
                ShoppingCartProducts = new List <ShoppingCartProducts>(),
                Comments             = new List <Comment>()
            };


            using (var curContext = new GameStoreContext(options))
            {
                curContext.Products.Add(product1);
                curContext.Products.Add(product2);
                curContext.SaveChanges();
                //Act
                var sut     = new ProductsService(curContext);
                var results = await sut.FindProductsByName(product => product.Name.Contains("nana"));

                //Assert

                Assert.IsTrue(results.Count() == 1);
                Assert.IsTrue(results.First() == product1);
            }
        }