Exemplo n.º 1
0
        public static OrderResponseModel Map(OrderResponseDataModel responseFilter)
        {
            if (responseFilter is null)
            {
                return(new OrderResponseModel());
            }

            var responseFilterModel = new OrderResponseModel
            {
                TotalCount = responseFilter.TotalCount
            };

            foreach (var order in responseFilter.Orders)
            {
                var orderItemModels = new List <OrderItemModel>();
                foreach (var orderItem in order.OrderItems)
                {
                    if (orderItem == null)
                    {
                        continue;
                    }
                    var orderItemModel = _orderItemModelMapper.Map(orderItem);
                    orderItemModel.PrintingEdition = _printingEditionModelMapper.Map(orderItem.PrintingEdition);
                    orderItemModels.Add(orderItemModel);
                }
                var orderModel = _orderModelMapper.Map(order);
                orderModel.User       = _userModelMapper.Map(order.User);
                orderModel.OrderItems = orderItemModels;
                responseFilterModel.Orders.Add(orderModel);
            }

            return(responseFilterModel);
        }
Exemplo n.º 2
0
        public async Task <OrderResponseDataModel> FilterAsync(OrderRequestDataModel orderRequestDataModel)
        {
            var query = $@"SELECT * FROM (
                            SELECT * FROM {tableName} WHERE {tableName}.IsRemoved != 1
                            ORDER BY {tableName}.CreationDate
                            OFFSET {orderRequestDataModel.Paging.ItemsCount * orderRequestDataModel.Paging.CurrentPage} ROWS 
                            FETCH NEXT {orderRequestDataModel.Paging.ItemsCount} ROWS ONLY
                        ) AS {tableName}
                        LEFT JOIN {Constants.USERS_TABLE_NAME} ON {tableName}.UserId = {Constants.USERS_TABLE_NAME}.Id
                        LEFT JOIN {Constants.ORDER_ITEMS_TABLE_NAME} ON {Constants.ORDER_ITEMS_TABLE_NAME}.OrderId = {tableName}.Id
                        LEFT JOIN {Constants.PRINTING_EDITIONS_TABLE_NAME} ON {Constants.ORDER_ITEMS_TABLE_NAME}.PrintingEditionId 
                            = {Constants.PRINTING_EDITIONS_TABLE_NAME}.Id";

            using (var dbContext = new SqlConnection(connectionString))
            {
                await dbContext.OpenAsync();

                var orders = await dbContext.QueryAsync <Order, User, OrderItem, PrintingEdition, Order>(
                    query, (order, user, orderItem, printingEdition) =>
                {
                    order.User = user;
                    if (orderItem != null)
                    {
                        orderItem.PrintingEdition = printingEdition;
                    }
                    order.OrderItems.Add(orderItem);
                    return(order);
                });

                var querybaleOrders = orders
                                      .GroupBy(order => order.Id)
                                      .Select(group =>
                {
                    var result        = group.FirstOrDefault();
                    result.OrderItems = group.Select(order => order.OrderItems.SingleOrDefault()).ToList();
                    return(result);
                });

                var subquery = new List <Order>().AsQueryable();
                foreach (var status in orderRequestDataModel.OrderStatuses)
                {
                    subquery = subquery.Concat(querybaleOrders.Where(o => o.Status == status));
                }
                querybaleOrders = subquery;
                orders          = querybaleOrders.ToList();
                var totalCount = orders.Count();
                var result     = new OrderResponseDataModel
                {
                    Orders     = orders,
                    TotalCount = totalCount
                };
                return(result);
            }
        }
Exemplo n.º 3
0
        public async Task <OrderResponseDataModel> FilterAsync(OrderRequestDataModel orderRequestDataModel)
        {
            var query = DbSet.Include(order => order.User)
                        .Include(order => order.OrderItems)
                        .ThenInclude(orderItem => orderItem.PrintingEdition)
                        .Where(o => !o.IsRemoved && orderRequestDataModel.OrderStatuses.Contains(o.Status));
            var totalCount = await query.CountAsync();

            query = query
                    .OrderBy(orderRequestDataModel.SortPropertyName, $"{orderRequestDataModel.SortType}")
                    .Skip(orderRequestDataModel.Paging.CurrentPage * orderRequestDataModel.Paging.ItemsCount)
                    .Take(orderRequestDataModel.Paging.ItemsCount);
            var orders = query.ToList();
            var result = new OrderResponseDataModel
            {
                Orders     = orders,
                TotalCount = totalCount
            };

            return(result);
        }