public List <ProductDTO> ConvertProductDTO(List <Product> products) { var productsDto = new List <ProductDTO>(); foreach (var product in products) { var spectDto = new SpecificationDTO { Id = product.Specifications.Id, Author = product.Specifications.Author, Genres = product.Specifications.Genre, OriginallyPublished = product.Specifications.OriginallyPublished, PageCount = product.Specifications.PageCount, Illustrator = product.Specifications.Illustrators }; var productDto = new ProductDTO { Id = product.Id, Name = product.Name, Price = product.Price, Specifications = spectDto }; productsDto.Add(productDto); } return(productsDto); }
public ActionResult <Specification> PostSpecification(int id, SpecificationDTO specification) { if (!_productRepository.TryGetProduct(id, out var product)) { return(NotFound()); } var specificationToCreate = new Specification(specification.description, specification.Type); product.AddSpecification(specificationToCreate); _productRepository.SaveChanges(); return(CreatedAtAction("GetSpecification", new { id = product.Id, specificationId = specificationToCreate.Id }, specificationToCreate)); }
public async override Task <VTEXNewIDResponse> PostWithNewIDAsync(TResource data, CancellationToken cancellationToken) { var contentString = JsonSerializer.Serialize(data); var request = new HttpRequestMessage(HttpMethod.Post, _path) { Content = new StringContent(contentString, Encoding.UTF8, "application/json") }; request.Headers.Add("X-VTEX-API-AppKey", _appKey); request.Headers.Add("X-VTEX-API-AppToken", _appToken); request.Headers.Add("Accept", "application/json"); HttpResponseMessage response = new HttpResponseMessage(); try { response = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); } catch { _logger.Error($"No se pudo dar de alta el recurso {contentString} en la ruta `{_path}`, por error en la conexion`"); } if (!response.IsSuccessStatusCode) { try { var content = await JsonSerializer.DeserializeAsync <VTEXErrorResponse>(await response.Content.ReadAsStreamAsync()); _logger.Error($"No se pudo dar de alta el recurso {contentString} en la ruta `{_path}`, el statuscode fue `{response.StatusCode}` y el mensaje de VTEX:`{content.Message}`"); } catch { _logger.Error($"No se pudo dar de alta el recurso {contentString} en la ruta `{_path}`, el statuscode fue `{response.StatusCode}`"); } } else { SpecificationDTO responseContent = await JsonSerializer.DeserializeAsync <SpecificationDTO>(await response.Content.ReadAsStreamAsync()); _logger.Information($"Recurso {contentString} dado de alta en la ruta {_path} exitosamente y se le dió el id {responseContent.Id}"); return(new VTEXNewIDResponse() { Success = response.IsSuccessStatusCode, NewId = responseContent.Id }); } return(new VTEXNewIDResponse() { Success = response.IsSuccessStatusCode, NewId = 0 }); }