public async Task <WholesalerDto> Handle(WholesalerCommandDeleteProducts request, CancellationToken cancellationToken)
            {
                var wholesaler = await _context.Wholesalers.Include(x => x.Products).FirstOrDefaultAsync(x => x.Id == request.WholesalerId);

                if (wholesaler == null)
                {
                    throw new InvalidOperationException($"WholesalerId '{request.WholesalerId}' not found");
                }

                var productDict = wholesaler.Products.ToDictionary(x => x.ProductInfo.Upc, x => x);

                foreach (var upc in request.Upcs)
                {
                    if (productDict.TryGetValue(upc, out Product product))
                    {
                        wholesaler.DeleteProduct(product);
                    }
                    else
                    {
                        throw new InvalidOperationException($"ProductUpc '{upc}' not found");
                    }
                }

                await _context.SaveChangesAsync(cancellationToken);

                return(WholesalerMapper.MapToDto(wholesaler));
            }
示例#2
0
            public async Task <WholesalerDto> Handle(WholesalerCommandUpdateInfo request, CancellationToken cancellationToken)
            {
                var wholesaler = _context.Wholesalers.FirstOrDefault(x => x.Id == request.Id);

                if (wholesaler == null)
                {
                    throw new InvalidOperationException($"WholesalerId '{request.Id}' not found");
                }
                wholesaler.UpdateWholesalerInfo(request.WholesalerInfoDto);
                await _context.SaveChangesAsync(cancellationToken);

                return(WholesalerMapper.MapToDto(wholesaler));
            }
示例#3
0
            public async Task <WholesalerDto> Handle(WholesalerQueryGetById request, CancellationToken cancellationToken)
            {
                var wholesaler = await _context.Wholesalers.Include(x => x.Products).FirstOrDefaultAsync(x => x.Id == request.Id);

                if (wholesaler == null)
                {
                    throw new InvalidOperationException($"WholesalerId '{request.Id}' not found");
                }

                var WholesalerDto = WholesalerMapper.MapToDto(wholesaler);

                return(WholesalerDto);
            }
示例#4
0
            public async Task <IEnumerable <WholesalerDto> > Handle(WholesalerQueryGetAll request, CancellationToken cancellationToken)
            {
                var query = _context.Wholesalers.AsQueryable();

                if (!string.IsNullOrEmpty(request.Upc))
                {
                    query = query.Include(x => x.Products).Where(x => x.Products.Any(y => y.ProductInfo.Upc == request.Upc));
                }
                var wholesalers = await query.ToListAsync();

                var wholesalerDtos = WholesalerMapper.MapToDto(wholesalers);

                return(wholesalerDtos);
            }
示例#5
0
            public async Task <WholesalerDto> Handle(WholesalerCommandCreate request, CancellationToken cancellationToken)
            {
                var existingWholesaler = _context.Wholesalers.FirstOrDefault(x => x.WholesalerInfo.Name == request.WholesalerInfoDto.Name);

                if (existingWholesaler != null)
                {
                    throw new InvalidOperationException($"Duplicate Wholesaler with name: '{request.WholesalerInfoDto.Name}'");
                }

                var wholesaler = new Wholesaler(request.WholesalerInfoDto);

                _context.Wholesalers.Add(wholesaler);

                await _context.SaveChangesAsync(cancellationToken);

                return(WholesalerMapper.MapToDto(wholesaler));
            }