Esempio n. 1
0
        public async Task <PrintingEditionModel> AddAsync(AddOrUpdatePrintingEditionModel model)
        {
            if (model is null)
            {
                throw new ServerException(HttpStatusCode.BadRequest,
                                          ExceptionMessage.EMPTY_PRINTING_EDITION);
            }

            var printingEditionModel = _mapper.Map <PrintingEditionModel>(model);

            printingEditionModel.Authors = model.Authors
                                           .Select(x => new AuthorInPrintingEditionModel(new Guid(x), printingEditionModel.Id));

            var converted = ConvertCurrencyToUSD(printingEditionModel);

            var printingEdition = _mapper.Map <PrintingEdition>(converted);

            if (await _printingEditionRepository.ExistsAsync(printingEdition.Id))
            {
                throw new ServerException(HttpStatusCode.BadRequest,
                                          ExceptionMessage.PRINTING_EDITION_ALREADY_EXISTS);
            }

            var added = await _printingEditionRepository.AddAsync(printingEdition);

            var mapped = _mapper.Map <PrintingEditionModel>(added);

            return(mapped);
        }
Esempio n. 2
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);
        }