Exemplo n.º 1
0
        public CreateBreweryPayload CreateBrewery(
            CreateBreweryInput input,
            [Service] IBreweryValidationConductor validationConductor,
            [Service] IBreweryConductor breweryConductor,
            [Service] IMapper mapper,
            CancellationToken cancellationToken)
        {
            var dto = mapper.Map <DTO.Brewery>(input);

            if (!validationConductor.CanCreate(dto, out var errors))
            {
                return(new CreateBreweryPayload(
                           errors.Select(err => new UserError(err.message, err.key)).ToList(),
                           input.ClientMutationId));
            }

            var brewery = breweryConductor.Create(mapper.Map <Entity.Brewery>(dto));

            if (brewery.HasErrors || !(brewery.ResultObject is object))
            {
                return(new CreateBreweryPayload(
                           brewery.Errors.Select(err => new UserError(err.Message, err.Key)).ToList(),
                           input.ClientMutationId));
            }

            return(new CreateBreweryPayload(brewery.ResultObject, input.ClientMutationId));
        }
        public ActionResult Create([FromBody] DTO.Brewery dto)
        {
            // Validation
            if (!_validationConductor.CanCreate(dto, out var errors))
            {
                return(BadRequest());
            }

            var brewery = _breweryConductor.Create(_mapper.Map <Brewery>(dto));

            if (brewery.HasErrors || brewery.ResultObject is null)
            {
                return(BadRequest(brewery.Errors));
            }

            dto = _mapper.Map <DTO.Brewery>(brewery.ResultObject);

            return(CreatedAtAction(nameof(Get), new { id = dto.Id }, dto));
        }