public async Task <bool> CreateProductAsync(CreateProductRequestBM createProductRequest)
        {
            using (IDbContextTransaction transaction = await dbContext.Database.BeginTransactionAsync()) {
                try {
                    await dbContext.Product.AddAsync(new Product()
                    {
                        Barcode        = createProductRequest.Barcode,
                        CategoryId     = createProductRequest.CategoryId,
                        ManufacturerId = createProductRequest.ManufacturerId
                    });

                    await dbContext.ProductName.AddRangeAsync(
                        createProductRequest.Name.Select(x => new ProductName()
                    {
                        Barcode       = createProductRequest.Barcode,
                        LanguageCode  = x.Key,
                        LocalizedName = x.Value
                    })
                        );

                    await dbContext.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception) {
                    transaction.Rollback();
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public override async Task <IActionResult> PostNewProduct(
            [FromRoute, Required] string barcode,
            [FromBody] PostProductRequestDto postProductRequestDto
            )
        {
            CreateProductRequestBM request = new CreateProductRequestBM(barcode, postProductRequestDto.ManufacturerId, postProductRequestDto.CategoryId, postProductRequestDto.ProductName);

            EmptyResultBM <CreateProductExplanation> result = await productService.CreateProductAsync(request);

            if (result.Successful)
            {
                return(new NoContentResult());
            }
            else
            {
                switch (result.Explanation)
                {
                case CreateProductExplanation.ProductWithBarcodeAlreadyExists:
                    return(new ApiErrorResult(
                               ErrorResultDto.ErrorCodeEnum.ProductAlreadyExistsEnum,
                               "A product with the specified barcode already exists.",
                               HttpStatusCode.Conflict
                               ));

                default:
                    return(new ApiGenericErrorResult());
                }
            }
        }
        public async Task <EmptyResultBM <CreateProductExplanation> > CreateProductAsync(CreateProductRequestBM createProductRequest)
        {
            if (await productDataManager.ProductExistsWithBarcodeAsync(createProductRequest.Barcode))
            {
                return(new EmptyResultBM <CreateProductExplanation>(CreateProductExplanation.ProductWithBarcodeAlreadyExists));
            }

            if (!await productDataManager.CreateProductAsync(createProductRequest))
            {
                return(new EmptyResultBM <CreateProductExplanation>(CreateProductExplanation.DatabaseError));
            }

            return(new EmptyResultBM <CreateProductExplanation>());
        }