Esempio n. 1
0
        public async Task <ManufacturerViewModel> Handle(StoreManufacturerCommand request, CancellationToken cancellationToken)
        {
            var anyManufacturers = _context.Set <Manufacturer>().Any(x => x.Name == request.ManufacturerDto.Name);

            if (anyManufacturers)
            {
                throw new DuplicateNameException();
            }

            var validator = new StoreManufacturerCommandValidator();
            await validator.ValidateAndThrowAsync(request, cancellationToken : cancellationToken);

            var mappedManufacturer = _mapper.Map <Manufacturer>(request.ManufacturerDto);

            await _context.AddAsync(mappedManufacturer, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(new ManufacturerViewModel
            {
                Id = mappedManufacturer.Id,
                Name = request.ManufacturerDto.Name,
                Description = request.ManufacturerDto.Description,
            });
        }
        public async Task <PaginatedItems <CategoryViewModel> > Handle(GetCategoriesQuery request,
                                                                       CancellationToken cancellationToken)
        {
            var products = _context.Set <Category>().AsQueryable();

            var productsMapped = _mapper.ProjectTo <CategoryViewModel>(products);

            var paginatedProducts =
                PaginatedItems <CategoryViewModel> .Create(request.PageIndex,
                                                           request.PageSize, productsMapped);

            await Task.CompletedTask;

            return(paginatedProducts);
        }
        public SystemParameter Save(SystemParameter entity)
        {
            if (entity.Id == 0)
            {
                db.SystemParameters.Add(entity);
                db.SaveChanges();
            }
            else
            {
                db.Set <SystemParameter>().Attach(entity);
                db.Entry(entity).State = EntityState.Modified;
                db.SaveChanges();
            }

            return(entity);
        }
Esempio n. 4
0
        public async Task <SmartProductViewModel> Handle(GetProductQuery request, CancellationToken cancellationToken)
        {
            var products = await _context.Set <Product>()
                           .Include(x => x.Category)
                           .Include(x => x.Manufacturer)
                           .AsNoTracking()
                           .FirstOrDefaultAsync(x => x.Id == request.Id,
                                                cancellationToken: cancellationToken);

            if (products is null)
            {
                throw new ObjectNotFoundException();
            }

            var productsMapped = _mapper.Map <SmartProductViewModel>(products);

            return(productsMapped);
        }
        public async Task <PaginatedItems <SmartProductViewModel> > Handle(GetProductsListQuery request,
                                                                           CancellationToken cancellationToken)
        {
            var products = _context.Set <Product>()
                           .Include(x => x.Category)
                           .Include(x => x.Manufacturer)
                           .AsNoTracking()
                           .AsQueryable();

            products = new ProductsQueryBuilder(products)
                       .FilterByPrice(request.Price)
                       .FilterByStockQuantity(request.StockQuantity)
                       .Build();

            var productsMapped = _mapper.ProjectTo <SmartProductViewModel>(products);

            var paginatedProducts =
                PaginatedItems <SmartProductViewModel> .Create(request.PageIndex, request.PageSize,
                                                               productsMapped);

            await Task.CompletedTask;

            return(paginatedProducts);
        }