Ejemplo n.º 1
0
        public async Task <ProductToReturnDTO> GetProductByIdAsync(int id)
        {
            //return await _context.Products.AsNoTracking()
            //    .Include(p => p.ProductBrand)
            //    .Include(p => p.ProductType)
            //    .FirstOrDefaultAsync(p => p.Id == id);
            //return product;


            using (IDbConnection db = new SqliteConnection(ConfigurationAccessUtility.ConnectionString))
            {
                //use the "Cast(<columnName> as real) as <columnName>" workaround for decimals in Sqlite
                var product = db.Query <Product>($"Select Id, Name, Description, Cast(Price as real) as Price, " +
                                                 $"PictureUrl, ProductTypeId, ProductBrandId from Products WHERE Id = {id}").FirstOrDefault();

                if (product is not null)
                {
                    product.ProductBrand = db.Query <ProductBrand>($"Select * from ProductBrands where Id = {product.ProductBrandId}").FirstOrDefault();
                    product.ProductType  = db.Query <ProductType>($"Select * from ProductTypes where Id = {product.ProductTypeId}").FirstOrDefault();

                    var result = new ProductToReturnDTO()
                    {
                        Id           = product.Id,
                        Name         = product.Name,
                        Description  = product.Description,
                        PictureUrl   = ConfigurationAccessUtility.ApiUrl + product.PictureUrl,
                        Price        = product.Price,
                        ProductBrand = product.ProductBrand.Name,
                        ProductType  = product.ProductType.Name
                    };

                    return(result);

                    //we're going for performance here so we don't want to use mapper wrapper
                    //return MapperWrapper.Mapper.Map<ProductToReturnDTO>(product);
                }
                else
                {
                    return(null);
                }



                //var result = new ProductToReturnDTO()
                //{
                //    Id = product.Id,
                //    Name = product.Name,
                //    Description = product.Description,
                //    PictureUrl = product.PictureUrl,
                //    Price = product.Price,
                //    ProductBrand = product.ProductBrand.Name,
                //    ProductType = product.ProductType.Name
                //};

                //return result;
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <ProductToReturnDTO> > GetProduct(int productId)
        {
            // Product product = await _productsRepo.GetByIdAsync(productId);
            ProductsWithTypesAndBrandsSpecification spec = new ProductsWithTypesAndBrandsSpecification(productId);
            Product product = await _productsRepo.GetEntityWithSpec(spec);

            if (product is null)
            {
                return(NotFound(new ApiResponse(404)));
            }

            // Shape our data with DTO
            ProductToReturnDTO result = new ProductToReturnDTO(product, _config);

            return(Ok(result));
        }