Skip to content

Commit

Permalink
fix workshop filtering by price (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlhaHoliak authored Jul 28, 2021
1 parent 28dd704 commit c03b299
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ private QueryContainer CreateQueryFromFilter(WorkshopFilterES filter)
};
}

if (filter.IsFree && !filter.IsPaid)
if (filter.IsFree && (filter.MinPrice == 0 && filter.MaxPrice == int.MaxValue))
{
queryContainer &= new TermQuery()
{
Field = Infer.Field<WorkshopES>(w => w.Price),
Value = 0,
};
}
else if (!filter.IsFree && filter.IsPaid)
else if (!filter.IsFree && !(filter.MinPrice == 0 || filter.MaxPrice == int.MaxValue))
{
queryContainer &= new NumericRangeQuery()
{
Expand All @@ -104,7 +104,7 @@ private QueryContainer CreateQueryFromFilter(WorkshopFilterES filter)
LessThanOrEqualTo = filter.MaxPrice,
};
}
else if (filter.IsFree && filter.IsPaid)
else if (filter.IsFree && !(filter.MinPrice == 0 || filter.MaxPrice == int.MaxValue))
{
var tempQuery = new QueryContainer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public class WorkshopFilterES

public bool IsFree { get; set; } = false;

public bool IsPaid { get; set; } = false;

public int MinPrice { get; set; } = 0;

public int MaxPrice { get; set; } = int.MaxValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ public void Mapping_WorkshopFilterDto_ToESModel_IsCorrect()
City = "City",
DirectionIds = new List<long>() { 1, 2 },
IsFree = false,
IsPaid = true,
MinPrice = 0,
MaxPrice = 20,
OrderByField = Enums.OrderBy.PriceDesc.ToString(),
Expand All @@ -151,7 +150,6 @@ public void Mapping_WorkshopFilterDto_ToESModel_IsCorrect()
Assert.AreEqual(filter.Ids, result.Ids);
Assert.AreEqual(filter.DirectionIds, result.DirectionIds);
Assert.AreEqual(filter.IsFree, result.IsFree);
Assert.AreEqual(filter.IsPaid, result.IsPaid);
Assert.AreEqual(filter.MinAge, result.MinAge);
Assert.AreEqual(filter.MaxAge, result.MaxAge);
Assert.AreEqual(filter.WithDisabilityOptions, result.WithDisabilityOptions);
Expand Down
2 changes: 0 additions & 2 deletions OutOfSchool/OutOfSchool.WebApi/Models/WorkshopFilterDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public class WorkshopFilterDto

public bool IsFree { get; set; } = false;

public bool IsPaid { get; set; } = false;

public int MinPrice { get; set; } = 0;

public int MaxPrice { get; set; } = int.MaxValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,15 @@ private Expression<Func<Workshop, bool>> PredicateBuild(WorkshopFilterDto filter
predicate = predicate.And(tempPredicate);
}

if (filter.MinPrice >= 0 && filter.MaxPrice < int.MaxValue)
if (filter.IsFree && (filter.MinPrice == 0 && filter.MaxPrice == int.MaxValue))
{
predicate = predicate.And(x => x.Price >= filter.MinPrice && x.Price <= filter.MaxPrice);
}

if (filter.IsFree && !filter.IsPaid)
{
predicate = predicate.And(x => x.Price == filter.MaxPrice);
predicate = predicate.And(x => x.Price == filter.MinPrice);
}
else if (!filter.IsFree && filter.IsPaid)
else if (!filter.IsFree && !(filter.MinPrice == 0 || filter.MaxPrice == int.MaxValue))
{
predicate = predicate.And(x => x.Price >= filter.MinPrice && x.Price <= filter.MaxPrice);
}
else if (filter.IsFree && filter.IsPaid)
else if (filter.IsFree && !(filter.MinPrice == 0 || filter.MaxPrice == int.MaxValue))
{
predicate = predicate.And(x => (x.Price >= filter.MinPrice && x.Price <= filter.MaxPrice) || x.Price == 0);
}
Expand Down

0 comments on commit c03b299

Please sign in to comment.