public async Task <LoginCustomerViewModel> Handle(LoginCustomerQuery request, CancellationToken cancellationToken)
        {
            var entity = await _context.Customers
                         .Where(e =>
                                e.Username == request.Username &&
                                e.Password == Encypt.EncryptString(request.Password))
                         .FirstOrDefaultAsync(cancellationToken);

            if (entity.Equals(null))
            {
                throw new NotFoundException(nameof(entity), request.Username);
            }

            var expiryDate = DateTime.Now;

            expiryDate = expiryDate.AddHours(9);

            entity.ExternalId       = Guid.NewGuid();
            entity.ExternalIdExpiry = expiryDate;
            _context.Update(entity);
            await _context.SaveChangesAsync();

            return(new LoginCustomerViewModel
            {
                ExternalId = entity.ExternalId
            });
        }
        public async Task <GetCategoriesViewModel> Handle(GetCategoriesBySupplierQuery request, CancellationToken cancellationToken)
        {
            var categories = await(from category in _context.Categories
                                   join product in _context.Products
                                   on category.CategoryId equals product.CategoryId
                                   join stock in _context.Stocks
                                   on product.ProductId equals stock.ProductId
                                   join service in _context.Services
                                   on stock.StockId equals service.StockId
                                   join customer in _context.Customers
                                   on service.CustomerId equals customer.CustomerId
                                   where customer.ExternalId == request.ExternalId &&
                                   product.Supplier.ExternalId == request.SupplierExternalId &&
                                   service.Customer.ExternalIdExpiry >= DateTime.Now
                                   group product by new { category.CategoryId, category.Description, customer.CustomerId } into groupedByProduct
                                   select
                                   new
            {
                CategoryId   = groupedByProduct.Key.CategoryId,
                CategoryName = groupedByProduct.Key.Description,
                groupedByProduct.Key.CustomerId
            }
                                   ).ToListAsync();


            if (categories.Equals(null) || categories.Count == 0)
            {
                throw new NotFoundException("Products", request.ExternalId);
            }

            var customerForUpdate = await _context.Customers.FirstOrDefaultAsync(c => c.CustomerId == categories.First().CustomerId);

            var expiryDate = DateTime.Now;

            expiryDate = expiryDate.AddHours(9);

            customerForUpdate.ExternalIdExpiry = expiryDate;
            _context.Update(customerForUpdate);
            await _context.SaveChangesAsync();

            return(new GetCategoriesViewModel
            {
                Categories = (from categoryDetails in categories
                              select new CategoryDetails {
                    CategoryId = categoryDetails.CategoryId, CategoryName = categoryDetails.CategoryName
                }).ToList()
            });
        }
        public async Task <PurchaseViewModel> Handle(PurchaseCommand request, CancellationToken cancellationToken)
        {
            var data = await(from category in _context.Categories
                             join product in _context.Products
                             on category.CategoryId equals product.CategoryId
                             join stock in _context.Stocks
                             on product.ProductId equals stock.ProductId
                             join service in _context.Services
                             on stock.StockId equals service.StockId
                             join customer in _context.Customers
                             on service.CustomerId equals customer.CustomerId
                             where service.Customer.ExternalId == request.ExternalId &&
                             service.Customer.ExternalIdExpiry == DateTime.Now
                             select new
            {
                stock.Pin,
                Customer  = customer,
                StockItem = stock
            }).FirstAsync();


            if (data.Equals(null))
            {
                throw new NotFoundException("Pin Retreival", request.ExternalId);
            }

            var expiryDate = DateTime.Now;

            expiryDate.AddHours(9);

            data.Customer.ExternalIdExpiry = expiryDate;
            _context.Update(data.Customer);

            data.StockItem.Used = true;
            _context.Update(data.StockItem);

            await _context.SaveChangesAsync();

            return(new PurchaseViewModel
            {
                Pin = Encypt.DecryptString(data.Pin)
            });
        }
Пример #4
0
        public async Task <GetProductsViewModel> Handle(GetProductsByCategoryQuery request, CancellationToken cancellationToken)
        {
            var products = await(from category in _context.Categories
                                 join product in _context.Products
                                 on category.CategoryId equals product.CategoryId
                                 join stock in _context.Stocks
                                 on product.ProductId equals stock.ProductId
                                 join service in _context.Services
                                 on stock.StockId equals service.StockId
                                 join customer in _context.Customers
                                 on service.CustomerId equals customer.CustomerId
                                 where customer.ExternalId == request.ExternalId &&
                                 category.ExternalId == request.CategoryExternalId &&
                                 service.Customer.ExternalIdExpiry == DateTime.Now
                                 group product by new { product.ProductId, product.ProductName, customer } into groupedByProduct
                                 select new ProductDetails
            {
                ProductId   = groupedByProduct.Key.ProductId,
                ProductName = groupedByProduct.Key.ProductName,
                Customer    = groupedByProduct.Key.customer
            }).ToListAsync();


            if (products.Equals(null) || products.Count == 0)
            {
                throw new NotFoundException("Products", request.ExternalId);
            }

            var expiryDate = DateTime.Now;

            expiryDate.AddHours(9);

            var customerForUpdate = products.First().Customer;

            customerForUpdate.ExternalIdExpiry = expiryDate;
            _context.Update(customerForUpdate);
            await _context.SaveChangesAsync();

            return(new GetProductsViewModel
            {
                Products = products
            });
        }
        public async Task <GetSuppliersViewModel> Handle(GetSuppliersQuery request, CancellationToken cancellationToken)
        {
            var suppliers = await(from category in _context.Categories
                                  join product in _context.Products
                                  on category.CategoryId equals product.CategoryId
                                  join stock in _context.Stocks
                                  on product.ProductId equals stock.ProductId
                                  join service in _context.Services
                                  on stock.StockId equals service.StockId
                                  join customer in _context.Customers
                                  on service.CustomerId equals customer.CustomerId
                                  where service.Customer.ExternalId == request.ExternalId &&
                                  service.Customer.ExternalIdExpiry == DateTime.Now
                                  select new SupplierDetails
            {
                SupplierId   = product.Supplier.SupplierId,
                SupplierName = product.Supplier.CompanyName,
                Customer     = customer
            }).ToListAsync();


            if (suppliers.Equals(null) || suppliers.Count == 0)
            {
                throw new NotFoundException("Suppliers", request.ExternalId);
            }

            var expiryDate = DateTime.Now;

            expiryDate.AddHours(9);

            var customerForUpdate = suppliers.First().Customer;

            customerForUpdate.ExternalIdExpiry = expiryDate;
            _context.Update(customerForUpdate);
            await _context.SaveChangesAsync();

            return(new GetSuppliersViewModel
            {
                Suppliers = suppliers
            });
        }