示例#1
0
        public Task <ProductUiModel> CreateProductAsync(ProductForCreationUiModel newProductUiModel)
        {
            var response =
                new ProductUiModel()
            {
                Message = "START_CREATION"
            };

            if (newProductUiModel == null)
            {
                response.Message = "ERROR_INVALID_PRODUCT_MODEL";
                return(Task.Run(() => response));
            }

            var productToBeCreated = new Product();

            try
            {
                productToBeCreated.InjectWithInitialAttributes(newProductUiModel.ProductName,
                                                               newProductUiModel.ProductDescription, newProductUiModel.ProductPrice);

                ThrowExcIfProductCannotBeCreated(productToBeCreated);
                ThrowExcIfThisProductAlreadyExist(productToBeCreated);

                Log.Debug(
                    $"Create Product: {newProductUiModel.ProductName}" +
                    "--CreateProduct--  @NotComplete@ [CreateProductProcessor]. " +
                    "Message: Just Before MakeItPersistence");

                MakeProductPersistent(productToBeCreated);

                Log.Debug(
                    $"Create Product: {newProductUiModel.ProductName}" +
                    "--CreateProduct--  @NotComplete@ [CreateProductProcessor]. " +
                    "Message: Just After MakeItPersistence");
                response         = ThrowExcIfProductWasNotBeMadePersistent(productToBeCreated);
                response.Message = "SUCCESS_CREATION";
            }
            catch (InvalidProductException e)
            {
                response.Message = "ERROR_INVALID_Product_MODEL";
                Log.Error(
                    $"Create Product: {newProductUiModel.ProductName}" +
                    "--CreateProduct--  @NotComplete@ [CreateProductProcessor]. " +
                    $"Broken rules: {e.BrokenRules}");
            }
            catch (ProductAlreadyExistsException ex)
            {
                response.Message = "ERROR_Product_ALREADY_EXISTS";
                Log.Error(
                    $"Create Product: {newProductUiModel.ProductName}" +
                    "--CreateProduct--  @fail@ [CreateProductProcessor]. " +
                    $"@innerfault:{ex?.Message} and {ex?.InnerException}");
            }
            catch (ProductDoesNotExistAfterMadePersistentException exx)
            {
                response.Message = "ERROR_Product_NOT_MADE_PERSISTENT";
                Log.Error(
                    $"Create Product: {newProductUiModel.ProductName}" +
                    "--CreateProduct--  @fail@ [CreateProductProcessor]." +
                    $" @innerfault:{exx?.Message} and {exx?.InnerException}");
            }
            catch (Exception exxx)
            {
                response.Message = "UNKNOWN_ERROR";
                Log.Error(
                    $"Create Product: {newProductUiModel.ProductName}" +
                    $"--CreateProduct--  @fail@ [CreateProductProcessor]. " +
                    $"@innerfault:{exxx.Message} and {exxx.InnerException}");
            }

            return(Task.Run(() => response));
        }
示例#2
0
        public Task <ProductUiModel> UpdateProductAsync(Guid productIdToBeModified, ProductForModificationUiModel updatedProduct)
        {
            var response =
                new ProductUiModel()
            {
                Message = "SUCCESS_MODIFICATION"
            };

            if (updatedProduct == null)
            {
                response.Message = "ERROR_INVALID_PRODUCT_MODEL";
                return(Task.Run(() => response));
            }

            if (productIdToBeModified == Guid.Empty)
            {
                response.Message = "ERROR_INVALID_PRODUCT_ID";
                return(Task.Run(() => response));
            }

            try
            {
                var productToBeModified = ThrowExceptionIfProductDoesNotExist(productIdToBeModified);

                productToBeModified.ModifyWith(updatedProduct.ProductName,
                                               updatedProduct.ProductDescription, updatedProduct.ProductPrice);

                Log.Debug(
                    $"Update-Modify Product: with Id: {productIdToBeModified}" +
                    "--UpdateProduct--  @Ready@ [UpdateProductAsync]. " +
                    "Message: Just Before MakeItPersistence");

                MakeProductPersistent(productToBeModified);

                Log.Debug(
                    $"Update-Modify Product: with Id: {productIdToBeModified}" +
                    "--UpdateProduct--  @Ready@ [UpdateProductAsync]. " +
                    "Message: Just After MakeItPersistence");

                response         = ThrowExcIfModificationWasNotBeMadePersistent(productIdToBeModified);
                response.Message = "SUCCESS_MODIFICATION";
            }
            catch (ProductDoesNotExistException e)
            {
                response.Message = "ERROR_PRODUCT_NOT_EXIST";
                Log.Error(
                    $"Modify Product: {updatedProduct.ProductName}" +
                    "--ModifyProductAsync-- @fail@ [UpdateProductProcessor]." +
                    $" @innerfault:{e?.Message} and {e?.InnerException}");
            }
            catch (InvalidProductException e)
            {
                response.Message = "ERROR_INVALID_PRODUCT_MODEL";
                Log.Error(
                    $"Modify Product: {updatedProduct.ProductName}" +
                    "--ModifyProductAsync--  @fail@ [UpdateProductProcessor]." +
                    $" @innerfault:{e?.Message} and {e?.InnerException}");
            }
            catch (ProductDoesNotExistAfterMadePersistentException e)
            {
                response.Message = "ERROR_MODIFICATION_NOT_MADE_PERSISTENT";
                Log.Error(
                    $"Modify Product: {updatedProduct.ProductName}" +
                    "--ModifyProductAsync--  @fail@ [UpdateProductProcessor]." +
                    $" @innerfault:{e?.Message} and {e?.InnerException}");
            }
            catch (Exception e)
            {
                response.Message = "UNKNOWN_ERROR";
                Log.Error(
                    $"Modify Product: {updatedProduct.ProductName}" +
                    "--ModifyProductAsync--  @fail@ [UpdateProductProcessor]." +
                    $" @innerfault:{e?.Message} and {e?.InnerException}");
            }


            return(Task.Run(() => response));
        }