Beispiel #1
0
        public async Task <IActionResult> GetBy([FromQuery] ReqItemDTO item)
        {
            try {
                var items = await this.m_itemService.GetItemsByKindAsync(item);

                return(this.Ok(items));
            } catch (Exception exception) {
                return(this.BadRequest(exception.Message));
            }
        }
Beispiel #2
0
        public async Task <object> GetItemsByKindAsync(ReqItemDTO item)
        {
            var expressionsList = new List <Expression <Func <Item, bool> > >();

            if (item.Color != null)
            {
                Expression <Func <Item, bool> > expColor = i => i.Color.Contains(item.Color, StringComparison.OrdinalIgnoreCase);
                expressionsList.Add(expColor);
            }

            if (item.Brand != null)
            {
                Expression <Func <Item, bool> > expBrand = i => i.Brand.Contains(item.Brand, StringComparison.OrdinalIgnoreCase);
                expressionsList.Add(expBrand);
            }

            if (item.Size != null)
            {
                Expression <Func <Item, bool> > expSize = i => i.Size.Contains(item.Size, StringComparison.OrdinalIgnoreCase);
                expressionsList.Add(expSize);
            }


            if (item.Kind != null)
            {
                Expression <Func <Item, bool> > expKind = i => i.CategoryId == item.Kind;
                expressionsList.Add(expKind);
            }

            if (item.Subkind != null)
            {
                Expression <Func <Item, bool> > expSubkind = i => i.SubCategoryId == item.Subkind;
                expressionsList.Add(expSubkind);
            }

            if (item.Status != null)
            {
                Statuses status;
                switch (item.Status.ToLower())
                {
                case "discounted":
                    status = Statuses.Discounted;
                    break;

                case "new":
                    status = Statuses.New;
                    break;

                default:
                    status = Statuses.Default;
                    break;
                }

                Expression <Func <Item, bool> > expStatus = i => i.Status == status;
                expressionsList.Add(expStatus);
            }

            if (item.Sex != null)
            {
                Gender gender;
                switch (item.Sex.ToLower())
                {
                case "male":
                    gender = Gender.Male;
                    break;

                case "female":
                    gender = Gender.Female;
                    break;

                default:
                    gender = Gender.Uni;
                    break;
                }

                Expression <Func <Item, bool> > expGender = i => i.Sex == gender || i.Sex == Gender.Uni;
                expressionsList.Add(expGender);
            }

            if (!string.IsNullOrWhiteSpace(item.Name))
            {
                Expression <Func <Item, bool> > expName = i => i.Name.Contains(item.Name, StringComparison.OrdinalIgnoreCase);
                expressionsList.Add(expName);
            }

            if (item.StartPrice != null && item.EndPrice != null)
            {
                Expression <Func <Item, bool> > expRange = i => i.Price <= item.EndPrice && i.Price >= item.StartPrice;

                expressionsList.Add(expRange);
            }

            long.TryParse(this.HttpContext.User.Claims.FirstOrDefault(i => i.Type == "Id")?.Value, out var userId);

            var query = (await this.Repository.GetAllAsync(expressionsList)).Include(i => i.FavoriteItems).Select(i => new GetFilteredItemDTO()
            {
                Id               = i.Id,
                Name             = i.Name,
                Price            = i.Price,
                Active           = i.Active,
                PreviewImagePath = i.MinPreviewImagePath != null
                    ? this.m_imageService.GetBase64String(i.MinPreviewImagePath)
                    :"",
                IsFavorite = i.FavoriteItems == null || i.FavoriteItems.Any(fa => fa.UserId == userId)
            });

            var result = await PaginationList <GetFilteredItemDTO> .CreateAsync(query, item.PageIndex ?? 1, item.PageSize ?? 12);

            if (result == null)
            {
                throw new Exception("Items not found.");
            }

            return(new { res = result, hasPrev = result.HasPreviousPage, hasNext = result.HasNextPage, total = result.TotalPages, index = result.PageIndex });
        }