Пример #1
0
        public void TestUpdateWrongSellerId()
        {
            var updateUser = new DTOArticle
            {
                Id          = 9,
                ImageSource = "path-to-photo",
                SellerId    = 1,
                State       = "free",
                Name        = "Lunettes noires",
                Description = "Ray Ban noires",
                Category    = "accessories",
                Sex         = "woman",
                Brand       = "RayBan",
                Condition   = 10
            };
            var result = _mockRepo.Update(updateUser).Result;

            Assert.IsNull(result);
            Assert.AreEqual(8, _mockRepo.Count().Result);
        }
Пример #2
0
        /// <summary>
        /// Gets all the articles in function of the parameters given
        /// </summary>
        /// <param name="gender">Either Male, Female, Unisex or Child</param>
        /// <param name="categories">List of categories of clothes</param>
        /// <param name="price">Tuple containing the price range</param>
        /// <param name="comparison">Type of the sorting parameter</param>
        /// <param name="ascending">Boolean to indicate the direction of the sorting algorithm</param>
        /// <returns>List of articles in fonction of the different filters</returns>
        public async Task <IEnumerable <DTOArticle> > GetArticles(string gender = null, string category = null,
                                                                  Tuple <float, float> price = null, int conditionMin          = 0,
                                                                  Comparison comparison      = Comparison.Date, bool ascending = false,
                                                                  string search = null, int page = 1, int pageSize = 12) // Filters
        {
            var res = new List <DTOArticle>();

            // Pagination var init
            var articleCount = await _articleRepo.Count();

            var pageStartIndex   = (page - 1) * pageSize;
            var pageCurrentIndex = 0;

            foreach (var element in await _articleRepo.Get())
            {
                if ((pageCurrentIndex < pageStartIndex) ||
                    (pageCurrentIndex >= page * pageSize))
                {
                    pageCurrentIndex++;
                    continue;
                }

                if (element.State.ToLower() != "free")
                {
                    continue;
                }
                if (search != null)
                {
                    if (element.Description.ToLower().Contains(search.ToLower()) ||
                        element.Name.ToLower().Contains(search.ToLower()) ||
                        element.Brand.ToLower().Contains(search.ToLower()))
                    {
                        continue;
                    }
                }
                if (gender != null)
                {
                    switch (gender)
                    {
                    case "child":
                        if (element.Sex.ToLower() != "child")
                        {
                            continue;
                        }
                        break;

                    case "man":
                        if (element.Sex.ToLower() != "man" && element.Sex.ToLower() != "unisex")
                        {
                            continue;
                        }
                        break;

                    case "woman":
                        if (element.Sex.ToLower() != "woman" && element.Sex.ToLower() != "unisex")
                        {
                            continue;
                        }
                        break;

                    case "unisex":
                        if (element.Sex.ToLower() != "unisex")
                        {
                            continue;
                        }
                        break;

                    default:
                        continue;
                    }
                }
                if (category != null)
                {
                    if (element.Category != category)
                    {
                        continue;
                    }
                }
                if (price != null)
                {
                    if (element.Price > price.Item2 || element.Price < price.Item1)
                    {
                        continue;
                    }
                }
                if (conditionMin > element.Condition)
                {
                    continue;
                }
                res.Add(element);
                pageCurrentIndex++;
            }
            // Filter OK
            switch (comparison)
            {
            case Comparison.Date:
                res = @ascending ? res.OrderBy(x => x.CreatedAt).ToList() : res.OrderByDescending(x => x.CreatedAt).ToList();
                break;

            case Comparison.Condition:
                res = @ascending ? res.OrderBy(x => x.Condition).ToList() : res.OrderByDescending(x => x.Condition).ToList();
                break;

            case Comparison.Price:
                res = @ascending ? res.OrderBy(x => x.Price).ToList() : res.OrderByDescending(x => x.Price).ToList();
                break;

            case Comparison.SellerRating:
                res = @ascending
                        ? res.OrderBy(x => x.User.Note).ToList()
                        : res.OrderByDescending(x => x.User.Note).ToList();
                break;
            }

            return(res);
        }