Example #1
0
        public IEnumerable <ProductDto> Invoke(BusinessKeyDto input)
        {
            _logger.Log($"Starting {GetType().FullName}");
            _logger.Log("Input model received: ", input, LogType.Debug);

            var businessKey = BusinessMapper.ToBusinessKey(input);
            var business    = _businessRepository.Find(businessKey);

            if (business == null)
            {
                var error = new BusinessDoesNotExists(businessKey);
                _logger.Log(error.Message, businessKey, LogType.Error);
                throw error;
            }

            var products = _productRepository.FindByBusiness(businessKey);

            var output = new List <ProductDto>(products.Select(product => new ProductDto()
            {
                BusinessCode       = business.Code,
                ProductCode        = product.Code,
                ProductName        = product.Name,
                ProductDescription = product.Description
            }));

            _logger.Log("Output model to return: ", output, LogType.Debug);
            return(output);
        }
Example #2
0
        public CreatedDto Invoke(ProductDto input)
        {
            _logger.Log($"Starting {GetType().FullName}");
            _logger.Log("Input model received: ", input, LogType.Debug);

            #region Business rules example

            var product  = ProductMapper.ToProduct(input);
            var business = _businessRepository.Find(product.BusinessKey);
            if (business == null)
            {
                var error = new BusinessDoesNotExists(product.BusinessKey);
                _logger.Log(error.Message, product.BusinessKey, LogType.Error);
                throw error;
            }

            // Validations
            var exists = _repository.FindByCodeOrName(product.BusinessKey, product.Code, product.Name);
            if (exists != null)
            {
                var error = new ProductAlreadyExists(product);
                _logger.Log(error.Message, product, LogType.Error);
                throw error;
            }

            #endregion

            #region Persistence

            var created = _repository.Insert(product);
            var message = created
                ? $"Product with Code {product.Code} was created successfully by _repository type: {_repository.GetType()}!"
                : $"Product wasn't created... The function _repository.Create(product) returned false. _repository type: {_repository.GetType()}.";
            _logger.Log(message);

            // To return
            var output = new CreatedDto()
            {
                Created = created
            };
            _logger.Log("Output model to return: ", output, LogType.Debug);
            return(output);

            #endregion
        }