Ejemplo n.º 1
0
        public ActionResult <Result <GetComponentDto> > Post([FromBody] CreateComponentDto createComponentDto)
        {
            var componentToCreate = new Component
            {
                QuantityType = createComponentDto.QuantityType,
                UnitPrice    = createComponentDto.UnitPrice,
                Description  = createComponentDto.Description
            };

            var resultFromRepository = componentRepository.Create(componentToCreate);

            return(CreatedAtAction(nameof(Get), new { id = resultFromRepository.Value.Id }, new Result <GetComponentDto>
            {
                IsSuccess = resultFromRepository.IsSuccess,
                Message = resultFromRepository.Message,
                Value = resultFromRepository.Value != null
        ? new GetComponentDto
                {
                    Id = resultFromRepository.Value.Id,
                    QuantityType = resultFromRepository.Value.QuantityType,
                    UnitPrice = resultFromRepository.Value.UnitPrice,
                    Description = resultFromRepository.Value.Description
                }
        : null
            }));
        }
Ejemplo n.º 2
0
        public async Task <Result <GetComponentDto> > CreateComponentAsync(CreateComponentDto componentToCreate)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.PostAsJsonAsync($"{BaseUrl}/{apiUrl}", componentToCreate);

                return(response.IsSuccessStatusCode
                    ? await response.Content.ReadAsAsync <Result <GetComponentDto> >()
                    : new Result <GetComponentDto>
                {
                    IsSuccess = false,
                    Message = ReasonPhrases.GetReasonPhrase(Convert.ToInt32(response.StatusCode))
                });
            }
        }