public async Task <IActionResult> Create(CreateOrUpdateBookViewModel book, IFormFile fImage)
        {
            if (ModelState.IsValid)
            {
                if (fImage == null || fImage.Length == 0)
                {
                    ModelState.AddModelError("", "Uploaded file is empty or null.");

                    DropdownForm(null);

                    return(View(nameof(CreateOrUpdate), book));
                }
                string image = UploadFileExtensions.UploadFile(fImage, "books");

                if (!string.IsNullOrEmpty(image))
                {
                    book.Image = image;
                }

                book.Alias = book.Title.ToFriendlyUrl();

                var viewModel = _mapper.Map <Book>(book);

                _context.Add(viewModel);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            DropdownForm(book);

            return(View(nameof(CreateOrUpdate), book));
        }
Exemple #2
0
 public bool Add(Order item)
 {
     try
     {
         _context.Add(item);
         _context.SaveChanges();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        public async Task <IActionResult> Create(CreateOrUpdateAuthorViewModel Author)
        {
            if (ModelState.IsValid)
            {
                Author.Alias = Author.Name.ToFriendlyUrl();

                Author.UpdateDate = DateTime.Now;

                var viewModel = _mapper.Map <Author>(Author);

                _context.Add(viewModel);

                Alert("Lưu danh mục thành công!");
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(nameof(CreateOrUpdate), Author));
        }
        public async Task <IActionResult> Create(CreateOrUpdateViewModel category)
        {
            if (ModelState.IsValid)
            {
                category.Alias = category.Name.ToFriendlyUrl();

                category.UpdateDate = DateTime.Now;

                var viewModel = _mapper.Map <Category>(category);

                _context.Add(viewModel);

                Alert("Lưu danh mục thành công!");

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(nameof(CreateOrUpdate), category));
        }
        public async Task FilterShouldReturnCorrectResultsWithSeachTextAndOrder()
        {
            // Arrange
            var dbContextOptions = new DbContextOptionsBuilder <BookShopDbContext>()
                                   .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                   .Options;
            var bookShopDbContext = new BookShopDbContext(dbContextOptions);

            var firstBook = new Book
            {
                Id          = 1,
                Title       = "Test Title 1",
                Description = "test description 1",
                AuthorId    = 1
            };

            var secondBook = new Book
            {
                Id          = 2,
                Title       = "Test Title 2",
                Description = "test description 2",
                AuthorId    = 1
            };

            var thirdBook = new Book
            {
                Id          = 3,
                Title       = "Test Title 3",
                Description = "A description that contains title 1",
                AuthorId    = 1
            };

            var category = new Category
            {
                Id   = 1,
                Name = "TestCategory"
            };

            var bookCategory = new BookCategory
            {
                BookId     = 1,
                CategoryId = 1
            };

            var author = new Author
            {
                Id        = 1,
                FirstName = "Test",
                LastName  = "User"
            };

            bookShopDbContext.Add(category);
            bookShopDbContext.Add(bookCategory);
            bookShopDbContext.Add(author);
            bookShopDbContext.Add(firstBook);
            bookShopDbContext.Add(secondBook);
            bookShopDbContext.Add(thirdBook);

            await bookShopDbContext.SaveChangesAsync();

            var configuration = new MapperConfiguration(config => config.AddProfile <AutoMapperServiceProfile>());
            var mapper        = new Mapper(configuration);

            var bookService = new BookService(bookShopDbContext, mapper);

            // Act
            var result = await bookService.Filter("title 1");

            // Assert
            result
            .Should()
            .HaveCount(2);

            var firstResultBook  = result.ElementAt(0);
            var secondResultBook = result.ElementAt(1);

            firstResultBook.Id.Should().Be(firstBook.Id);
            firstResultBook.Categories.Count().Should().Be(1);
            firstResultBook.Categories.First().Should().Be(category.Name);
            firstResultBook.Author.Id.Should().Be(author.Id);
            firstResultBook.Author.FirstName.Should().Be(author.FirstName);
            firstResultBook.Author.LastName.Should().Be(author.LastName);

            secondResultBook.Id.Should().Be(thirdBook.Id);
            secondResultBook.Categories.Should().BeNullOrEmpty();
            secondResultBook.Author.Id.Should().Be(author.Id);
            secondResultBook.Author.FirstName.Should().Be(author.FirstName);
            secondResultBook.Author.LastName.Should().Be(author.LastName);
        }