示例#1
0
        public virtual ActionResult Search(FilterOrderModel model)
        {
            GetDeliverTyps(model.DeliverType);
            GetOrderStatuses(model.OrderStatus);

            var result = _orderBusiness.Search(model);

            if (!Request.IsAjaxRequest())
            {
                return(View(result));
            }

            return(PartialView(MVC.Order.Views.Partials._SearchList, result.Result));
        }
示例#2
0
        public async Task <IActionResult> GetOrdersAsync(string role, [FromBody] FilterOrderModel filterOrder)
        {
            var userId = string.Empty;

            if (!string.IsNullOrWhiteSpace(role) && role.Equals(Constants.Roles.Admin))
            {
                userId = Constants.AdminSettings.AdminId.ToString();
            }

            if (!string.IsNullOrWhiteSpace(role) && role.Equals(Constants.Roles.User))
            {
                userId = User.Claims.FirstOrDefault(x => x.Type.Equals(ClaimTypes.NameIdentifier))?.Value;
            }

            var responseModel = await _orderService.GetOrdersAsync(filterOrder, userId);

            return(Ok(responseModel));
        }
示例#3
0
        public async Task <OrderModel> GetOrdersAsync(FilterOrderModel filterOrder, string userId)
        {
            var responseModel = new OrderModel();

            var repositoryFilter = _mapperHelper.Map <FilterOrderModel, DataFilter.FilterOrderModel>(filterOrder);

            if (!long.TryParse(userId, out long _userId) || _userId == 0)
            {
                responseModel.Errors.Add(Constants.Errors.UserNotFound);
                return(responseModel);
            }

            var orders = await _orderRepository.GetAllOrdersAsync(repositoryFilter, _userId);

            responseModel.ItemsCount = orders.CollectionCount;

            foreach (var order in orders.Collection)
            {
                var orderModelItem = order.MapToModel();
                responseModel.Items.Add(orderModelItem);
            }

            return(responseModel);
        }
示例#4
0
        public async Task <GenericModel <OrderDataModel> > GetAllOrdersAsync(FilterOrderModel filterOrder, long userId)
        {
            var query = _context.Orders
                        .Include(x => x.Payment)
                        .Include(x => x.User)
                        .Include(x => x.OrderItems)
                        .ThenInclude(x => x.PrintingEdition)
                        .AsQueryable();

            if (userId > 1)
            {
                query = query.Where(x => x.User.Id.Equals(userId));
            }

            if (filterOrder.TransactionStatus.Equals(TransactionStatus.Paid))
            {
                query = query.Where(x => x.Payment.TransactionId != null);
            }

            if (filterOrder.TransactionStatus.Equals(TransactionStatus.UnPaid))
            {
                query = query.Where(x => x.Payment.TransactionId == null);
            }

            var orders = query.Select(x => new OrderDataModel
            {
                Id           = x.Id,
                Amount       = x.Amount,
                CreationDate = x.CreationDate,
                Email        = x.User.Email,
                FirstName    = x.User.FirstName,
                LastName     = x.User.LastName,
                Currency     = x.OrderItems.Select(z => z.Currency).FirstOrDefault(),
                PaymentId    = x.Payment.TransactionId,
                OrderItems   = x.OrderItems.Select(z => new OrderItemDataModel
                {
                    Title = z.PrintingEdition.Title,
                    Count = z.Count,
                    PrintingEditionType = z.PrintingEdition.PrintingEditionType,
                    Amount   = z.Amount,
                    Currency = z.Currency
                }).ToList()
            });

            Expression <Func <OrderDataModel, object> > predicate = x => x.Id;

            if (filterOrder.SortType.Equals(SortType.Amount))
            {
                predicate = x => x.Amount;
            }

            if (filterOrder.SortType.Equals(SortType.Date))
            {
                predicate = x => x.CreationDate;
            }

            var responseModel = new GenericModel <OrderDataModel>()
            {
                CollectionCount = orders.Count()
            };

            var ordersPage = await PaginationAsync(filterOrder, predicate, orders);

            responseModel.Collection = ordersPage;

            return(responseModel);
        }
示例#5
0
        public async Task <GenericModel <OrderDataModel> > GetAllOrdersAsync(FilterOrderModel filter, long userId)
        {
            var responseModel = new GenericModel <OrderDataModel>();

            var columnSql = $"oi.Id, oi.Count, pe.Id, pe.Title, pe.PrintingEditionType, o.Id, o.CreationDate, o.Amount, pa.Id, pa.TransactionId, u.Id, u.FirstName, u.LastName, u.Email";

            var searchUserSql  = string.Empty;
            var offsetSql      = string.Empty;
            var sort           = string.Empty;
            var orderSql       = string.Empty;
            var transactionSql = string.Empty;

            if (userId > 1)
            {
                searchUserSql = $@"AND o.UserId = @userId";
            }

            if (filter.TransactionStatus.Equals(TransactionStatus.Paid))
            {
                transactionSql = $"AND pa.TransactionId IS NOT NULL ";
            }

            if (filter.TransactionStatus.Equals(TransactionStatus.UnPaid))
            {
                transactionSql = $"AND pa.TransactionId IS NULL ";
            }

            var sortType = $"Id";

            if (filter.SortType.Equals(SortType.Amount))
            {
                sortType = $"Amount";
            }

            if (filter.SortType.Equals(SortType.Date))
            {
                sortType = $"CreationDate";
            }

            var filterTypeSql = $"o.{sortType}";


            if (filter.SortState.Equals(SortState.Asc))
            {
                sort = "ASC";
            }

            if (filter.SortState.Equals(SortState.Desc))
            {
                sort = "DESC";
            }

            var countBuilder = new StringBuilder($@"
                                SELECT COUNT(DISTINCT o.Id)
                                FROM OrderItems AS oi
                                INNER JOIN PrintingEditions AS pe ON pe.Id = oi.PrintingEditionId                                                               
                                INNER JOIN (
	                                SELECT  o.Id, o.CreationDate, o.Amount, o.UserId, o.PaymentId
	                                FROM Orders AS o
	                                INNER JOIN Payments AS pa ON o.PaymentId = pa.Id
	                                WHERE o.IsRemoved = 0 {transactionSql} {searchUserSql} "    );

            var endSql = $@"
                    ) AS o ON o.Id = oi.OrderId
                    INNER JOIN Payments AS pa ON pa.Id = o.PaymentId
                    INNER JOIN AspNetUsers AS u ON u.Id = o.UserId;";

            var mainBuilder = new StringBuilder(countBuilder.ToString().Replace("COUNT(DISTINCT o.Id)", columnSql));

            orderSql = $@"ORDER BY {filterTypeSql} {sort}
                          OFFSET (@Page - 1) * @PageSize ROWS FETCH NEXT @PageSize ROWS ONLY";

            var resultSql = mainBuilder.Append(orderSql)
                            .Append(endSql)
                            .Append(countBuilder.Append(endSql).ToString()).ToString();

            var orders = new List <OrderDataModel>();

            using (var connection = GetSqlConnection())
            {
                var dict = new Dictionary <long, OrderDataModel>();

                var result = await connection.QueryMultipleAsync(resultSql, new {
                    filter.Page,
                    filter.PageSize,
                    userId
                });

                orders = result.Read <OrderItem, PrintingEdition, Order, Payment, ApplicationUser, OrderDataModel>(
                    (orderItem, printingEdition, order, payment, user) =>
                {
                    OrderDataModel model;

                    if (!dict.TryGetValue(order.Id, out model))
                    {
                        model = new OrderDataModel
                        {
                            Id           = order.Id,
                            CreationDate = order.CreationDate,
                            Amount       = order.Amount,
                            FirstName    = user.FirstName,
                            LastName     = user.LastName,
                            Email        = user.Email,
                            PaymentId    = payment.TransactionId,

                            OrderItems = new List <OrderItemDataModel>()
                        };

                        dict.Add(model.Id, model);
                    }

                    model.OrderItems.Add(new OrderItemDataModel
                    {
                        PrintingEditionType = printingEdition.PrintingEditionType,
                        Count = orderItem.Count,
                        Title = printingEdition.Title
                    });

                    return(model);
                },
                    splitOn: "Id")
                         .Distinct()
                         .ToList();

                responseModel.CollectionCount = result.Read <int>().FirstOrDefault();
            }

            responseModel.Collection = orders;

            return(responseModel);
        }