public async Task <BaseResponse <ProductDto> > Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <ProductDto>();

            var product = await _productRespository.GetByIdAsync(request.Id);

            if (product == null)
            {
                response.AddError("Product not found.", "404");
                return(response);
            }

            product.Name        = request.Name;
            product.Description = request.Description;
            product.Price       = request.Price;

            if (request.CategoryId != product.Category.Id)
            {
                var category = await _categoryRepository.GetByIdAsync(request.CategoryId);

                if (category == null)
                {
                    response.AddError("Category not found.", "404");
                    return(response);
                }

                product.Category = category;
            }


            await _productRespository.UpdateAsync(request.Id, product);

            response.Data = _mapper.Map <ProductDto>(product);
            return(response);
        }
Example #2
0
        public async Task <BaseResponse <ProductDto> > Handle(GetProductByIdRequest request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <ProductDto>();

            var product = await _productRespository.GetByIdAsync(request.Id);

            if (product == null)
            {
                response.AddError("Product not found.", "404");
                return(response);
            }

            response.Data = _mapper.Map <ProductDto>(product);
            return(response);
        }
        protected object CreateBadRequestResponse(ModelValidationResult validation)
        {
            BaseResponse <object> response = new BaseResponse <object>();

            response.StatusCode = System.Net.HttpStatusCode.BadRequest;
            response.IsSuccess  = false;

            foreach (var error in validation.Errors)
            {
                foreach (var errorMessage in error.Value)
                {
                    response.AddError(new ErrorItemResponse(errorMessage, error.Key));
                }
            }

            return(this.CreateJsonResponse(response));
        }
Example #4
0
        public async Task <BaseResponse <bool> > Handle(DeleteProductCommand request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <bool>();
            var product  = await _productRespository.GetByIdAsync(request.Id);

            if (product == null)
            {
                response.AddError("Product not found.", "404");
                return(response);
            }

            await _productRespository.SafeDeleteAsync(product);

            response.Data = true;

            return(response);
        }
        public BaseResponse <GetUserResponse> CreateUser(CreateUserRequest request)
        {
            BaseResponse <GetUserResponse> response = new BaseResponse <GetUserResponse>();

            var user = this.UserRepository.GetUser(request.Contacts.Email);

            if (user == null)
            {
                user = UserMapper.Map(request, this.ConfigurationUtility);
                this.UserRepository.CreateOrUpdateUser(user);
                response.SuccessBody = UserMapper.Map(user);
                response.StatusCode  = HttpStatusCode.Created;
                response.IsSuccess   = true;
            }
            else
            {
                response.StatusCode = HttpStatusCode.BadRequest;
                response.AddError(new ErrorItemResponse("email", "Email already registered"));
            }

            return(response);
        }
Example #6
0
        public BaseResponse <TransferResponse> Transfer(string userId, TransferRequest transferRequest)
        {
            var response = new BaseResponse <TransferResponse>();

            try
            {
                transferRequest.UserId = userId;
                TransferValidator transferValidator = new TransferValidator(_checkingAccountService);
                FluentValidation.Results.ValidationResult validationResult = transferValidator.Validate(transferRequest);

                if (!validationResult.IsValid)
                {
                    response.AddErrors(validationResult.Errors);
                    return(response);
                }

                var from = _checkingAccountService.UpdateBalance(transferRequest.NumberFrom, transferRequest.Amount * -1);
                var to   = _checkingAccountService.UpdateBalance(transferRequest.NumberTo, transferRequest.Amount);

                _transactionService.Add(new Transaction
                {
                    Amount = transferRequest.Amount,
                    FromCheckingAccountId = from.Id,
                    ToCheckingAccountId   = to.Id
                });

                _checkingAccountService.Commit();

                response.Data = new TransferResponse {
                    Balance = from.Balance
                };
            }
            catch (Exception ex)
            {
                _checkingAccountService.Rollback();
                response.AddError(1, "Erro inesperado");
            }
            return(response);
        }
Example #7
0
        public async Task <BaseResponse <ProductDto> > Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <ProductDto>();
            var category = await _categoryRepository.GetByIdAsync(request.CategoryId);

            if (category == null)
            {
                response.AddError("Category not found.", "404");
                return(response);
            }

            var product = _mapper.Map <Product>(request);

            product.Category  = category;
            product.CreatedAt = DateTime.Now;
            product.IsActive  = true;
            product.IsDeleted = false;

            await _productRespository.AddAsync(product);

            response.Data = _mapper.Map <ProductDto>(product);

            return(response);
        }
Example #8
0
 public InternalServerErrorObjectResult(BaseResponse response, string message) : base(response)
 {
     StatusCode = DefaultStatusCode;
     response.AddError(DefaultStatusCode, message);
     response.EndResponseTime = DateTimeHelper.Now();
 }
 public static BaseResponse <TResponse, TError> AddError <TResponse, TError>(this BaseResponse <TResponse, TError> baseResponse, Event error) where TError : ErrorResponse
 {
     return(baseResponse.AddError(error.ToErrorResponse <TError>()));
 }
 public static BaseResponse <TResponse> AddError <TResponse>(this BaseResponse <TResponse> baseResponse, Event error)
 {
     return(baseResponse.AddError(error.ToErrorResponse()));
 }
 public static BaseResponse <TResponse> AddError <TResponse>(this BaseResponse <TResponse> baseResponse, ValidationFailure error)
 {
     return(baseResponse.AddError(error.ToErrorResponse()));
 }