Files
duempelkas/src/Duempelkas.App/Services/EntryListQuery.cs
T

29 lines
1.4 KiB
C#

using System.Globalization;
using Duempelkas.App.Services.Models;
using Duempelkas.Domain.Enums;
namespace Duempelkas.App.Services;
public static class EntryListQuery
{
public static decimal SignedAmount(EntryDto entry) => entry.Type == EntryType.Income ? entry.Amount : -entry.Amount;
public static List<EntryDto> Apply(IEnumerable<EntryDto> entries, string search, bool showDeleted, string sort)
{
var terms = search.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var de = CultureInfo.GetCultureInfo("de-DE");
var matches = entries.Where(e => !e.IsDeleted || showDeleted).Where(e =>
{
var text = $"{e.Title} {e.DisplayId} {e.Date:dd.MM.yyyy} {e.Amount.ToString("N2", de)} {SignedAmount(e).ToString("0.00", de)} {e.LinkedAccountName}";
return terms.All(term => text.Contains(term, StringComparison.OrdinalIgnoreCase));
});
return (sort switch
{
"date-desc" => matches.OrderByDescending(e => e.Date).ThenByDescending(e => e.Id),
"amount-asc" => matches.OrderBy(SignedAmount).ThenBy(e => e.Id),
"amount-desc" => matches.OrderByDescending(SignedAmount).ThenBy(e => e.Id),
"title" => matches.OrderBy(e => e.Title, StringComparer.Create(de, true)).ThenBy(e => e.Id),
_ => matches.OrderBy(e => e.Date).ThenBy(e => e.Id)
}).ToList();
}
}