public Editions CreateEdition(Editions editions)
 {
     if (Isunique(editions.Name))
     {
         return(editionRepository.CreateEdition(editions));
     }
     return(null);
 }
Beispiel #2
0
        public ActionResult <Dto.Book> CreateEdition(Dto.EditionRequest request)
        {
            var       edition   = _mapper.Map <Edition>(request);
            var       book      = _bookRepository.GetBook(request.BookId);
            Publisher publisher = null;

            if (book == null)
            {
                return(BadRequest("Book not found"));
            }

            if (request.PublisherName != null)
            {
                string publisherName = request.PublisherName;
                publisher = _publisherRepository.GetPublisher(publisherName);
                if (publisher == null)
                {
                    Publisher newPublisher = new Publisher
                    {
                        Name = request.PublisherName
                    };
                    _publisherRepository.CreatePublisher(newPublisher);
                    if (_publisherRepository.SaveChanges() == false)
                    {
                        return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
                    }
                    publisher = _publisherRepository.GetPublisher(publisherName);
                }
            }

            _repository.CreateEdition(edition, book, publisher);

            if (_repository.SaveChanges() == false)
            {
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }

            return(CreatedAtAction("GetEdition", new { Id = edition.Id }, _mapper.Map <Dto.Edition>(edition)));
        }