Exemple #1
0
        public async Task <IActionResult> GetAllProducts()
        {
            // return specific fields of model
            List <ProductGetDto> productDTO = new List <ProductGetDto>();

            var products = await _products.GetAllAsync();

            //var products = _seeder.GetAllProducts();
            foreach (var product in products)
            {
                var dto = new ProductGetDto();
                dto.Id          = product.Id;
                dto.Name        = product.Name;
                dto.Description = product.Description;
                dto.Price       = product.Price;
                dto.SupplierId  = product.SupplierId;
                dto.PhotoUrl    = product.PhotoUrl;
                productDTO.Add(dto);
            }
            //var product = new Product();
            //product.Id = 1;
            //product.Name = "Shampoo";
            //product.Price = 12.82f;

            //return Ok(_seeder.GetAllProducts());
            return(Ok(productDTO));
        }
Exemple #2
0
        public async Task <IActionResult> GetProductById(int id)
        {
            var product = await _products.GetByIdAsync(id);

            //var product = _seeder.GetAllProducts().FirstOrDefault(p => p.Id == id);

            if (product == null)
            {
                return(NotFound());
            }
            var productDTO = new ProductGetDto();

            productDTO.Id          = product.Id;
            productDTO.Name        = product.Name;
            productDTO.Description = product.Description;
            productDTO.Price       = product.Price;
            productDTO.SupplierId  = product.SupplierId;
            productDTO.PhotoUrl    = product.PhotoUrl;
            //var product = new Product();
            //product.Id = id;
            //product.Name = $"product No.{id}";
            //product.Price = 12.82f;
            //return Ok(product);
            return(Ok(productDTO));
        }
        public async Task <Product> GetProduct(int productId)
        {
            ProductGetDto product = null;

            var request = new HttpRequestMessage(HttpMethod.Get, $"Products/{productId}");

            var response = await _client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                product = await response.Content.ReadAsAsync <ProductGetDto>();
            }

            return(_mapper.Map <Product>(product));
        }