public async Task CreateBookAsync_WithIncorrectModel_ReturnsNull() { // Arrange var context = this.NewInMemoryDatabase(); var serviceModel = new BookCreateServiceModel { Title = "TestTitle", Author = "TestAuthor", Price = -300, // Invalid Price Year = 2000, Description = "TestDescription", ShortDescription = "TestShortDescription", Pages = 1, ImageUrl = "http://example.com/favicon.ico", Isbn = "0000005000000", PublisherId = Guid.NewGuid().ToString() }; var booksService = new BooksService(context); // Act var createdId = await booksService.CreateBookAsync(serviceModel); // Assert Assert.Null(createdId); Assert.False(await context.Books.AnyAsync()); }
public async Task CreateBookAsync_WithCorrectModel_WorksCorrectly() { // Arrange var context = this.NewInMemoryDatabase(); var booksService = new BooksService(context); var serviceModel = new BookCreateServiceModel { Title = "TestTitle", Author = "TestAuthor", Price = 1, Year = 2000, Description = "TestDescription", ShortDescription = "TestShortDescription", Pages = 1, ImageUrl = "http://example.com/favicon.ico", Isbn = "0000005000000", PublisherId = Guid.NewGuid().ToString(), DownloadUrl = "http://example.com/download.ico" }; // Act var createdId = await booksService.CreateBookAsync(serviceModel); // Assert Assert.NotNull(createdId); Assert.Equal(1, await context.Books.CountAsync()); Assert.True(await context.Books.AnyAsync(b => b.Id == createdId)); }
public async Task <string> CreateBookAsync(BookCreateServiceModel serviceBook) { if (!this.IsEntityStateValid(serviceBook)) { return(null); } var dbBook = Mapper.Map <Book>(serviceBook); await this.Context.Books.AddAsync(dbBook); await this.Context.SaveChangesAsync(); return(dbBook.Id); }
public async Task <IActionResult> Create(BookCreateServiceModel model, IFormFile coverPicture) { byte[] pictureContents = null; if (coverPicture != null) { if (!(coverPicture.FileName.EndsWith(".jpg") || coverPicture.FileName.EndsWith(".png") || coverPicture.FileName.EndsWith(".gif")) || coverPicture.Length > PictureSize) { ModelState.AddModelError(string.Empty, "Снимката трябва да е с разширение: \".zip\", \".png\" или \".gif\", как и да е с размер до 2 MB."); return(View(model)); } pictureContents = await coverPicture.ToByteArrayAsync(); } if (!ModelState.IsValid) { return(View(model)); } var traderId = this.userManager.GetUserId(User); var bookId = await this.books .CreateAsync( model.Title, model.AuthorNames, model.PublisherName, model.Category, model.PublicationYear, model.Price, model.Condition, model.Language, pictureContents, model.Coverage, model.Description, traderId ); TempData.AddSuccessMessage($"Книгата със заглавие {model.Title} е добавена успешно."); return(RedirectToAction(nameof(ItemsController.Details), new { controller = "Items", Id = bookId })); }