public Task <IActionResult> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
        {
            IActionResult response = new BadRequestResult();

            try
            {
                var validatorResults = _createValidator.Validate(request);

                if (!validatorResults.IsValid)
                {
                    var errors = validatorResults.Errors.Select(f => f.ErrorMessage);
                    var output = ApiResultHelper.GenerateErrorResponse(HttpStatusCode.UnprocessableEntity, errors);

                    response = new UnprocessableEntityObjectResult(output);
                }
                else
                {
                    var entity = _mapper.Map <Category>(request);
                    var result = _repository.Save(entity).Result;

                    response = new OkObjectResult(ApiResultHelper.GenerateResponse(HttpStatusCode.OK, result));
                }
            }
            catch (Exception ex)
            {
                if (this._logger != null)
                {
                    _logger.LogError(ex, "Exception");
                }
            }

            return(Task.FromResult(response));
        }
示例#2
0
        public Task <IActionResult> Handle(CategoryQuery request, CancellationToken cancellationToken)
        {
            IActionResult response = new BadRequestResult();

            try
            {
                var list = new List <Category>();

                if (request.CategoryId == 0)
                {
                    list = _repository.GetAll().Result;
                }
                else
                {
                    var obj = _repository.GetById(request.CategoryId).Result;
                    if (obj != null)
                    {
                        list.Add(obj);
                    }
                }

                response = new OkObjectResult(ApiResultHelper.GenerateResponse(HttpStatusCode.OK, list, 1));
            }
            catch (Exception ex)
            {
                if (this._logger != null)
                {
                    _logger.LogError(ex, "Exception");
                }
            }

            return(Task.FromResult(response));
        }
示例#3
0
        public async Task <IActionResult> Save([FromBody] CreateCategoryCommand request)
        {
            if (request == null)
            {
                return(BadRequest(ApiResultHelper.GenerateResponse(HttpStatusCode.BadRequest)));
            }

            return(await _mediator.Send(request));
        }
        public Task <IActionResult> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken)
        {
            IActionResult response = new BadRequestResult();

            try
            {
                var entity = new Category {
                    CategoryId = request.CategoryId
                };
                var result = _repository.Delete(entity).Result;

                response = new OkObjectResult(ApiResultHelper.GenerateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                if (this._logger != null)
                {
                    _logger.LogError(ex, "Exception");
                }
            }

            return(Task.FromResult(response));
        }