Exemplo n.º 1
0
        public async Task <ActionResult <Dtos.PublicationHouseOutputModel> > GetPublicationHouse(Guid publicationHouseId)
        {
            var publicationHouse = await _publicationHouseRepository.GetAsync(publicationHouseId);

            if (publicationHouse == null)
            {
                return(NotFound());
            }

            return(Ok(_mapper.Map <Dtos.PublicationHouseOutputModel>(publicationHouse)));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <Dtos.PublicationOutputModel> > AddPublication(
            Dtos.PublicationInputModel publicationInputModel)
        {
            var publicationToInsert = _mapper.Map <Domain.Models.Publication>(publicationInputModel);

            publicationToInsert.Id = Guid.NewGuid();

            var authorIdsFromInput = publicationInputModel.AuthorIds;

            var publicationAuthors = new List <Domain.Models.PublicationAuthors>();

            foreach (var authorId in authorIdsFromInput)
            {
                var authorFromRepo = await _authorRepository.GetAsync(authorId);

                if (authorFromRepo == null)
                {
                    return(NotFound($"Author with id: {authorId} was not found"));
                }

                var publicationAuthor = new Domain.Models.PublicationAuthors()
                {
                    Author        = authorFromRepo,
                    AuthorId      = authorFromRepo.Id,
                    Publication   = publicationToInsert,
                    PublicationId = publicationToInsert.Id
                };

                publicationAuthors.Add(publicationAuthor);
            }

            var categoryIdsFromInput = publicationInputModel.CategoryIds;

            var publicationCategories = new List <Domain.Models.PublicationCategories>();

            foreach (var categoryId in categoryIdsFromInput)
            {
                var categoryFromRepo = await _categoryRepository.GetAsync(categoryId);

                if (categoryFromRepo == null)
                {
                    return(NotFound($"Category with id: {categoryId} was not found"));
                }

                var publicationCategory = new Domain.Models.PublicationCategories()
                {
                    Category      = categoryFromRepo,
                    CategoryId    = categoryId,
                    Publication   = publicationToInsert,
                    PublicationId = publicationToInsert.Id
                };

                publicationCategories.Add(publicationCategory);
            }

            var publicationHouseFromInput = await _publicationHouseRepository
                                            .GetAsync(publicationInputModel.PublicationHouseId);

            if (publicationHouseFromInput == null)
            {
                return(NotFound($"Publication House with id: {publicationHouseFromInput.Id} was not found"));
            }

            publicationToInsert.PublicationAuthors    = publicationAuthors;
            publicationToInsert.PublicationCategories = publicationCategories;
            publicationToInsert.PublicationHouseId    = publicationHouseFromInput.Id;

            _publicationRepository.Add(publicationToInsert);
            await _publicationRepository.SaveChangesAsync();

            var publicationOutput = await GetPublication(publicationToInsert.Id);

            return(CreatedAtRoute("GetPublication", new { publicationId = publicationToInsert.Id }
                                  , publicationOutput.Result));
        }