public async Task <IActionResult> Create([FromBody] BookRequestDTO bookRequestDto) { _logger.LogInfo("Creating Book"); try { if (bookRequestDto == null) { return(BadRequest("Input data is null")); } if (!ModelState.IsValid) { return(BadRequest("Mandatory fields missing")); } var bookDto = _mapper.Map <BookDTO>(bookRequestDto); var result = await _bookRepository.Create(_mapper.Map <Book>(bookDto)); _logger.LogInfo(result ? $"Author {bookRequestDto.Title} created" : $"Author {bookRequestDto.Title} failed"); return(Created("Created book successfully", new { bookRequestDto })); } catch (Exception ex) { return(BadRequest( $"Exception Occurred: {ex.Message} {Environment.NewLine} {ex.InnerException} {Environment.NewLine}{ex.StackTrace}")); } }
public string Update(int bookId, BookRequestDTO book) { var titleExists = _context.Books.Any(book => book.Title.ToLower().Trim() == book.Title.ToLower().Trim()); if (titleExists) { return("Não foi possível alterar o livro. Este título já existe na base de dados."); } var bookEntity = _context.Books .Include(book => book.BookWriters) .Where(book => book.Id == bookId) .FirstOrDefault(); if (bookEntity != null) { bookEntity.ReleaseDate = book.ReleaseDate; bookEntity.Title = book.Title; bookEntity.BookWriters = book.WritersId.Select(writerId => new BookWriter() { BookId = bookId, WriterId = writerId }).ToList(); _context.Update <Book>(bookEntity); _context.SaveChanges(); return(""); } return("Livro não encontrado na base de dados"); }
public async Task <IActionResult> Put(int id, [FromBody] BookRequestDTO bookRequestDto) { _logger.LogInfo("Updating Book"); try { if (bookRequestDto == null) { return(BadRequest("Input data is null")); } if (!ModelState.IsValid) { return(BadRequest("Mandatory fields missing")); } var bookDto = _mapper.Map <BookDTO>(bookRequestDto); bookDto.Bookid = id; var result = await _bookRepository.Update(_mapper.Map <Book>(bookDto)); _logger.LogInfo($"Book {bookDto.Title} updated"); return(Ok($"Updated book {bookDto.Title} successfully")); } catch (Exception ex) { Console.WriteLine(ex); throw; } }
public Guid Insert(BookRequestDTO request) { var entity = Mapper.Map <BookEntity>(request); _repository.Insert(entity); return(entity.Id); }
public void Update(Guid id, BookRequestDTO request) { var entity = _repository.Get(id) .SingleOrDefault() ?? throw new EntityNotFoundException($"Book ({id})"); Mapper.Map(request, entity); _repository.Update(entity); }
public void Create(BookRequestDTO bookRequest) { Author author = _authorRepository.FindById(bookRequest.AuthorId); if (author == null) { throw new NotSupportedException(); } Book book = new Book(bookRequest); book.Author = author; _bookRepository.Create(book); _bookRepository.Save(); }
public IActionResult Update(int bookId, [FromBody] BookRequestDTO book) { try { var message = _bookService.Update(bookId, book); if (string.IsNullOrEmpty(message)) { return(Ok("Livro alterado com sucesso!")); } else { return(BadRequest(message)); } } catch (Exception ex) { return(BadRequest(ex.InnerException.Message)); } }
public string Add(BookRequestDTO book) { var titleExists = _context.Books.Any(book => book.Title.ToLower().Trim() == book.Title.ToLower().Trim()); if (titleExists) { return("Não foi possível cadastrar o livro. Este título já existe na base de dados."); } var bookEntity = new Book { Title = book.Title, RegistrationDate = DateTime.Now, ReleaseDate = book.ReleaseDate, BookWriters = book.WritersId.Select(id => new BookWriter() { WriterId = id }).ToList() }; _context.Add <Book>(bookEntity); _context.SaveChanges(); return(""); }
public void Update(Guid id, BookRequestDTO bookRequest) { Book b = _bookRepository.FindById(id); if (bookRequest.AuthorId != null) { Author author = _authorRepository.FindById(bookRequest.AuthorId); if (author == null) { throw new NotSupportedException(); } b.Author = author; } if (bookRequest.Title != null) { b.Title = bookRequest.Title; } _bookRepository.Update(b); _bookRepository.Save(); }
public IActionResult Update(Guid id, BookRequestDTO bookRequestDTO) { _bookService.Update(id, bookRequestDTO); return(Ok()); }
public IActionResult Create(BookRequestDTO bookRequestDTO) { _bookService.Create(bookRequestDTO); return(Ok()); }
public IActionResult Update(Guid?id, BookRequestDTO request) { _bookBll.Update(id.GetValueOrDefault(), request); return(NoContent()); }
public IActionResult Insert(BookRequestDTO request) { var id = _bookBll.Insert(request); return(CreatedAtAction(nameof(Insert), new { id })); }
public Book(BookRequestDTO bookRequestDTO) { Title = bookRequestDTO.Title; noCopies = bookRequestDTO.noCopies; AuthorId = bookRequestDTO.AuthorId; }
public string Update(int bookId, BookRequestDTO book) { var message = _bookRepository.Update(bookId, book); return(message); }
public string Add(BookRequestDTO book) { var message = _bookRepository.Add(book); return(message); }