コード例 #1
0
        public async Task <IActionResult> GetProductByIdAsync(string id, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(BadRequest(new { Message = "Id Cant be null" }));
            }

            var result = await _catalogContext.Products.Where(x => x.Id == id)
                         .Include(x => x.ProductImages)
                         .Include(x => x.ProductColors)
                         .FirstOrDefaultAsync(cancellationToken);

            if (result != null)
            {
                var imagesWithUrl = new List <ProductImage>();
                foreach (var img in result.ProductImages.ToList())
                {
                    var imgWithUrl = await UrlImageHelper <ProductImage> .GetImageBase64UrlAsync(img.ProductId, img,
                                                                                                 _fileUtility, "ProductImage",
                                                                                                 cancellationToken);

                    imagesWithUrl.Add(imgWithUrl);
                }

                var productViewModel = new ProductViewModel
                {
                    CategoryId     = result.CategoryId,
                    ManufacturerId = result.ManufacturerId,
                    Description    = result.Description,
                    Id             = result.Id,
                    Name           = result.Name,
                    Price          = result.Price,
                    ProductColors  = result.ProductColors.ToArray(),
                    ProductImages  = imagesWithUrl.ToArray(),
                    Location       = result.Location,
                    MinPurchase    = result.MinPurchase,
                    HasExpiry      = result.HasExpiry ? "True" : "False",
                    ExpireDate     = result.ExpireDate.ToShortDateString(),
                    Discount       = result.Discount,
                    Stock          = result.AvailableStock
                };
                return(Ok(productViewModel));
            }

            return(NotFound());
        }
コード例 #2
0
        public async Task <IActionResult> GetManufacturerByIdAsync(int id, CancellationToken cancellationToken)
        {
            if (id <= 0)
            {
                return(BadRequest(new { Message = $"Invalid Manufacturer id." }));
            }

            var result = await _catalogContext.Manufacturers.Where(x => x.Id == id)
                         .FirstOrDefaultAsync(cancellationToken);

            if (result != null)
            {
                var withUrl =
                    await UrlImageHelper <Manufacturer> .GetImageBase64UrlAsync(result, _fileUtility, "Manufacturer",
                                                                                cancellationToken);

                return(Ok(withUrl));
            }

            return(NotFound());
        }