Ejemplo n.º 1
0
        internal async Task <bool> ValidateToSave(ProductUpdateInputDto _request, string _culture, bool _isUpdate)
        {
            #region Product Input Validation
            if (_request == null)
            {
                throw new ValidationsException(_messageResource.InvalidId(_culture));
            }
            if (_isUpdate && _request.Id <= default(int))
            {
                throw new ValidationsException(_messageResource.InvalidRequest(_culture));
            }

            if (_isUpdate && _request.Id >= default(int))
            {
                if (await GetItemIfValid(_request.Id, _culture) == null)
                {
                    throw new ValidationsException(_messageResource.ProductNotFound(_culture));
                }
            }
            if (string.IsNullOrEmpty(_request.Name))
            {
                throw new ValidationsException(_messageResource.InvalidName(_culture));
            }
            if (string.IsNullOrEmpty(_request.Photo))
            {
                throw new ValidationsException(_messageResource.InvalidPhoto(_culture));
            }
            if (_request.Price == 0)
            {
                throw new ValidationsException(_messageResource.InvalidPrice(_culture));
            }


            #endregion

            #region DB Validation

            if (await _productRepository.GetAnyAsync(us => us.Name == _request.Name &&
                                                     us.Id != _request.Id))
            {
                throw new ValidationsException(_messageResource.NameAlreadyExisit(_culture));
            }

            #endregion

            return(true);
        }
        public async Task <bool> HandleUseCase(ProductUpdateInputDto _request, IOutputPort <ResultDto <bool> > _presenter)
        {
            ProductSharedMethods productSharedMethod = new ProductSharedMethods(MessageResource, ProductRepository);

            //validate Updated Input Request Product
            await productSharedMethod.ValidateToSave(Mapper.Map <ProductUpdateInputDto>(_request), _culture, true);

            //validate Founded Product or not
            Entities.Product oldProduct = await productSharedMethod.GetItemIfValid(_request.Id, _culture);

            //mappind the founded Product to prepare update it in DB
            if (_request.Photo != null && !_request.Photo.Contains("http"))
            {
                oldProduct.Photo = new FileOperation(Configuration).SaveFile(new SharedKernal.Files.Dto.FileDto {
                    File = _request.Photo, Name = _request.Name
                });
            }
            else
            {
                _request.Photo = oldProduct.Photo;
            }

            oldProduct = Mapper.Map(_request, oldProduct);

            #region Save The New Files Physically
            #endregion
            oldProduct.LastUpdated = DateTime.Now;
            //Update Product Entity
            ProductRepository.Update(oldProduct);

            //Commit Product Entity
            await UnitOfWork.Commit(_culture);

            _presenter.HandlePresenter(new ResultDto <bool>(true));
            return(true);
        }