Ejemplo n.º 1
0
        public IActionResult CreatePhotographer([FromBody] PhotographerForCreationDto photographer)
        {
            if (photographer == null)
            {
                return(BadRequest());
            }

            var photographerEntity = Mapper.Map <Photographer> (photographer);

            _magnumPhotosRepository.AddPhotographer(photographerEntity);

            if (!_magnumPhotosRepository.Save())
            {
                throw new Exception("Creating an photographer failed on save.");
            }

            var photographerToReturn = Mapper.Map <PhotographerDto> (photographerEntity);

            return(CreatedAtRoute("GetPhotographer",
                                  new { id = photographerToReturn.Id },
                                  photographerToReturn));
        }
Ejemplo n.º 2
0
        public IActionResult CreateBook(Guid photographerId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(BookForCreationDto),
                                         "The provided description should be different from the title.");
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (!_magnumPhotosRepository.PhotographerExists(photographerId))
            {
                return(NotFound());
            }

            var bookEntity = Mapper.Map <Book> (book);

            _magnumPhotosRepository.AddBook(photographerId, bookEntity);

            if (!_magnumPhotosRepository.Save())
            {
                throw new Exception($"Creating a book for photographer {photographerId} failed on save.");
            }

            var bookToReturn = Mapper.Map <BookDto> (bookEntity);

            return(CreatedAtRoute("GetBook",
                                  new { photographerId = photographerId, id = bookToReturn.Id },
                                  CreateLinksForBook(bookToReturn)));
        }