Exemplo n.º 1
0
        public async Task <CommandResponse <Business> > Handle(BusinessCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new CommandResponse <Business>()
            {
                Errors = GetRequestErrors(request)
            };

            if (result.Errors.Count == 0)
            {
                try
                {
                    var aggregate = new AssociateAggregate(_dbContext);
                    await aggregate.BusinessCreateAsync(request.Item);

                    result.Result = (Business)request.Item;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);

                    result.ErrorInfo = new ErrorInfo()
                    {
                        UserErrorMessage = "An internal error has occured",
                        HasException     = true
                    };
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        private List <KeyValuePair <string, string> > GetRequestErrors(BusinessCreateCommand request)
        {
            var issues = _validator.Validate((Business)request.Item).Errors;

            foreach (var issue in issues)
            {
                _errors.Add(new KeyValuePair <string, string>(issue.PropertyName, issue.ErrorMessage));
            }
            return(_errors);
        }
Exemplo n.º 3
0
        public async Task <ActionResult <Business> > PostBusiness([FromBody] Business item)
        {
            var command     = new BusinessCreateCommand(item);
            var cmdResponse = await Mediator.Send(command);

            if (cmdResponse.ErrorInfo.HasException)
            {
                return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError));
            }

            if (cmdResponse.Errors.Any())
            {
                return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status400BadRequest));
            }

            Uri.TryCreate($"{Request.Host}{Request.Path.ToString().AddLast("/").AddLast(cmdResponse.Result.BusinessKey.ToString())}", UriKind.RelativeOrAbsolute, out Uri getUrl);

            return(Created(getUrl ?? new Uri("http://localhost"), cmdResponse.Result));
        }