예제 #1
0
        public async Task CreateBook_CallsCreateBookAsyncWithBookParam()
        {
            //  Arrange
            var bookRequest = new Models.BookRequest()
            {
                ISBN          = "1234567890123",
                PublishedDate = "New published date",
                Descr         = "New description.",
                ThumbnailUrl  = "New thumbnail URL",
                Title         = "New Title",
            };

            var newBook = new Domain.Book()
            {
                ISBN          = bookRequest.ISBN,
                LongDescr     = "Long description.",
                PublishedDate = bookRequest.PublishedDate,
                ShortDescr    = bookRequest.Descr,
                ThumbnailUrl  = bookRequest.ThumbnailUrl,
                Title         = bookRequest.Title,
            };

            _mockMapper.Setup(m => m.Map(bookRequest, It.IsAny <Action <IMappingOperationOptions <Models.BookRequest, Domain.Book> > >()))
            .Returns(newBook);
            _mockBookService.Setup(b => b.UpdateBookAsync(newBook));
            var sut = new BooksController(_mockLogger.Object, _mockBookService.Object, _mockMapper.Object);

            //  Act
            await sut.UpdateBook(newBook.BookId, bookRequest);

            //  Assert
            _mockBookService.Verify(s => s.UpdateBookAsync(newBook), Times.Once, $"Expected UpdateBookAsync have been called once with provided book");
        }
        public async Task <CreateBookCommandResult> Handle(CreateBookManualCommand request, CancellationToken cancellationToken)
        {
            var categories = await _categoryRepository.GetAllByIdAsync(request.CategoriesIds);

            if (!categories.Any())
            {
                return(new CreateBookCommandResult(false, new[] { "Requested category/s not found" }));
            }

            var authors = await _authorRepository.GetAllByIdAsync(request.AuthorsIds);

            if (!authors.Any())
            {
                return(new CreateBookCommandResult(false, new[] { "Requested category/s not found" }));
            }

            var book = new Domain.Book(request.Title,
                                       request.Description, request.Isbn10,
                                       request.Isbn13, request.LanguageId,
                                       request.PublisherId, request.PageCount,
                                       request.Visibility, request.PublishedDate);

            foreach (var category in categories)
            {
                book.AddCategory(category);
            }

            foreach (var author in authors)
            {
                book.AddAuthor(author);
            }

            var result = _bookRepository.Add(book);

            if (await _bookRepository.UnitOfWork.SaveChangesAsync(cancellationToken) < 1)
            {
                return(new CreateBookCommandResult(false, new[] { "Error occured during saving Book" }));
            }


            var bookResult = _mapper.Map <CommandBookDto>(result);

            var bookResultEvent = _mapper.Map <CreateBook>(result);

            bookResultEvent.Language = result.LanguageId > 0
                ? _mapper.Map <LanguageDto>(await _languageRepository.FindByIdAsync(result.LanguageId ?? 0))
                : null;
            bookResultEvent.Publisher = result.LanguageId > 0
                ? _mapper.Map <PublisherDto>(await _publisherRepository.FindByIdAsync(result.PublisherId ?? 0))
                : null;


            var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri($"queue:{EventBusConstants.CreateBookQueue}"));

            await endpoint.Send(bookResultEvent, cancellationToken);

            return(new CreateBookCommandResult(true, bookResult));
        }
예제 #3
0
 public RedirectToRouteResult RemoveFromCart(Cart cart, int BookID, string returnUrl)
 {
     Domain.Book book = repository.Books.FirstOrDefault(b => b.BookID == BookID);
     if (book != null)
     {
         cart.RemoveLine(book);
     }
     return(RedirectToAction("Index", new { returnUrl }));
 }
예제 #4
0
        public async Task AddReview(Guid bookId, Review review)
        {
            Domain.Book book = await _repository.Get(bookId);

            if (book.Reviews == null)
            {
                book.Reviews = new List <Review>();
            }
            book.Reviews.Add(review);

            await _repository.Update(book);
        }
예제 #5
0
        public async Task <Unit> Handle(AddBookQuery request, CancellationToken cancellationToken)
        {
            var book = new Domain.Book
            {
                Title    = request.Title,
                AuthorId = request.AuthorId,
                Genre    = (Domain.Enums.Genres)request.Genre
            };

            _context.Books.Add(book);
            _context.SaveChanges();

            return(await Unit.Task);
        }
        public Domain.Book Add(Domain.Book book)
        {
            _logger.LogInfoCreateMethodStarted <Domain.Book>(BookRepositoryType, nameof(Add), new object[] { book });

            if (book is null)
            {
                _logger.LogWarningCreateMethodNullData <Domain.Book>(BookRepositoryType, nameof(Add));
            }

            var result = _bookRepository.Add(book);


            _logger.LogInfoCreateMethodEnded(BookRepositoryType, nameof(Add), result);
            return(result);
        }
예제 #7
0
        public void Can_AddNew_Lines()
        {
            Domain.Book b1 = new Domain.Book {
                BookID = 1, Title = "ASP.NET"
            };
            Domain.Book b2 = new Domain.Book {
                BookID = 2, Title = "Oracle"
            };
            Cart Target = new Cart();

            Target.AddItem(b1);
            Target.AddItem(b2, 3);
            CartLine[] result = Target.Lines.ToArray();
            Assert.AreEqual(result[0].Book, b1);
            Assert.AreEqual(result[1].Book, b2);
        }
예제 #8
0
        private async Task AddAuthorsToBookAsync(Domain.Book book, IEnumerable <string> authorsNames)
        {
            var authors = await _authorRepository.GetAllAsync();

            foreach (var name in authorsNames)
            {
                var authorThatExist = authors.SingleOrDefault(a => a.Name.FullName.Equals(name));
                if (authorThatExist is not null)
                {
                    book.AddAuthor(authorThatExist);
                    continue;
                }

                book.AddAuthor(new Author(new AuthorName(name)));
            }
        }
예제 #9
0
        public void AddItem(Domain.Book book, int quantity = 1)
        {
            CartLine line = lineCollection
                            .Where(b => b.Book.BookID == book.BookID)
                            .FirstOrDefault();

            if (line == null)
            {
                lineCollection.Add(new CartLine {
                    Book = book, Quantity = quantity
                });
            }
            else
            {
                line.Quantity += quantity;
            }
        }
예제 #10
0
        private async Task AddCategoriesToBookAsync(Domain.Book book,
                                                    IEnumerable <string> categoriesNames)
        {
            var categories = await _categoryRepository.GetAllAsync();

            foreach (var categoryName in categoriesNames)
            {
                var categoryThatExist = categories.SingleOrDefault(c => c.Name.Equals(categoryName));
                if (categoryThatExist is not null)
                {
                    book.AddCategory(categoryThatExist);
                    continue;
                }

                book.AddCategory(new Category(categoryName));
            }
        }
        public async Task <CreateBookCommandResponse> AddBookAsync(CreateBookCommand request)
        {
            var book = new Domain.Book
            {
                Name     = request.Name,
                AuthorId = request.AuthorId
            };

            await _bookDbContext.Books.AddAsync(book);

            await _bookDbContext.SaveChangesAsync();

            return(new CreateBookCommandResponse
            {
                Id = book.Id
            });
        }
예제 #12
0
        public async Task <CreateBookCommandResult> Handle(CreateBookCommand request, CancellationToken cancellationToken)
        {
            var language = await GetOrCreateLanguageAsync(request, cancellationToken);

            var publisher = await GetOrCreatePublisherAsync(request, cancellationToken);

            var book = new Domain.Book(request.Title,
                                       request.Description, request.Isbn10,
                                       request.Isbn13, language?.Id,
                                       publisher?.Id, request.PageCount,
                                       request.Visibility, request.PublishedDate);

            if (request.CategoriesNames is not null && request.CategoriesNames.Any())
            {
                await AddCategoriesToBookAsync(book, request.CategoriesNames.Distinct());
            }


            if (request.AuthorsNames is not null && request.AuthorsNames.Any())
            {
                await AddAuthorsToBookAsync(book, request.AuthorsNames);
            }

            var result = _bookRepository.Add(book);

            if (await _bookRepository.UnitOfWork.SaveChangesAsync(cancellationToken) < 1)
            {
                return(new CreateBookCommandResult(false, new[] { "Error occured during saving Book" }));
            }

            var bookResult = _mapper.Map <CommandBookDto>(result);

            var bookResultEvent = _mapper.Map <CreateBook>(result);

            bookResultEvent.Language = language is not null?_mapper.Map <LanguageDto>(language) : null;

            bookResultEvent.Publisher = publisher is not null?_mapper.Map <PublisherDto>(publisher) : null;


            var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri($"queue:{EventBusConstants.CreateBookQueue}"));

            await endpoint.Send(bookResultEvent, cancellationToken);

            return(new CreateBookCommandResult(true, bookResult));
        }
예제 #13
0
        public void Can_Add_Qty_for_Existing_Lines()
        {
            Domain.Book b1 = new Domain.Book {
                BookID = 1, Title = "ASP.NET"
            };
            Domain.Book b2 = new Domain.Book {
                BookID = 2, Title = "Oracle"
            };
            Cart Target = new Cart();

            Target.AddItem(b1);
            Target.AddItem(b2, 3);
            Target.AddItem(b1, 5);
            CartLine[] result = Target.Lines.OrderBy(cl => cl.Book.BookID).ToArray();
            Assert.AreEqual(result.Length, 2);
            Assert.AreEqual(result[0].Quantity, 6);
            Assert.AreEqual(result[1].Quantity, 3);
        }
예제 #14
0
        public Domain.Book Created(CreatedBookDTO createdBookDTO)
        {
            try
            {
                _bookRepository.Begin();

                string slug = _generateSlug.generate(createdBookDTO.Name);

                if (_bookRepository.CheckSlugCreated(slug))
                {
                    throw new Exception("Livro já cadastrado com esse nome!");
                }



                Domain.Book _bk = new Domain.Book();

                _bk.Photo = this.uploadPhoto(createdBookDTO.Photo, slug);

                _bk.Author           = createdBookDTO.Author;
                _bk.Slug             = slug;
                _bk.DescriptionLong  = createdBookDTO.DescriptionLong;
                _bk.DescriptionShort = createdBookDTO.DescriptionShort;
                _bk.Language         = createdBookDTO.Language;
                _bk.Name             = createdBookDTO.Name;
                _bk.Price            = createdBookDTO.Price;
                _bk.Publishing       = createdBookDTO.Publishing;
                _bk.QuantityPages    = createdBookDTO.QuantityPages;
                _bk.Weight           = createdBookDTO.Weight;
                _bk.Year             = createdBookDTO.Year;
                _bk.CreatdAt         = DateTime.Now;

                _bk = _bookRepository.Save(_bk);
                _bookRepository.Commit();
                return(_bk);
            }
            catch (Exception ex)
            {
                _bookRepository.RollBack();
                throw ex;
            }
        }
예제 #15
0
        public void Can_Clear_Contents()
        {
            Domain.Book b1 = new Domain.Book {
                BookID = 1, Title = "ASP.NET"
            };
            Domain.Book b2 = new Domain.Book {
                BookID = 2, Title = "Oracle"
            };
            Domain.Book b3 = new Domain.Book {
                BookID = 3, Title = "C#"
            };
            Cart Target = new Cart();

            Target.AddItem(b1);
            Target.AddItem(b2, 3);
            Target.AddItem(b3, 5);
            Target.AddItem(b2, 1);
            Target.Clear();
            Assert.AreEqual(Target.Lines.Count(), 0);
        }
예제 #16
0
        public void Calculate_Cart_Total()
        {
            Domain.Book b1 = new Domain.Book {
                BookID = 1, Title = "ASP.NET", Price = 100m
            };
            Domain.Book b2 = new Domain.Book {
                BookID = 2, Title = "Oracle", Price = 50m
            };
            Domain.Book b3 = new Domain.Book {
                BookID = 3, Title = "C#", Price = 70m
            };
            Cart Target = new Cart();

            Target.AddItem(b1, 1);
            Target.AddItem(b2, 2);
            Target.AddItem(b3);
            decimal result = Target.ComputeTotalValue();

            Assert.AreEqual(result, 270);
        }
예제 #17
0
        public void Can_Remove_Line()
        {
            Domain.Book b1 = new Domain.Book {
                BookID = 1, Title = "ASP.NET"
            };
            Domain.Book b2 = new Domain.Book {
                BookID = 2, Title = "Oracle"
            };
            Domain.Book b3 = new Domain.Book {
                BookID = 3, Title = "C#"
            };
            Cart Target = new Cart();

            Target.AddItem(b1);
            Target.AddItem(b2, 3);
            Target.AddItem(b3, 5);
            Target.AddItem(b2, 1);
            Target.RemoveLine(b2);
            Assert.AreEqual((Target.Lines.Where(cl => cl.Book == b2)).Count(), 0);
            Assert.AreEqual(Target.Lines.Count(), 2);
        }
예제 #18
0
        public Domain.Book Updated(int id, CreatedBookDTO createdBookDTO)
        {
            _bookRepository.Begin();

            try
            {
                Domain.Book _bk = _bookRepository.Get(new Object[] { id });

                string slug = _generateSlug.generate(createdBookDTO.Name);

                if (createdBookDTO.Photo != null)
                {
                    _bk.Photo = uploadPhoto(createdBookDTO.Photo, slug);
                }

                _bk.Slug             = slug;
                _bk.Author           = createdBookDTO.Author;
                _bk.DescriptionLong  = createdBookDTO.DescriptionLong;
                _bk.DescriptionShort = createdBookDTO.DescriptionShort;
                _bk.Language         = createdBookDTO.Language;
                _bk.Name             = createdBookDTO.Name;
                _bk.Price            = createdBookDTO.Price;
                _bk.Publishing       = createdBookDTO.Publishing;
                _bk.QuantityPages    = createdBookDTO.QuantityPages;
                _bk.Weight           = createdBookDTO.Weight;
                _bk.Year             = createdBookDTO.Year;

                _bookRepository.Save(_bk);

                _bookRepository.Commit();
                return(_bk);
            }
            catch (Exception ex)
            {
                _bookRepository.RollBack();
                throw ex;
            }
        }
예제 #19
0
        public void Delete(int id)
        {
            _bookRepository.Begin();

            try
            {
                Domain.Book _bk = _bookRepository.Get(new Object[] { id });

                if (_bk == null)
                {
                    throw new Exception("Livro informado não existe!");
                }

                //remover imagem do livro
                _bookRepository.Remove(_bk);
                _bookRepository.Commit();
            }
            catch (Exception ex)
            {
                _bookRepository.RollBack();
                throw ex;
            }
        }
예제 #20
0
 public async Task Add(Domain.Book book)
 {
     await _books.InsertOneAsync(book);
 }
예제 #21
0
        public Domain.Book Add(Domain.Book book)
        {
            var result = _bookContext.Books.Add(book).Entity;

            return(result);
        }
예제 #22
0
 public async Task Update(Domain.Book book)
 {
     await _books.ReplaceOneAsync(x => x.Id == book.Id, book);
 }
예제 #23
0
 public void RemoveLine(Domain.Book book)
 {
     lineCollection.RemoveAll(b => b.Book.BookID == book.BookID);
 }