public async Task <ActionResult <Author> > PostAuthor(AddAuthorDTO author) { string[] writers = author.WriterName.Split(','); var book = new Book() { ISBN = author.ISBN }; _context.Attach(book); foreach (var w in writers) { var writer = new Writer() { WriterName = w.Trim() }; _context.Attach(writer); var newAuthor = new Author { Book = book, Writer = writer }; _context.Author.Add(newAuthor); } await _context.SaveChangesAsync(); return(NoContent()); }
public bool Add(AddAuthorDTO authorDto) { var author = GetAuthorByFirstNameAndLastName(authorDto.FirstName, authorDto.LastName); if (author == null) { this.db.Authors.Add(new Author() { FirstName = authorDto.FirstName, LastName = authorDto.LastName, Nationality = authorDto.Nationality }); } int count = this.db.SaveChanges(); return(count != 0); }
public void AddAuthor_WithCorrectData_ShouldSuccessfullyAddAuthor() { string errorMessagePrefix = "AuthorService Add() method does not work properly."; var context = LibraaryDbContextInMemoryFactory.InitializeContext(); this.authorService = new AuthorService(context); AddAuthorDTO author = new AddAuthorDTO() { FirstName = "Test1", LastName = "Testov", Nationality = "Test" }; bool actualResult = this.authorService.Add(author); Assert.True(actualResult, errorMessagePrefix); }
public void AddAuthor_WithAuthorWhichIsAlreadyInDb_ShouldNotAddAuthor() { string errorMessagePrefix = "AuthorService Add() method does not work properly."; var context = LibraaryDbContextInMemoryFactory.InitializeContext(); this.authorService = new AuthorService(context); LibraaryDbContextInMemoryFactory.SeedDb(context); AddAuthorDTO author = new AddAuthorDTO() { FirstName = "Test", LastName = "Testov", Nationality = "Test" }; bool actualResult = this.authorService.Add(author); Assert.True(!actualResult, errorMessagePrefix); }
public IActionResult AddAuthor([FromBody] AddAuthorDTO addAuthorDTO) { if (ModelState.IsValid) { AddAuthorCommand addBookCommand = new AddAuthorCommand(addAuthorDTO.Firstname, addAuthorDTO.Lastname); try { var result = _messages.Dispatch(addBookCommand); return(FromResult(result)); } catch (DomainException ex) { _logger.LogError(ex.Message); return(Error(ex.Message)); } catch (Exception ex) { _logger.LogCritical(ex.Message); return(StatusCode(500)); } } return(BadRequest()); }