Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SolidAufgabe #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Spg.AloMalo.Application/Filter/ContainsFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class ContainsFilter<T> : Filter<T>

Check failure on line 1 in src/Spg.AloMalo.Application/Filter/ContainsFilter.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1 in src/Spg.AloMalo.Application/Filter/ContainsFilter.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)
{
private readonly Func<T, string> _propertySelector;
private readonly string _value;

public ContainsFilter(Func<T, string> propertySelector, string value)
{
_propertySelector = propertySelector;
_value = value;
}

public override bool Matches(T item)
{
return _propertySelector(item).Contains(_value);
}
}
16 changes: 16 additions & 0 deletions src/Spg.AloMalo.Application/Filter/EndsWithFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class EndsWithFilter<T> : Filter<T>

Check failure on line 1 in src/Spg.AloMalo.Application/Filter/EndsWithFilter.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1 in src/Spg.AloMalo.Application/Filter/EndsWithFilter.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)
{
private readonly Func<T, string> _propertySelector;
private readonly string _value;

public EndsWithFilter(Func<T, string> propertySelector, string value)
{
_propertySelector = propertySelector;
_value = value;
}

public override bool Matches(T item)
{
return _propertySelector(item).EndsWith(_value);
}
}
16 changes: 16 additions & 0 deletions src/Spg.AloMalo.Application/Filter/EqualsFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class EqualsFilter<T, TProperty> : Filter<T>

Check failure on line 1 in src/Spg.AloMalo.Application/Filter/EqualsFilter.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1 in src/Spg.AloMalo.Application/Filter/EqualsFilter.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)
{
private readonly Func<T, TProperty> _propertySelector;
private readonly TProperty _value;

public EqualsFilter(Func<T, TProperty> propertySelector, TProperty value)
{
_propertySelector = propertySelector;
_value = value;
}

public override bool Matches(T item)
{
return _propertySelector(item).Equals(_value);
}
}
25 changes: 25 additions & 0 deletions src/Spg.AloMalo.Application/Filter/FilterService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class FilterService<T>
{
private readonly List<Filter<T>> _filters;

Check failure on line 3 in src/Spg.AloMalo.Application/Filter/FilterService.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)

public FilterService()
{
_filters = new List<Filter<T>>();
}

public void AddFilter(Filter<T> filter)

Check failure on line 10 in src/Spg.AloMalo.Application/Filter/FilterService.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 10 in src/Spg.AloMalo.Application/Filter/FilterService.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)
{
_filters.Add(filter);
}

public IEnumerable<T> ApplyFilters(IEnumerable<T> items)
{
foreach (var item in items)
{
if (_filters.All(f => f.Matches(item)))
{
yield return item;
}
}
}
}
16 changes: 16 additions & 0 deletions src/Spg.AloMalo.Application/Filter/StartsWithFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class StartsWithFilter<T> : Filter<T>

Check failure on line 1 in src/Spg.AloMalo.Application/Filter/StartsWithFilter.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The type or namespace name 'Filter<>' could not be found (are you missing a using directive or an assembly reference?)
{
private readonly Func<T, string> _propertySelector;
private readonly string _value;

public StartsWithFilter(Func<T, string> propertySelector, string value)
{
_propertySelector = propertySelector;
_value = value;
}

public override bool Matches(T item)
{
return _propertySelector(item).StartsWith(_value);
}
}
26 changes: 26 additions & 0 deletions test/Spg.AloMalo.Api.Test/Helpers/FilterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

public class FilterTests
{
public FilterTests()
{
var persons = new List<Person>
{
new Person { FirstName = "John", LastName = "Doe", Age = 30, Email = "[email protected]" },
new Person { FirstName = "Jane", LastName = "Smith", Age = 25, Email = "[email protected]" }
};

var filterService = new FilterService<Person>();

filterService.AddFilter(new EqualsFilter<Person, string>(p => p.LastName, "Doe"));
filterService.AddFilter(new ContainsFilter<Person>(p => p.Email, "example"));

var filteredPersons = filterService.ApplyFilters(persons);

foreach (var person in filteredPersons)
{
Console.WriteLine($"{person.FirstName} {person.LastName}");
}

}
}
Loading