Exemple #1
0
        public async Task <PrintingEditionModel> UpdateEditionAsync(PrintingEditionItemModel model)
        {
            var printingEdition = await _editionRepository.GetEditionByTitle(model.Title, model.Id);

            if (printingEdition is null)
            {
                var editionToUpdate = _mapper.Map <PrintingEditionItemModel, PrintingEdition>(model);
                var editionUpdated  = await _editionRepository.UpdateEditionAsync(editionToUpdate);

                var authorsInPrintingEdition =
                    AuthorInPrintingEditionProvider.GetAuthorInPrintingEditionList(model.Authors,
                                                                                   editionUpdated.Id);

                await _authorInPeRepository.UpdateAuthorsInPrintingEditionAsync(authorsInPrintingEdition,
                                                                                editionUpdated.Id);

                printingEdition = await _editionRepository.GetEditionByIdAsync(editionUpdated.Id.ToString());

                var printingEditionModel = _mapper.Map <PrintingEdition, PrintingEditionModel>(printingEdition);

                return(printingEditionModel);
            }

            throw new ServerException(Constants.Errors.EDITION_ALREADY_EXIST, Enums.Errors.BadRequest);
        }
Exemple #2
0
        public async Task <PrintingEditionModel> CreateEditionAsync(PrintingEditionItemModel model)
        {
            var printingEdition = await _editionRepository.GetEditionByTitle(model.Title);

            if (printingEdition is null)
            {
                var editionToAdd = _mapper.Map <PrintingEditionItemModel, PrintingEdition>(model);
                var editionAdded = await _editionRepository.CreateEditionAsync(editionToAdd);

                if (model.Authors.Count > 1)
                {
                    var authorInPrintingEditions =
                        AuthorInPrintingEditionProvider.GetAuthorInPrintingEditionList(model.Authors, editionAdded.Id);

                    await _authorInPeRepository.AddAuthorsToPrintingEditionAsync(authorInPrintingEditions);
                }
                else
                {
                    var authorInPrintingEdition =
                        AuthorInPrintingEditionProvider.GetAuthorInPrintingEdition(editionAdded.Id, model.Authors[0]);

                    await _authorInPeRepository.AddAuthorToPrintingEditionAsync(authorInPrintingEdition);
                }

                printingEdition = await _editionRepository.GetEditionByIdAsync(editionAdded.Id.ToString());

                var printingEditionModel = _mapper.Map <PrintingEdition, PrintingEditionModel>(printingEdition);

                return(printingEditionModel);
            }

            throw new ServerException(Constants.Errors.EDITION_ALREADY_EXIST, Enums.Errors.BadRequest);
        }
Exemple #3
0
        public async Task <AuthorModel> UpdateAuthorAsync(AuthorItemModel model)
        {
            var author = await _authorRepository.GetAuthorByNameAsync(model.Name, model.Id);

            if (author is null)
            {
                var authorToUpdate = _mapper.Map <AuthorItemModel, Author>(model);
                var authorUpdated  = await _authorRepository.UpdateAuthorAsync(authorToUpdate);

                if (model.PrintingEditions.Count == 0)
                {
                    await _authorInPeRepository.RemoveInPrintingEditionByAuthorsAsync(authorUpdated.Id);
                }

                var authorInPrintingEdition =
                    AuthorInPrintingEditionProvider.GetAuthorInPrintingEditionList(authorUpdated.Id,
                                                                                   model.PrintingEditions);
                await _authorInPeRepository.UpdateInPrintingEditionByAuthorsAsync(authorInPrintingEdition,
                                                                                  authorUpdated.Id);

                author = await _authorRepository.GetAuthorByIdAsync(model.Id.ToString());

                var authorModel = _mapper.Map <Author, AuthorModel>(author);

                return(authorModel);
            }

            throw new ServerException(Constants.Errors.AUTHOR_ALREADY_EXIST, Enums.Errors.BadRequest);
        }