Пример #1
0
        public ActionResult Purchase(Order model)
        {
            db.Orders.Add(model);
            var cart = ShoppingCart.Cart;

            foreach (var p in cart.Items)
            {
                var d = new OrderDetail
                {
                    Order     = model,
                    ProductId = p.Id,
                    UnitPrice = p.UnitPrice,
                    Discount  = p.Discount,
                    Quantity  = p.Quantity
                };
                var prod = db.Products.Find(p.Id);
                db.Products.Remove(prod);
                db.SaveChanges();
                prod.Views    = prod.Views + 1;
                prod.Quantity = prod.Quantity - p.Quantity;
                db.Products.Add(prod);
                db.OrderDetails.Add(d);
            }
            db.SaveChanges();

            // Thanh toán trực tuyến
            //var api = new WebApiClient<AccountInfo>();
            //var data = new AccountInfo {
            //    Id=Request["BankAccount"],
            //    Balance = cart.Total
            //};
            //api.Put("api/Bank/nn", data);
            return(RedirectToAction("Detail", new { id = model.Id }));
        }
Пример #2
0
        public ActionResult Create([Bind(Include = "Id,Name,UnitBrief,UnitPrice,Image,ProductDate,Available,Description,CategoryId,SupplierId,Quantity,Discount,Special,Latest,Views")] Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", product.CategoryId);
            ViewBag.SupplierId = new SelectList(db.Suppliers, "Id", "Name", product.SupplierId);
            return(View(product));
        }
 public void Execute(ProductChangeDto request)
 {
     request.Id = 0;
     _validator.ValidateAndThrow(request);
     _context.Products.Add(_mapper.Map <Product>(request));
     _context.SaveChanges();
 }
 public void Execute(MechanismDto request)
 {
     request.Id = 0;
     _validator.ValidateAndThrow(request);
     _context.Mechanisms.Add(_mapper.Map <Mechanism>(request));
     _context.SaveChanges();
 }
Пример #5
0
 public void Execute(GroupDto request)
 {
     request.Id = 0;
     _validator.ValidateAndThrow(request);
     _context.Groups.Add(_mapper.Map <Group>(request));
     _context.SaveChanges();
 }
Пример #6
0
        public void Execute(OrderCreateDto request)
        {
            _validator.ValidateAndThrow(request);
            var order = new Order
            {
                UserId    = _actor.Id,
                Address   = request.Address,
                OrderDate = request.OrderDate
            };

            foreach (var item in request.Items)
            {
                var product = _context.Products.Find(item.ProductId);

                product.Quantity -= item.Quantity;

                order.OrderLines.Add(new OrderLine
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity,
                    Name      = product.Name,
                    Price     = (decimal)product.Price
                });
            }
            _context.Orders.Add(order);
            _context.SaveChanges();
        }
Пример #7
0
        public void Execute(RegisterUserDto request)
        {
            _validator.ValidateAndThrow(request);
            var useCases = _context.UseCases.Where(x => !x.AdminCase && x.Id != 8).Select(x => x.Id).ToList();

            var user = _mapper.Map <WatchShop.Domain.User>(request);

            user.GroupId = 2;

            _context.Add(user);

            var listOfUseCases = new List <UserUserCase>();

            foreach (var item in useCases)
            {
                listOfUseCases.Add(new UserUserCase
                {
                    Id        = user.Id,
                    UseCaseId = item
                });
            }

            user.UserUserCases = listOfUseCases;

            _context.SaveChanges();

            _sender.Send(new SendEmailDto
            {
                Content = "<h1>Successfull registration!</h1>",
                SendTo  = request.Email,
                Subject = "Registration"
            });
        }
Пример #8
0
 public void Execute(BrandDto request)
 {
     request.Id = 0;
     _validator.ValidateAndThrow(request);
     _context.Brands.Add(_mapper.Map <Brand>(request));
     _context.SaveChanges();
 }
Пример #9
0
        public void Execute(UserAdminAddOrChangeDto request)
        {
            _validator.ValidateAndThrow(request);

            _context.Users.Add(_mapper.Map <WatchShop.Domain.User>(request));

            _context.SaveChanges();

            var user = _context.Users.FirstOrDefault(x => x.Username == request.Username);

            var useCases = new List <WatchShop.Domain.UserUserCase>();

            var useCasesList = _context.UseCases.ToList();

            var adminUseCases = useCasesList.Select(x => x.Id);

            var userUseCases = useCasesList.Where(x => !x.AdminCase).Select(x => x.Id);

            if (user.GroupId == 1)
            {
                foreach (var useCase in adminUseCases)
                {
                    useCases.Add(new WatchShop.Domain.UserUserCase
                    {
                        UseCaseId = useCase,
                        UserId    = user.Id
                    });
                }
            }
            if (user.GroupId == 2)
            {
                foreach (var useCase in userUseCases)
                {
                    useCases.Add(new WatchShop.Domain.UserUserCase
                    {
                        UseCaseId = useCase,
                        UserId    = user.Id
                    });
                }
            }
            if (useCases.Count > 0)
            {
                user.UserUserCases = useCases;
                _context.SaveChanges();
            }
        }
Пример #10
0
 public void Log(IUseCase useCase, IApplicationActor actor, object useCaseData)
 {
     _context.UseCaseLogs.Add(new Domain.UseCaseLog
     {
         Actor       = actor.Identity,
         Data        = JsonConvert.SerializeObject(useCaseData),
         Date        = DateTime.UtcNow,
         UseCaseName = useCase.Name
     });
     _context.SaveChanges();
 }
        public void Execute(GroupDto request)
        {
            _validator.ValidateAndThrow(request);
            var group = _context.Groups.Find(request.Id);

            if (group == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(Group));
            }

            _mapper.Map(request, group);
            _context.SaveChanges();
        }
        public void Execute(BrandDto request)
        {
            _validator.ValidateAndThrow(request);
            var brand = _context.Brands.Find(request.Id);

            if (brand == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(Brand));
            }

            _mapper.Map(request, brand);
            _context.SaveChanges();
        }
Пример #13
0
        public void Execute(MechanismDto request)
        {
            _validator.ValidateAndThrow(request);
            var mechanism = _context.Mechanisms.Find(request.Id);

            if (mechanism == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(Mechanism));
            }

            _mapper.Map(request, mechanism);
            _context.SaveChanges();
        }
        public void Execute(ProductChangeDto request)
        {
            _validator.ValidateAndThrow(request);
            var product = _context.Products.Find(request.Id);

            if (product == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(Product));
            }

            _mapper.Map(request, product);
            _context.SaveChanges();
        }
Пример #15
0
        public void Execute(UserChangeDto request)
        {
            _validator.ValidateAndThrow(request);
            var user = _context.Users.Find(request.Id);

            if (user == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(WatchShop.Domain.User));
            }
            if (_actor.Id != user.Id)
            {
                throw new UnauthorizedAccessException();
            }
            _mapper.Map(request, user);
            _context.SaveChanges();
        }
        public void Execute(int request)
        {
            var brand = _context.Brands.Include(x => x.Products).FirstOrDefault(x => x.Id == request);

            if (brand == null)
            {
                throw new EntityNotFoundException(request, typeof(Brand));
            }
            if (brand.Products.Any(x => x.IsActive))
            {
                throw new EntityConflictException("Brand has active products");
            }

            brand.IsDeleted = true;
            brand.IsActive  = false;
            _context.SaveChanges();
        }
        public void Execute(int request)
        {
            var mechanism = _context.Mechanisms.Include(x => x.Products).FirstOrDefault(x => x.Id == request);

            if (mechanism == null)
            {
                throw new EntityNotFoundException(request, typeof(Mechanism));
            }
            if (mechanism.Products.Any(x => x.IsActive))
            {
                throw new EntityConflictException("Mechanism has active products");
            }

            mechanism.IsDeleted = true;
            mechanism.IsActive  = false;
            _context.SaveChanges();
        }
Пример #18
0
        public void Execute(int request)
        {
            var group = _context.Groups.Include(x => x.Users).FirstOrDefault(x => x.Id == request);

            if (group == null)
            {
                throw new EntityNotFoundException(request, typeof(Group));
            }
            if (group.Users.Any(x => x.IsActive))
            {
                throw new EntityConflictException("Group has active users");
            }

            group.IsDeleted = true;
            group.IsActive  = false;
            _context.SaveChanges();
        }
        public void Execute(int request)
        {
            var gender = _context.Genders.Include(x => x.Products).FirstOrDefault(x => x.Id == request);

            if (gender == null)
            {
                throw new EntityNotFoundException(request, typeof(Gender));
            }
            if (gender.Products.Any(x => x.IsActive))
            {
                throw new EntityConflictException("Gender has active products");
            }

            gender.IsDeleted = true;
            gender.IsActive  = false;
            _context.SaveChanges();
        }
        public void Execute(int request)
        {
            var user = _context.Users.Include(x => x.Orders).FirstOrDefault(x => x.Id == request);

            if (user == null)
            {
                throw new EntityNotFoundException(request, typeof(WatchShop.Domain.User));
            }
            if (user.Orders.Any(x => x.OrderStatus == OrderStatus.Recieved || x.OrderStatus == OrderStatus.Shipped))
            {
                throw new EntityConflictException("User has active orders - can't be deleted");
            }

            user.IsDeleted = true;
            user.IsActive  = false;
            _context.SaveChanges();
        }
Пример #21
0
        public void Execute(ChangeUserGroupDto request)
        {
            _validator.ValidateAndThrow(request);
            var user = _context.Users.Include(x => x.UserUserCases).Where(x => x.Id == request.Id).FirstOrDefault();

            if (user == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(WatchShop.Domain.User));
            }
            user.GroupId = request.GroupId;
            var useCases      = new List <WatchShop.Domain.UserUserCase>();
            var useCasesList  = _context.UseCases.ToList();
            var adminUseCases = useCasesList.Where(x => x.Id != 8).Select(x => x.Id);
            var userUseCases  = useCasesList.Where(x => !x.AdminCase).Select(x => x.Id);

            if (user.GroupId == 1)
            {
                foreach (var useCase in adminUseCases)
                {
                    useCases.Add(new WatchShop.Domain.UserUserCase
                    {
                        UseCaseId = useCase,
                        UserId    = user.Id
                    });
                }
            }
            else
            {
                foreach (var useCase in userUseCases)
                {
                    useCases.Add(new WatchShop.Domain.UserUserCase
                    {
                        UseCaseId = useCase,
                        UserId    = user.Id
                    });
                }
            }
            user.UserUserCases = useCases;
            _context.SaveChanges();
        }
Пример #22
0
        public void Execute(int request)
        {
            var product = _context.Products
                          .Include(x => x.OrderLines)
                          .ThenInclude(x => x.Order)
                          .FirstOrDefault(x => x.Id == request);

            if (product == null)
            {
                throw new EntityNotFoundException(request, typeof(Product));
            }
            var orderLines = _context.OrderLines.Where(x => x.ProductId == request).ToList();

            foreach (var orderLine in orderLines)
            {
                orderLine.ProductId = null;
            }

            product.IsDeleted = true;
            product.IsActive  = false;
            _context.SaveChanges();
        }
Пример #23
0
        public void Execute(UserRightsDto request)
        {
            _validator.ValidateAndThrow(request);
            var user = _context.Users.Include(x => x.UserUserCases).Where(x => x.Id == request.Id).FirstOrDefault();

            if (user == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(WatchShop.Domain.User));
            }
            var listOfRights = new List <UserUserCase>();

            foreach (var useCase in request.UseCases)
            {
                listOfRights.Add(new UserUserCase
                {
                    UserId    = request.Id,
                    UseCaseId = useCase
                });
            }
            user.UserUserCases = listOfRights;
            _context.SaveChanges();
        }
Пример #24
0
        public void Execute(OrderStatusChangeDto request)
        {
            _validator.ValidateAndThrow(request);
            var order = _context.Orders
                        .Include(o => o.OrderLines)
                        .ThenInclude(ol => ol.Product)
                        .FirstOrDefault(x => x.Id == request.OrderId);

            if (order == null)
            {
                throw new EntityNotFoundException(request.OrderId, typeof(Order));
            }
            if (order.OrderStatus == OrderStatus.Delivered)
            {
                throw new EntityConflictException("Can't change status of delivered orders");
            }

            if (order.OrderStatus == OrderStatus.Recieved)
            {
                if (request.Status == OrderStatus.Delivered)
                {
                    throw new EntityConflictException("Must be in state shipped before choosing delivered");
                }
                if (request.Status == OrderStatus.Canceled)
                {
                    foreach (var line in order.OrderLines)
                    {
                        line.Product.Quantity += line.Quantity;
                    }
                }
            }
            if (order.OrderStatus == OrderStatus.Shipped)
            {
                if (request.Status == OrderStatus.Canceled)
                {
                    throw new EntityConflictException("Order is in shipping progress - can't be canceled");
                }
            }
            if (order.OrderStatus == OrderStatus.Canceled)
            {
                if (request.Status == OrderStatus.Shipped || request.Status == OrderStatus.Delivered)
                {
                    throw new EntityConflictException("Canceled order can be changed in received only");
                }
                if (request.Status == OrderStatus.Recieved)
                {
                    foreach (var line in order.OrderLines)
                    {
                        if (line.Product != null)
                        {
                            throw new EntityConflictException("One of the products doesn't exist - you can't retrive order");
                        }
                        line.Product.Quantity -= line.Quantity;
                        if (line.Product.Quantity < 0)
                        {
                            throw new EntityConflictException("One of the products isn't available at the moment - you can't retrive order");
                        }
                    }
                }
            }
            order.OrderStatus = request.Status;
            _context.SaveChanges();
        }