예제 #1
0
        public async void GetById_ShouldReturnBookWithSearchedId_WhenBookWithSearchedIdExist()
        {
            await using (var context = new BookStoreDbContext(_options))
            {
                var bookRepository = new BookRepository(context);

                var book = await bookRepository.GetById(2);

                Assert.NotNull(book);
                Assert.IsType <Book>(book);
            }
        }
        public async void GetById_ShouldReturnNull_WhenBookWithSearchedIdDoesNotExist()
        {
            await BookStoreHelperTests.CleanDataBase(_options);

            await using (var context = new BookStoreDbContext(_options))
            {
                var bookRepository = new BookRepository(context);
                var book           = await bookRepository.GetById(1);

                Assert.Null(book);
            }
        }
예제 #3
0
 public InfoController(IUserServices userServices, IProductServices productServices, BookStoreDbContext db,
                       IAddressesServices addressesServices, IOrderServices orderServices, IIncomeMoneyService incomeMoneyService, ISearchService searchService, IUserRequestsService userRequestsService)
 {
     this.userServices = userServices;
     _productServices  = productServices;
     _db = db;
     _addressesServices   = addressesServices;
     _orderServices       = orderServices;
     _searchService       = searchService;
     _userRequestsService = userRequestsService;
     _incomeMoneyService  = incomeMoneyService;
 }
예제 #4
0
        public async void GetBooksByCategory_ShouldReturnAListOfBook_WhenBooksWithSearchedCategoryExist()
        {
            await using (var context = new BookStoreDbContext(_options))
            {
                var bookRepository = new BookRepository(context);

                var books = await bookRepository.GetBooksByCategory(1);

                Assert.NotNull(books);
                Assert.IsType <List <Book> >(books);
            }
        }
예제 #5
0
        public async void GetAll_ShouldReturnAListOfCategory_WhenCategoriesExist()
        {
            await using (var context = new BookStoreDbContext(_options))
            {
                var categoryRepository = new CategoryRepository(context);

                var categories = await categoryRepository.GetAll();

                Assert.NotNull(categories);
                Assert.IsType <List <Category> >(categories);
            }
        }
예제 #6
0
        public async void GetById_ShouldReturnCategoryWithCorrectValues_WhenCategoryExist()
        {
            await using (var context = new BookStoreDbContext(_options))
            {
                var categoryRepository = new CategoryRepository(context);

                var expectedCategories = CreateCategoryList();
                var category           = await categoryRepository.GetById(2);

                Assert.Equal(expectedCategories[1].Id, category.Id);
                Assert.Equal(expectedCategories[1].Name, category.Name);
            }
        }
예제 #7
0
        protected void Application_Start()
        {
            // Generate Database
            BookStoreDbContext db = new BookStoreDbContext();

            if (db.Database.CreateIfNotExists())
            {
                initial i = new initial();
                i.InitialData();
            }

            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
 public AdminsController(UserManager <ApplicationUser> userManager, IUserService userService, RoleManager <IdentityRole> roleManager,
                         BookStoreDbContext context, IBaseUrlHelper baseUrlHelper, IImageFileService imageFileService,
                         IAdminService adminService, IMapper mapper)
 {
     _userManager      = userManager;
     _userService      = userService;
     _roleManager      = roleManager;
     _baseUrlHelper    = baseUrlHelper;
     _imageFileService = imageFileService;
     _adminService     = adminService;
     _mapper           = mapper;
     this._context     = context;
 }
예제 #9
0
        public IActionResult AddPublisher(PublisherDTO publisher)
        {
            Publisher newPublisher = _mapper.Map <Publisher>(publisher);

            using (var _context = new BookStoreDbContext())
            {
                _context.Publishers.Add(newPublisher);

                _context.SaveChanges();
            }

            return(Ok());
        }
        public async void SearchBookWithCategory_ShouldReturnAnEmptyList_WhenNoBooksWithSearchedValueExist()
        {
            await using (var context = new BookStoreDbContext(_options))
            {
                var bookRepository = new BookRepository(context);
                var books          = await bookRepository.SearchBookWithCategory("Testt");

                var bookList = books as List <Book>;

                Assert.NotNull(bookList);
                Assert.Empty(bookList);
                Assert.IsType <List <Book> >(bookList);
            }
        }
예제 #11
0
        public async void GetAll_ShouldRetunAnEmptyList_WhenBooksDoNotExist()
        {
            // await BookStoreHelperTests.CleanDataBase(_options);

            await using (var context = new BookStoreDbContext(_options))
            {
                var bookRepository = new BookRepository(context);
                var books          = await bookRepository.GetAll();

                Assert.NotNull(books);
                Assert.Empty(books);
                Assert.IsType <List <Book> >(books);
            }
        }
예제 #12
0
        public async void GetAll_ShouldReturnAnEmptyList_WhenCategoriesDoNotExist()
        {
            await BookStoreHelperTests.CleanDataBase(_options);

            await using (var context = new BookStoreDbContext(_options))
            {
                var categoryRepository = new CategoryRepository(context);
                var categories         = await categoryRepository.GetAll();

                Assert.NotNull(categories);
                Assert.Empty(categories);
                Assert.IsType <List <Category> >(categories);
            }
        }
예제 #13
0
 public static ApplicationUser GetApplicationUser(this IIdentity identity)
 {
     if (identity.IsAuthenticated)
     {
         using (var db = new BookStoreDbContext())
         {
             var userManager = new ApplicationUserManager(new CustomUserStore(db));
             return(userManager.FindByNameAsync(identity.Name).Result);
         }
     }
     else
     {
         return(null);
     }
 }
예제 #14
0
 public static bool IsRole(this IIdentity identity, string roleName)
 {
     if (identity.IsAuthenticated)
     {
         using (var db = new BookStoreDbContext())
         {
             var userManager = new ApplicationUserManager(new CustomUserStore(db));
             return(userManager.IsInRoleAsync(identity.GetApplicationUser().Id, roleName).Result);
         }
     }
     else
     {
         return(false);
     }
 }
예제 #15
0
        public ActionResult Index(string searchString)
        {
            var context = new BookStoreDbContext();

            var books = context.Books
                        .Include(b => b.Author)
                        .Where(b => b.Title.Contains(searchString) ||
                               b.Author.Lastname.Contains(searchString))
                        .Select(BookViewModel.FromBook);

            ViewBag.Success      = true;
            ViewBag.SearchString = searchString;

            return(View(books));
        }
예제 #16
0
        private async Task SeedDb(BookStoreDbContext db)
        {
            var firstBook = new Book {
                Id = 1, Title = "First Book", Price = 10, TraderId = UserId
            };
            var secondBook = new Book {
                Id = 2, Title = "Second Book", Price = 11, TraderId = "penka"
            };
            var thirdBook = new Book {
                Id = 3, Title = "Third Book", Price = 12, TraderId = UserId, CoverPicture = new byte[] { 1, 2, 3, 4 }
            };

            db.AddRange(firstBook, secondBook, thirdBook);

            await db.SaveChangesAsync();
        }
예제 #17
0
        private async Task SeedDb(BookStoreDbContext db)
        {
            var firstUser = new User {
                Id = "ivan", UserName = "******", Address = Address
            };
            var secondUser = new User {
                Id = "petur", UserName = "******"
            };
            var thirdUser = new User {
                Id = "canko", UserName = "******"
            };

            db.AddRange(firstUser, secondUser, thirdUser);

            await db.SaveChangesAsync();
        }
예제 #18
0
        public void CreateShoulCreateIncomeMoneyOrder()
        {
            var options = new DbContextOptionsBuilder <BookStoreDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;
            //

            var dbContext = new BookStoreDbContext(options);

            var incomeMoneyService = new IncomeMoneyService(dbContext);

            incomeMoneyService.Create("D220E7A5-D7A8-465F-A923-066873664B4F", 12.6M, 6, 2, "Card", "Razgrad bul.Bulgaria 13 A", DateTime.Now);

            var isIncomeMonetCreated = dbContext.IcIncomeMonies.ToList();

            Assert.True(isIncomeMonetCreated.Count == 1);
        }
예제 #19
0
        public void CreateShouldCreateSupplierAndAddToDb()
        {
            var options = new DbContextOptionsBuilder <BookStoreDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;
            //

            var dbContext = new BookStoreDbContext(options);

            var supplierService = new SuppliersService(dbContext);

            supplierService.Create("Speedy", 5, 4);

            var isSupplierIsCreate = dbContext.Suppliers.ToList();

            Assert.True(isSupplierIsCreate.Count == 1);
        }
예제 #20
0
        public async void GetById_ShouldReturnBookWithCorrectValues_WhenBookExist()
        {
            await using var context = new BookStoreDbContext(_options);
            var bookRepository = new BookRepository(context);

            var expectedBooks = CreateBookList();
            var book          = await bookRepository.GetById(2);

            Assert.Equal(expectedBooks[1].Id, book.Id);
            Assert.Equal(expectedBooks[1].Name, book.Name);
            Assert.Equal(expectedBooks[1].Description, book.Description);
            Assert.Equal(expectedBooks[1].CategoryId, book.CategoryId);
            Assert.Equal(expectedBooks[1].PublishDate, book.PublishDate);
            Assert.Equal(expectedBooks[1].Value, book.Value);
            Assert.Equal(expectedBooks[1].Category.Id, book.Category.Id);
            Assert.Equal(expectedBooks[1].Category.Name, book.Category.Name);
        }
예제 #21
0
        //static void CreateUser(BookStoreDbContext db)
        //{
        //    var jsonData = File.ReadAllText("jsondata/users.json");
        //    var items = JsonConvert.DeserializeObject<List<ApplicationUser>>(jsonData);

        //}
        static void CreateAspNetUsers(BookStoreDbContext db)
        {
            var jsonData    = File.ReadAllText("jsonData/users.json");
            var userModels  = JsonConvert.DeserializeObject <List <UserModel> >(jsonData);
            var userManager = new ApplicationUserManager(new UserStore <ApplicationUser>(db));

            foreach (UserModel userModel in userModels)
            {
                var applicationUser = new ApplicationUser {
                    UserName = userModel.Login
                };
                userManager.Create(applicationUser, userModel.Password);
                var employee = db.Employees.Find(userModel.EmployeeId);
                employee.ApplicationUserId = applicationUser.Id;
                db.SaveChanges();
            }
        }
        private static SqliteConnection CreateDatabaseAndGetConnection()
        {
            var connection = new SqliteConnection("Data Source=:memory:");

            connection.Open();

            var options = new DbContextOptionsBuilder <BookStoreDbContext>()
                          .UseSqlite(connection)
                          .Options;

            using (var context = new BookStoreDbContext(options))
            {
                context.GetService <IRelationalDatabaseCreator>().CreateTables();
            }

            return(connection);
        }
예제 #23
0
        //  [TestMethod]
        public void GenerateRandomBookCategoriesData()
        {
            // foreach book add up to 3 categories

            //  BooksRepository repository = new BooksRepository( new BookStoreDbContext());

            //using (BookStoreDbContext db = new BookStoreDbContext())
            //{
            //    var book = db.Books.FirstOrDefault(b => b.Id == 2333);
            //    var category = db.Categories.FirstOrDefault(c => c.Id == 1);
            //    if (category != null)
            //    {
            //        category.Books = new List<Book> {book};
            //    }
            //    db.SaveChanges();
            //}


            using (BookStoreDbContext db = new BookStoreDbContext())
            {
                //var books = db.Books.OrderBy(b => Guid.NewGuid()).Take(5).ToList();
                var books = db.Books.ToList();

                foreach (var b in books)
                {
                    Random rnd = new Random();
                    int    randomCategoriesCount = rnd.Next(1, 5);
                    var    categories            = db.Categories.OrderBy(c => Guid.NewGuid()).Take(randomCategoriesCount).ToList();
                    b.Categories = new List <Category>();
                    foreach (var c in categories)
                    {
                        b.Categories.Add(c);
                        db.SaveChanges();
                    }
                }
            }


            ////  var books =  repository.GetAll().Take(2);
            // // var category = new CategoryRepository(new BookStoreDbContext()).GetAll().First();
            //  foreach (var b in books)
            //  {

            //  }
            Assert.AreEqual(4114, 4114);
        }
예제 #24
0
        public void CreateAddressShoudCreateAddress()
        {
            var options = new DbContextOptionsBuilder <BookStoreDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;
            //
            var dbContext = new BookStoreDbContext(options);

            var addressServices = new AddressesServices(dbContext, null);

            addressServices.CreateAddress("Beli lom", "Some text", "Razgrad", "7200");
            addressServices.CreateAddress("Beli lom", "Some text", "Razgrad", "7201");

            var countAddress = dbContext.Addresses.ToArray().Length;

            Assert.Equal(2, countAddress);
        }
예제 #25
0
        public async void GetAll_ShouldReturnAListOfCategoryWithCorrectValues_WhenCategoriesExist()
        {
            await using (var context = new BookStoreDbContext(_options))
            {
                var expectedCategories = CreateCategoryList();
                var categoryRepository = new CategoryRepository(context);
                var categoryList       = await categoryRepository.GetAll();

                Assert.Equal(3, categoryList.Count);
                Assert.Equal(expectedCategories[0].Id, categoryList[0].Id);
                Assert.Equal(expectedCategories[0].Name, categoryList[0].Name);
                Assert.Equal(expectedCategories[1].Id, categoryList[1].Id);
                Assert.Equal(expectedCategories[1].Name, categoryList[1].Name);
                Assert.Equal(expectedCategories[2].Id, categoryList[2].Id);
                Assert.Equal(expectedCategories[2].Name, categoryList[2].Name);
            }
        }
예제 #26
0
        public async Task <bool> ExecuteAsync(SaveOrderModel orderModel)
        {
            using (var db = new BookStoreDbContext())
            {
                var order = new Order
                {
                    ClientId   = orderModel.ClientId,
                    EmployeeId = orderModel.EmployeeId,
                    OrderDate  = orderModel.OrderDate,
                    TotalConst = orderModel.TotalCost
                };
                db.Orders.Add(order);

                foreach (var bookModel in orderModel.OrderedBooks)
                {
                    db.OrderedBooks.Add(
                        new OrderedBook
                    {
                        Amount = bookModel.Amount,
                        BookId = bookModel.BookId,
                        Order  = order,
                        Price  = bookModel.Price
                    });
                }

                var bookIds = orderModel.OrderedBooks.Select(ob => ob.BookId).ToArray();

                var bookAmounts = await db.BookAmounts
                                  .Where(ba => ba.BranchId == orderModel.BranchId)
                                  .Where(ba => bookIds.Contains(ba.BookId))
                                  .ToListAsync();

                foreach (var bookAmount in bookAmounts)
                {
                    var orderAmount = orderModel.OrderedBooks
                                      .First(ob => ob.BookId == bookAmount.BookId)
                                      .Amount;
                    bookAmount.Amount -= orderAmount;
                }

                await db.SaveChangesAsync();

                return(true);
            }
        }
예제 #27
0
        public void GetAllProductsShouldReturnListOfProducts()
        {
            var options = new DbContextOptionsBuilder <BookStoreDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;
            //

            var dbContext = new BookStoreDbContext(options);

            var genreServices  = new Mock <IGenreService>();
            var _searchService = new Mock <ISearchService>();
            var _userServices  = new Mock <IUserServices>();

            var productServices = new ProductServices(dbContext, genreServices.Object, _searchService.Object, _userServices.Object);
            //
            var testProduct1 = new Product()
            {
                Id           = 1,
                Title        = "Бай Ганьо1",
                ProductTypes = ProductTypes.Book,
                Price        = 153.03M,
                Quantity     = 1
            };
            var testProduct2 = new Product()
            {
                Id           = 2,
                Title        = "Бай Ганьо2",
                ProductTypes = ProductTypes.Book,
                Price        = 153.03M,
                Quantity     = 1
            };
            var listOfProducts = new List <Product>()
            {
                testProduct2,
                testProduct1
            };

            dbContext.Products.AddRange(listOfProducts);
            dbContext.SaveChanges();


            var productFromDB = productServices.GetAllProducts().Count() == 2;

            Assert.True(productFromDB);
        }
예제 #28
0
        public static void SeedData(BookStoreDbContext context)
        {
            if (context.Categories.Count() == 0)
            {
                context.Categories.AddRange(categories);
            }
            if (context.Authors.Count() == 0)
            {
                context.Authors.AddRange(authors);
            }

            if (context.Products.Count() == 0)
            {
                context.Products.AddRange(products);
            }

            context.SaveChanges();
        }
예제 #29
0
        public void AllPurchaseShoulReturnIncomeMoneyOrders()
        {
            var options = new DbContextOptionsBuilder <BookStoreDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;
            //

            var dbContext = new BookStoreDbContext(options);

            var incomeMoneyService = new IncomeMoneyService(dbContext);

            incomeMoneyService.Create("D220E7A5-D7A8-465F-A923-066873664B4F", 12.6M, 6, 2, "Card", "Razgrad bul.Bulgaria 13 A", DateTime.Now);
            incomeMoneyService.Create("2C1D4E9A-EB8A-4ED7-8E67-9B4CE19FFDBD", 112.6M, 6, 3, "Card", "Razgrad bul.Bulgaria 13 B", DateTime.Now);

            var listOfPurchase = incomeMoneyService.AllPurchase().ToList();

            Assert.True(listOfPurchase.Count == 2);
        }
예제 #30
0
        public static BookStoreDbContext Create()
        {
            var options = new DbContextOptionsBuilder <BookStoreDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var context = new BookStoreDbContext(
                new MachineDateTime(),
                new AnonymousUserService(),
                options
                );

            context.Database.EnsureCreated();

            FillTestData(context);

            return(context);
        }
예제 #31
0
 public OrderService()
 {
     db = new BookStoreDbContext();
 }
 public PromotionService()
 {
     db = new BookStoreDbContext();
 }
예제 #33
0
 public GroupService()
 {
     db = new BookStoreDbContext();
 }
 public AccountService()
 {
     db = new BookStoreDbContext();
 }
 public CategoryService()
 {
     db = new BookStoreDbContext();
 }