Beispiel #1
0
        public async Task <PagedResults <IEnumerable <OrderServiceModel> > > GetAsync(GetOrdersServiceModel model)
        {
            var orders = from c in this.context.Orders
                         join os in this.context.OrderStatuses on c.OrderStatusId equals os.Id
                         join ost in this.context.OrderStatusTranslations on os.Id equals ost.OrderStatusId
                         where ost.Language == model.Language && c.IsActive
                         select new OrderServiceModel
            {
                Id                   = c.Id,
                SellerId             = c.SellerId,
                ClientId             = c.ClientId.Value,
                ClientName           = c.ClientName,
                BillingAddressId     = c.BillingAddressId,
                BillingCity          = c.BillingCity,
                BillingCompany       = c.BillingCompany,
                BillingCountryCode   = c.BillingCountryCode,
                BillingFirstName     = c.BillingFirstName,
                BillingLastName      = c.BillingLastName,
                BillingPhone         = c.BillingPhone,
                BillingPhonePrefix   = c.BillingPhonePrefix,
                BillingPostCode      = c.BillingPostCode,
                BillingRegion        = c.BillingRegion,
                BillingStreet        = c.BillingStreet,
                ShippingAddressId    = c.ShippingAddressId,
                ShippingCity         = c.ShippingCity,
                ShippingCompany      = c.ShippingCompany,
                ShippingCountryCode  = c.ShippingCountryCode,
                ShippingFirstName    = c.ShippingFirstName,
                ShippingLastName     = c.ShippingLastName,
                ShippingPhone        = c.ShippingPhone,
                ShippingPhonePrefix  = c.ShippingPhonePrefix,
                ShippingPostCode     = c.ShippingPostCode,
                ShippingRegion       = c.ShippingRegion,
                ShippingStreet       = c.ShippingStreet,
                ExpectedDeliveryDate = c.ExpectedDeliveryDate,
                ExternalReference    = c.ExternalReference,
                MoreInfo             = c.MoreInfo,
                Reason               = c.Reason,
                OrderStateId         = c.OrderStateId,
                OrderStatusId        = c.OrderStatusId,
                OrderStatusName      = ost.Name,
                OrderItems           = c.OrderItems.Select(x => new OrderItemServiceModel
                {
                    ProductId            = x.ProductId,
                    ProductSku           = x.ProductSku,
                    ProductName          = x.ProductName,
                    PictureUrl           = x.PictureUrl,
                    Quantity             = x.Quantity,
                    ExternalReference    = x.ExternalReference,
                    ExpectedDeliveryFrom = x.ExpectedDeliveryFrom,
                    ExpectedDeliveryTo   = x.ExpectedDeliveryTo,
                    MoreInfo             = x.MoreInfo,
                    LastModifiedDate     = x.LastModifiedDate,
                    CreatedDate          = x.CreatedDate
                }),
                LastModifiedDate = c.LastModifiedDate,
                CreatedDate      = c.CreatedDate
            };

            if (model.IsSeller is false)
            {
                orders = orders.Where(x => x.SellerId == model.OrganisationId.Value);
            }

            if (!string.IsNullOrWhiteSpace(model.SearchTerm))
            {
                orders = orders.Where(x => x.ClientName == model.SearchTerm || x.OrderItems.Any(y => y.ExternalReference == model.SearchTerm));
            }

            if (model.CreatedDateGreaterThan.HasValue)
            {
                orders = orders.Where(x => x.CreatedDate >= model.CreatedDateGreaterThan);
            }

            orders = orders.ApplySort(model.OrderBy);

            return(orders.PagedIndex(new Pagination(orders.Count(), model.ItemsPerPage), model.PageIndex));
        }
Beispiel #2
0
        public async Task <IActionResult> Get(string searchTerm, int pageIndex, int itemsPerPage, string orderBy, DateTime?createdDateGreaterThan)
        {
            var sellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim);

            var serviceModel = new GetOrdersServiceModel
            {
                Language               = CultureInfo.CurrentCulture.Name,
                SearchTerm             = searchTerm,
                PageIndex              = pageIndex,
                ItemsPerPage           = itemsPerPage,
                OrderBy                = orderBy,
                CreatedDateGreaterThan = createdDateGreaterThan,
                Username               = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value,
                OrganisationId         = GuidHelper.ParseNullable(sellerClaim?.Value),
                IsSeller               = this.User.IsInRole("Seller")
            };

            var validator = new GetOrdersModelValidator();

            var validationResult = await validator.ValidateAsync(serviceModel);

            if (validationResult.IsValid)
            {
                var orders = await this.ordersService.GetAsync(serviceModel);

                if (orders != null)
                {
                    var response = new PagedResults <IEnumerable <OrderResponseModel> >(orders.Total, orders.PageSize)
                    {
                        Data = orders.Data.OrEmptyIfNull().Select(x => new OrderResponseModel
                        {
                            Id                   = x.Id,
                            ClientId             = x.ClientId,
                            ClientName           = x.ClientName,
                            BillingAddressId     = x.BillingAddressId,
                            BillingCity          = x.BillingCity,
                            BillingCompany       = x.BillingCompany,
                            BillingCountryCode   = x.BillingCountryCode,
                            BillingFirstName     = x.BillingFirstName,
                            BillingLastName      = x.BillingLastName,
                            BillingPhone         = x.BillingPhone,
                            BillingPhonePrefix   = x.BillingPhonePrefix,
                            BillingPostCode      = x.BillingPostCode,
                            BillingRegion        = x.BillingRegion,
                            BillingStreet        = x.BillingStreet,
                            ShippingAddressId    = x.ShippingAddressId,
                            ShippingCity         = x.ShippingCity,
                            ShippingCompany      = x.ShippingCompany,
                            ShippingCountryCode  = x.ShippingCountryCode,
                            ShippingFirstName    = x.ShippingFirstName,
                            ShippingLastName     = x.ShippingLastName,
                            ShippingPhone        = x.ShippingPhone,
                            ShippingPhonePrefix  = x.ShippingPhonePrefix,
                            ShippingPostCode     = x.ShippingPostCode,
                            ShippingRegion       = x.ShippingRegion,
                            ShippingStreet       = x.ShippingStreet,
                            ExpectedDeliveryDate = x.ExpectedDeliveryDate,
                            MoreInfo             = x.MoreInfo,
                            Reason               = x.Reason,
                            OrderStateId         = x.OrderStateId,
                            OrderStatusId        = x.OrderStatusId,
                            OrderStatusName      = x.OrderStatusName,
                            OrderItems           = x.OrderItems.Select(y => new OrderItemResponseModel
                            {
                                ProductId            = y.ProductId,
                                ProductSku           = y.ProductSku,
                                ProductName          = y.ProductName,
                                PictureUrl           = y.PictureUrl,
                                Quantity             = y.Quantity,
                                ExternalReference    = y.ExternalReference,
                                ExpectedDeliveryFrom = y.ExpectedDeliveryFrom,
                                ExpectedDeliveryTo   = y.ExpectedDeliveryTo,
                                MoreInfo             = y.MoreInfo,
                                LastModifiedDate     = y.LastModifiedDate,
                                CreatedDate          = y.CreatedDate
                            }),
                            LastModifiedDate = x.LastModifiedDate,
                            CreatedDate      = x.CreatedDate
                        })
                    };

                    return(this.StatusCode((int)HttpStatusCode.OK, response));
                }
            }

            throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
        }