public override CommandResponse <Nop> Handle(UpdateCartViewModelCommand task)
        {
            var cart = _repository.GetAllAndInclude(new List <Expression <Func <Cart, object> > >
            {
                x => x.Items,
                x => x.Coupons
            })
                       .OrderByDescending(x => x.Created)
                       .FirstOrDefault(x => x.UserId == task.UserId);

            var items     = GetItems(cart);
            var viewModel = new CartViewModel
            {
                Id            = task.UserId,
                RootEntityKey = task.UserId,
                Items         = items,
                SubTotal      = items.Sum(x => x.Price)
            };

            _viewModelRepository.Save(viewModel);

            Mediator.InternalEvent(new CartViewModelUpdatedEvent {
                UserId = task.UserId
            });

            return(CommandResponse.Succeeded(Nop.NoValue));
        }
        public override CommandResponse <Nop> Handle(AddProductToCartCommand task)
        {
            var user = _repository.Get <SiteUser>(task.UserId);

            if (user == null)
            {
                Mediator.Command(new CreateAnonymousUserCommand
                {
                    SessionId = task.UserId
                });
            }

            var cart = _repository
                       .GetAllAndInclude(new List <Expression <Func <Cart, object> > > {
                x => x.Items
            })
                       .FirstOrDefault(x => x.UserId == task.UserId) ?? new Cart
            {
                UserId = task.UserId,
                Id     = Guid.NewGuid()
            };
            var product = _repository.Get <Product>(task.ProductId);

            AddToCart(cart, product, task.Quantity);


            _repository.SaveOrUpdate(cart);
            Mediator.InternalEvent(new UserCartUpdatedEvent {
                UserId = task.UserId
            });
            return(CommandResponse.Succeeded(Nop.NoValue));
        }
        public override CommandResponse <TestResult> Handle(TestCommand task)
        {
            Mediator.Notification(new TestUiEvent {
                Message = $"UI NOtification from {Guid.NewGuid()}"
            });

            return(CommandResponse.Succeeded(new TestResult
            {
                Message = "Worked"
            }));
        }
        public override CommandResponse <Nop> Handle(CreateSampleCatalogCommand task)
        {
            var oldCatalog = _repository.GetAll <Product>();

            _repository.DeleteCollection(_repository.GetAll <CartItem>());
            _repository.DeleteCollection(_repository.GetAll <Cart>());
            _repository.DeleteCollection(oldCatalog);

            _repository.SaveOrUpdateCollection(SampleCatalog());
            Mediator.InternalEvent(new CatalogUpdatedEvent());
            return(CommandResponse.Succeeded(Nop.NoValue));
        }
Beispiel #5
0
        public override CommandResponse <Nop> Handle(UpdateHomePageViewModelCommand task)
        {
            var products = _repository.GetAll <Product>().ToArray();

            var homepageModel = new HomePageViewModelBuilder().SetInputs(new Dictionary <string, object>
            {
                { HomePageViewModelBuilder.Keys.Products, products }
            }).Build();

            _viewModelRepository.Save(homepageModel);

            Mediator.Notification(new HomePageUpdatedUiEvent());
            return(CommandResponse.Succeeded(Nop.NoValue));
        }
Beispiel #6
0
        public override CommandResponse <Nop> Handle(CreateNewSessionViewModelCommand task)
        {
            _repository.Save(new SessionViewModel
            {
                Id            = task.SessionId,
                Name          = "",
                RootEntityKey = task.SessionId
            });

            Mediator.InternalEvent(new SessionViewModelCreatedEvent {
                SessionId = task.SessionId
            });


            return(CommandResponse.Succeeded(Nop.NoValue));
        }
        public override CommandResponse <Nop> Handle(CreateAnonymousUserCommand task)
        {
            var user = _repository.Get <SiteUser>(task.SessionId);

            if (user != null)
            {
                return(CommandResponse.Succeeded(Nop.NoValue, "User already existed"));
            }
            user = new SiteUser
            {
                Id          = task.SessionId,
                IsAnonomous = true,
                LastLogin   = new SystemTime().UtcNow(),
                LoginCount  = 1,
                Name        = ""
            };
            _repository.SaveOrUpdate(user);
            Mediator.InternalEvent(new UserCreatedEvent
            {
                UserId = user.Id
            });
            return(CommandResponse.Succeeded(Nop.NoValue));
        }