Beispiel #1
0
        public async Task AddAsync(PrintingEditionModel model)
        {
            if (string.IsNullOrEmpty(model.Title))
            {
                model.Errors.Add(ExceptionsInfo.InvalidTitle);
            }

            if (string.IsNullOrEmpty(model.Description))
            {
                model.Errors.Add(ExceptionsInfo.InvalidDescription);
            }

            if (model.Type == PrintingEditionType.None)
            {
                model.Errors.Add(ExceptionsInfo.InvalidPrintingEditionType);
            }

            if (model.Currency == CurrencyType.None)
            {
                model.Errors.Add(ExceptionsInfo.InvalidCurrencyType);
            }

            if (model.Price == default)
            {
                model.Errors.Add(ExceptionsInfo.InvalidPrice);
            }

            if (model.Authors is null)
            {
                model.Errors.Add(ExceptionsInfo.InvalidAuthor);
            }

            if (model.Errors.Any())
            {
                throw new UserException(HttpStatusCode.BadRequest, model.Errors);
            }

            model.Price    = _exchangeRateProvider.ExchangeToUSD(model.Price, model.Currency);
            model.Currency = CurrencyType.USD;

            List <Author> authors = await _authorRepository.GetByNamesAsync(model.Authors);

            var printingEdition = _printingEditionMapper.Map(model);
            await _printingEditionRepository.AddAsync(printingEdition);

            var authorInPrintingEditions = authors.Select(item => new AuthorInPrintingEdition
            {
                AuthorId          = item.Id,
                PrintingEditionId = printingEdition.Id
            }).ToList();
            await _authorInPrintingEditionRepository.AddRangeAsync(authorInPrintingEditions);
        }
Beispiel #2
0
        private async Task AddToAuthorsAsync(Guid printingEditionId, List <AuthorModel> authorModels)
        {
            var authors = ListMapper <Author, AuthorModel> .Map(authorModels);

            var authorInPrintingEditions = new List <AuthorInPrintingEdition>();

            authorInPrintingEditions.AddRange
            (
                authors.Select(author => new AuthorInPrintingEdition
            {
                AuthorId          = author.Id,
                PrintingEditionId = printingEditionId
            })
            );

            await _authorInPrintingEditionRepository.AddRangeAsync(authorInPrintingEditions);
        }