private async Task <OrderEntity> GetOrderForBuyerToPayOrThrow(CreateCheckoutRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                throw new KnownException("Invalid request model!");
            }

            EnsureBuyerProfile();
            OrdersRepo.ChainQueryable(q => q.Include(o => o.Buyer));
            var order = await OrdersRepo.GetOne(model.OrderId);

            if (order == null)
            {
                throw new KnownException("Comanda nu a fost găsită!");
            }

            if (order.Buyer != CurrentBuyerProfile)
            {
                throw new KnownException("Nu poți plăti această comandă deoarece nu îți aparține!");
            }

            if (order.State == OrderState.Paid || order.State == OrderState.WaitingProcessing)
            {
                throw new KnownException("Această comandă este deja plătită!");
            }

            return(order);
        }
 public override void OnActionExecuting(ActionExecutingContext context)
 {
     base.OnActionExecuting(context);
     ProductsRepo.ChainQueryable(q => q.Include(p => p.Seller));
     OrdersRepo.ChainQueryable(q => q
                               .Include(o => o.Buyer)
                               .Include(o => o.Products)
                               .ThenInclude(p => p.Product.Seller.Address)
                               .Include(o => o.Products)
                               .ThenInclude(p => p.Product.Categories)
                               .ThenInclude(c => c.Category.Parent)
                               );
 }
        public async Task <IActionResult> GetOne([FromRoute][Required] string id)
        {
            if (await UserManager.IsInRoleAsync(CurrentUser, "Buyer"))
            {
                EnsureBuyerProfile();
                OrdersRepo.ChainQueryable(q => q.Where(o => o.Buyer == CurrentBuyerProfile));
                var order = await OrdersRepo.GetOne(id);

                if (order == null)
                {
                    return(NotFound());
                }

                return(Ok(Mapper.Map <OrderViewModel>(order)));
            }

            return(NotFound());
        }
        public async Task <IActionResult> GetOwn()
        {
            if (await UserManager.IsInRoleAsync(CurrentUser, "Buyer"))
            {
                EnsureBuyerProfile();
                OrdersRepo.ChainQueryable(q => q.Where(o => o.Buyer == CurrentBuyerProfile));
            }

            if (await UserManager.IsInRoleAsync(CurrentUser, "Seller"))
            {
                EnsureSellerProfile();
                throw new KnownException("Can't filter orders for a seller yet.");
            }

            var orders = await OrdersRepo.GetAll();

            return(Ok(Mapper.Map <IEnumerable <OrderViewModel> >(orders)));
        }