Example #1
0
        public async Task CreateAsync(PrintingEditionModel model)

        {
            var editions = await _printingEditionRepository.GetAsync(edition => edition.Description == model.Description);

            if (editions.Any())
            {
                throw new CustomExeption(Constants.Error.EDITION_EXISTS_DB,
                                         StatusCodes.Status400BadRequest);
            }

            if (!model.AuthorModels.Any())
            {
                throw new CustomExeption(Constants.Error.EDITION_MUST_HAVE_AUTHOR,
                                         StatusCodes.Status400BadRequest);
            }

            var allAuthors = await _authorRepository.GetAllAsync();

            var autors = allAuthors.Where(a => model.AuthorModels.Exists(p => p.Id == a.Id)).ToList();

            if (model.AuthorModels.Count != autors.Count)
            {
                throw new CustomExeption(Constants.Error.NO_AUTHOR_ID_IN_DB_ADD_AUTHOR_FIRST,
                                         StatusCodes.Status400BadRequest);
            }

            var printingEdition = _mapper.Map <PrintingEdition>(model);
            await _printingEditionRepository.CreateAsync(printingEdition);
        }
Example #2
0
        public async Task <PrintingEditionModel> CreateAsync(PrintingEditionModel printingEditionModel)
        {
            var printingEdition = _printingEditionMapper.Map(printingEditionModel);

            printingEdition = await _printingEditionRepository.CreateAsync(printingEdition);

            if (printingEditionModel.Authors.Any())
            {
                await AddToAuthorsAsync(printingEdition.Id, printingEditionModel.Authors);
            }
            printingEditionModel = _printingEditionModelMapper.Map(printingEdition);
            return(printingEditionModel);
        }
Example #3
0
        public async Task <PrintingEditionModel> CreatePrintingEditionAsync(PrintingEditionModel model)
        {
            var existsPrintingEdition = await _printingEditionRepository.GetPrintingEditionByTitleAsync(model.Title);

            if (existsPrintingEdition is not null)
            {
                throw new ServerException(Constants.Errors.ALREADY_EXISTS);
            }

            var printingEdition = _autoMapper.Map <PrintingEdition>(model);
            await _printingEditionRepository.CreateAsync(printingEdition);

            await _authorInPrinting.AddAuthorToPEAsync(model.AuthorsId, printingEdition.Id);

            var printingEditionModel = _autoMapper.Map <PrintingEditionModel>(printingEdition);

            return(printingEditionModel);
        }
Example #4
0
        public async Task <PrintingEditionModel> CreatePrintingEditionAsync(PrintingEditionModelItem printingEditionsModelItem)
        {
            var responseModel = ValidateData(printingEditionsModelItem);

            if (responseModel.Errors.Any())
            {
                return(responseModel);
            }

            var printingEdition = _mapperHelper.Map <PrintingEditionModelItem, PrintingEdition>(printingEditionsModelItem);

            printingEdition.Price = _currencyConverterHelper.Convert(printingEditionsModelItem.Currency, Enums.Currency.USD, printingEditionsModelItem.Price);

            printingEdition.Currency = Enums.Currency.USD;

            var authorsId = printingEditionsModelItem.Authors.Select(x => x.Id).ToArray();

            if (authorsId == null || !authorsId.Any())
            {
                responseModel.Errors.Add(Constants.Errors.InvalidData);
                return(responseModel);
            }

            var createResult = await _printingEditionRepository.CreateAsync(printingEdition);

            if (createResult.Equals(0))
            {
                responseModel.Errors.Add(Constants.Errors.FailedCreate);
                return(responseModel);
            }

            var createAuthorsInPrintingEditionResult = await _authorInPrintingEditionRepository.CreateAuthorsInPrintingEditionAsync(printingEdition.Id, authorsId);

            if (createAuthorsInPrintingEditionResult.Equals(0))
            {
                responseModel.Errors.Add(Constants.Errors.FailedCreate);
                return(responseModel);
            }

            return(responseModel);
        }
Example #5
0
        public async Task <BaseModel> CreateAsync(PrintingEditionsModelItem model)
        {
            var response = new BaseModel();

            if (model == null)
            {
                response.Errors.Add(ErrorConstants.ModelIsNull);
                return(response);
            }
            var changedPrice = _currencyConverter.Convert(model.Currency, CurrencyEnum.Currency.USD, model.Price);
            var product      = PrintingEditionMapper.MapModelToEntity(model, changedPrice);
            var result       = await _printingEditionRepository.CreateAsync(product);

            if (!result)
            {
                response.Errors.Add(ErrorConstants.ImpossibleToCreateProduct);
            }
            response = await CreateAuthorInProductAsync(product, model.Authors);

            return(response);
        }
        public async Task <BaseModel> CreateAsync(PrintingEditionModelItem printingEditionModel)
        {
            var printingEdition = new PrintingEdition();

            printingEdition = printingEditionModel.MapToEntity(printingEdition);
            var printingEditionId = await _printingEditionRepository.CreateAsync(printingEdition);

            if (printingEditionId == 0)
            {
                printingEditionModel.Errors.Add(Constants.Errors.CreatePrintingEditionError);
                return(printingEditionModel);
            }

            var authorInPrintingEditions = printingEditionModel.Authors.MapToAuthorInPrintingEditionList(printingEditionId).ToList();
            var result = await _authorInPrintingEditionRepository.CreateListAsync(authorInPrintingEditions);

            if (!result && authorInPrintingEditions.Count > 0)
            {
                printingEditionModel.Errors.Add(Constants.Errors.CreatePrintingEditionError);
            }

            return(printingEditionModel);
        }