コード例 #1
0
        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}"));
            }
        }
コード例 #2
0
        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");
        }
コード例 #3
0
        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;
            }
        }
コード例 #4
0
        public Guid Insert(BookRequestDTO request)
        {
            var entity = Mapper.Map <BookEntity>(request);

            _repository.Insert(entity);

            return(entity.Id);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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();
        }
コード例 #7
0
 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));
     }
 }
コード例 #8
0
        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("");
        }
コード例 #9
0
        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();
        }
コード例 #10
0
 public IActionResult Update(Guid id, BookRequestDTO bookRequestDTO)
 {
     _bookService.Update(id, bookRequestDTO);
     return(Ok());
 }
コード例 #11
0
 public IActionResult Create(BookRequestDTO bookRequestDTO)
 {
     _bookService.Create(bookRequestDTO);
     return(Ok());
 }
コード例 #12
0
        public IActionResult Update(Guid?id, BookRequestDTO request)
        {
            _bookBll.Update(id.GetValueOrDefault(), request);

            return(NoContent());
        }
コード例 #13
0
        public IActionResult Insert(BookRequestDTO request)
        {
            var id = _bookBll.Insert(request);

            return(CreatedAtAction(nameof(Insert), new { id }));
        }
コード例 #14
0
ファイル: Book.cs プロジェクト: alexchirea/ProiectDAW-Library
 public Book(BookRequestDTO bookRequestDTO)
 {
     Title    = bookRequestDTO.Title;
     noCopies = bookRequestDTO.noCopies;
     AuthorId = bookRequestDTO.AuthorId;
 }
コード例 #15
0
        public string Update(int bookId, BookRequestDTO book)
        {
            var message = _bookRepository.Update(bookId, book);

            return(message);
        }
コード例 #16
0
        public string Add(BookRequestDTO book)
        {
            var message = _bookRepository.Add(book);

            return(message);
        }