Ejemplo n.º 1
0
 public async Task <IEnumerable <ProductDto> > GetAllAsync()
 {
     return(await _context.Products
            .Include(p => p.Category)
            .Select(p => ProductDto.FromEntity(p))
            .ToListAsync());
 }
Ejemplo n.º 2
0
        public async Task <ProductDto> GetByIdAsync(int id)
        {
            var product = await _context
                          .Products
                          .Include(p => p.Category)
                          .FirstOrDefaultAsync(x => x.ProductId == id);

            return(ProductDto.FromEntity(product));
        }
Ejemplo n.º 3
0
        public async Task <ProductDto> GetById(int userId, int productId)
        {
            // TODO: [TESTS] (ProductService.GetById) Add tests
            var builder = new ServiceMetricBuilder(nameof(ProductService), nameof(GetById))
                          .WithCategory(MetricCategory.Product, MetricSubCategory.GetById)
                          .WithCustomInt1(userId)
                          .WithCustomInt2(productId);

            try
            {
                using (builder.WithTiming())
                {
                    ProductEntity dbEntry;
                    using (builder.WithCustomTiming1())
                    {
                        builder.IncrementQueryCount();
                        dbEntry = await _productRepo.GetById(productId);

                        builder.CountResult(dbEntry);
                    }

                    if (dbEntry == null)
                    {
                        // TODO: [HANDLE] (ProductService.GetById) Handle this better
                        return(null);
                    }

                    if (dbEntry.UserId != userId)
                    {
                        // TODO: [HANDLE] (ProductService.GetById) Handle this better
                        return(null);
                    }

                    return(ProductDto.FromEntity(dbEntry));
                }
            }
            catch (Exception ex)
            {
                _logger.LogUnexpectedException(ex);
                builder.WithException(ex);
                return(null);
            }
            finally
            {
                await _metrics.SubmitPointAsync(builder.Build());
            }
        }