Ejemplo n.º 1
0
        public async Task <IActionResult> DeleteProduct(int id)
        {
            ProductToReturnDto product = new ProductToReturnDto();

            if (ModelState.IsValid)
            {
                using (var httpClient = new HttpClient())
                {
                    using (var response = await httpClient.GetAsync($"https://localhost:5001/api/products/getproduct/{id}"))
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        product = JsonConvert.DeserializeObject <ProductToReturnDto>(apiResponse);
                    }
                }
                if (product == null)
                {
                    TempData["msgAccount"] = "Product Not Found";
                    return(RedirectToAction("Index", "Products"));
                }
            }
            var sharedMethod = new SharedSpace(_httpContextAccessor, _mapper);

            TempData["types"] = await sharedMethod.FetchProductTypes();

            TempData["brands"] = await sharedMethod.FetchProducBrands();

            await PublicMethods();

            return(View("DeleteProduct", product));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddToBasket(ProductToReturnDto product)
        {
            var service = new SharedSpace(_httpContextAccessor, _mapper);
            await service.AddProductToBasket(product);

            return(RedirectToAction("Index", "Products"));
        }
Ejemplo n.º 3
0
        public async Task <IEnumerable <ProductToReturnDto> > GetAll()
        {
            var product = await productsApiService.GetAll();

            List <ProductToReturnDto> response = new List <ProductToReturnDto>();
            var categories = _context.Categories;

            foreach (var elem in product)
            {
                var category       = categories.Find(elem.CategoryID).Name;
                var categoryArName = categories.Find(elem.CategoryID).ArName;
                elem.Description   = Method.RemoveRegx(elem.Description);
                elem.ArDescription = Method.RemoveRegx(elem.ArDescription);

                ProductToReturnDto products = new ProductToReturnDto
                {
                    Category       = category,
                    CategoryId     = elem.CategoryID,
                    CategoryArName = categoryArName,
                    Price          = elem.Price,
                    Name           = elem.Name,
                    PictuerUrl     = elem.ProductPictures,
                    ArName         = elem.ArName,
                    isOutOfStouck  = elem.isOutOfStock,
                    Id             = elem.ID,
                    Description    = elem.Description,
                    ArDescription  = elem.ArDescription
                };
                response.Add(products);
            }
            return(response);
        }
Ejemplo n.º 4
0
 public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id)
 {
     ProductToReturnDto productData = await _productManager.GetProduct(id);
     if (productData == null)
     {
         return NotFound(new ApiResponse(404));
     }
     return productData;
 }
        private async Task <ProductToReturnDto> GetProudct(int id)
        {
            ProductToReturnDto result = null;

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = _baseAddress;
                var url = "products/" + id;
                using (var response = await client.GetAsync(url))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();

                    result = JsonConvert.DeserializeObject <ProductToReturnDto>(apiResponse);
                }
            }
            return(result);
        }
Ejemplo n.º 6
0
        // GET
        public async Task <IActionResult> GetProductByProductId(int id)
        {
            ProductToReturnDto product = new ProductToReturnDto();

            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync($"https://localhost:5001/api/products/getproduct/{id}"))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();

                    product = JsonConvert.DeserializeObject <ProductToReturnDto>(apiResponse);
                }
            }
            await PublicMethods();

            return(View(product));
        }
        public async Task <ActionResult <Product> > GetProduct(int id)
        {
            var spec = new ProductsWithTypesAndBrandsSpecification(id);

            var product = await _productsRepo.GetEntityWithSpec(spec);

            var productToReturn = new ProductToReturnDto
            {
                Id           = product.Id,
                Name         = product.Name,
                Description  = product.Description,
                PhotoUrl     = product.PhotoUrl,
                Price        = product.Price,
                ProductBrand = product.ProductBrand.Name,
                ProducType   = product.ProducType.Name
            };

            return(Ok(productToReturn));
        }
Ejemplo n.º 8
0
        public async Task <bool> AddProductToBasket(ProductToReturnDto product)
        {
            if (basketID == null)
            {
                basketItem.Id          = product.Id;
                basketItem.PictureUrl  = product.PictureUrl;
                basketItem.Price       = product.Price;
                basketItem.ProductName = product.Name;
                basketItem.Type        = product.ProductType;
                basketItem.Brand       = product.ProductBrand;
                basketItem.Quantity    = 1;

                Guid id = Guid.NewGuid(); // create basketID for everysession, we use GUID number
                basket.Id = id.ToString();
                basket.Items.Add(basketItem);

                using (var httpClient = new HttpClient())
                {
                    var myContent   = JsonConvert.SerializeObject(basket);
                    var buffer      = System.Text.Encoding.UTF8.GetBytes(myContent);
                    var byteContent = new ByteArrayContent(buffer);
                    byteContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
                    using (var response = await httpClient.PostAsync("https://localhost:5001/api/basket/updatebasket", byteContent))
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        output = JsonConvert.DeserializeObject <CustomerBasket>(apiResponse);
                        _httpContextAccessor.HttpContext.Session.SetString("basketId", output.Id);
                        _httpContextAccessor.HttpContext.Session.SetString("output", JsonConvert.SerializeObject(output.Items));
                    }
                }
            }
            else
            {
                basketItem.Id          = product.Id;
                basketItem.PictureUrl  = product.PictureUrl;
                basketItem.Price       = product.Price;
                basketItem.ProductName = product.Name;
                basketItem.Type        = product.ProductType;
                basketItem.Brand       = product.ProductBrand;
                basketItem.Quantity    = 1;

                using (var httpClient = new HttpClient())
                {
                    using (var response = await httpClient.GetAsync($"https://localhost:5001/api/basket/getbasket/{basketID}"))
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        output = JsonConvert.DeserializeObject <CustomerBasket>(apiResponse);
                        //var ss = JsonConvert.DeserializeObject<List<BasketItem>>(_httpContextAccessor.HttpContext.Session.GetString("output"));
                        //output.Items = x;
                        var temp = output.Items.FindIndex(x => x.Id == basketItem.Id);
                        if (temp == -1)
                        {
                            output.Items.Add(basketItem);
                        }
                        else
                        {
                            output.Items[temp].Quantity++;
                        }

                        _httpContextAccessor.HttpContext.Session.SetString("output", JsonConvert.SerializeObject(output.Items));

                        var myContent   = JsonConvert.SerializeObject(output);
                        var buffer      = System.Text.Encoding.UTF8.GetBytes(myContent);
                        var byteContent = new ByteArrayContent(buffer);
                        byteContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
                        using (var response1 = await httpClient.PostAsync("https://localhost:5001/api/basket/updatebasket", byteContent))
                        {
                            string apiResponse1 = await response1.Content.ReadAsStringAsync();

                            output = JsonConvert.DeserializeObject <CustomerBasket>(apiResponse1);
                        }
                    }
                }
            }
            if (output != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }