From f6117d45177762d87e798473094803ba49562269 Mon Sep 17 00:00:00 2001 From: Stan___Kudri <53861218+Stan-Kudri@users.noreply.github.com> Date: Fri, 16 Feb 2024 01:02:18 +0300 Subject: [PATCH 1/4] (Solution)WatchList - Add combobox MudSelect and change OrderBy in a project "MudBlazor". --- .../Model/Sortable/ISortableSmartEnum.cs | 9 ++++ .../Sortable/ISortableSmartEnumOperation.cs | 8 +++ .../Model/Sortable/SortFieldWatchItem.cs | 51 ++++++++++++++++++ .../Model/Sortable/SortWatchItem.cs | 26 +++++++++ WatchList.Core/Model/Sortable/Sorter.cs | 35 ++++++++++++ WatchList.Core/Model/Sorting/SortField.cs | 23 -------- .../Model/Sorting/WatchItemSortField.cs | 23 ++++++++ WatchList.Core/PageItem/ItemSearchRequest.cs | 36 +++++++++++++ .../PageItem/WatchItemSearchRequest.cs | 6 +-- .../Repository/WatchItemRepository.cs | 7 +++ .../DataLoading/DownloadDataService.cs | 2 +- WatchList.Core/Service/WatchItemService.cs | 2 + WatchList.MudBlazors/Model/SortModel.cs | 8 +-- .../Pages/WatchCinemaTable.razor | 16 ++++-- .../Pages/WatchCinemaTable.razor.cs | 19 ++++--- WatchList.MudBlazors/Program.cs | 3 ++ WatchList.MudBlazors/app.db-shm | Bin 32768 -> 32768 bytes WatchList.MudBlazors/app.db-wal | Bin 0 -> 32992 bytes .../FilterItemTest/FilterByStatusTest.cs | 2 +- .../FilterItemTest/FilterByTypeTest.cs | 2 +- WatchList.Test/CoreTest/SortFieldTest.cs | 14 ++--- .../CoreTest/WatchItemRepositoryTest.cs | 2 +- .../BindingItem/ModelBoxForm/SortModel.cs | 8 +-- 23 files changed, 247 insertions(+), 55 deletions(-) create mode 100644 WatchList.Core/Model/Sortable/ISortableSmartEnum.cs create mode 100644 WatchList.Core/Model/Sortable/ISortableSmartEnumOperation.cs create mode 100644 WatchList.Core/Model/Sortable/SortFieldWatchItem.cs create mode 100644 WatchList.Core/Model/Sortable/SortWatchItem.cs create mode 100644 WatchList.Core/Model/Sortable/Sorter.cs delete mode 100644 WatchList.Core/Model/Sorting/SortField.cs create mode 100644 WatchList.Core/Model/Sorting/WatchItemSortField.cs create mode 100644 WatchList.Core/PageItem/ItemSearchRequest.cs diff --git a/WatchList.Core/Model/Sortable/ISortableSmartEnum.cs b/WatchList.Core/Model/Sortable/ISortableSmartEnum.cs new file mode 100644 index 00000000..976f9175 --- /dev/null +++ b/WatchList.Core/Model/Sortable/ISortableSmartEnum.cs @@ -0,0 +1,9 @@ +namespace WatchList.Core.Model.Sortable +{ + public interface ISortableSmartEnum + { + IOrderedQueryable OrderBy(IQueryable query, bool asc); + + IOrderedQueryable ThenBy(IOrderedQueryable query, bool asc); + } +} diff --git a/WatchList.Core/Model/Sortable/ISortableSmartEnumOperation.cs b/WatchList.Core/Model/Sortable/ISortableSmartEnumOperation.cs new file mode 100644 index 00000000..864e6af4 --- /dev/null +++ b/WatchList.Core/Model/Sortable/ISortableSmartEnumOperation.cs @@ -0,0 +1,8 @@ +namespace WatchList.Core.Model.Sortable +{ + public interface ISortableSmartEnumOperation + { + static abstract ISortableSmartEnum DefaultValue { get; } + static abstract IReadOnlyCollection> List { get; } + } +} diff --git a/WatchList.Core/Model/Sortable/SortFieldWatchItem.cs b/WatchList.Core/Model/Sortable/SortFieldWatchItem.cs new file mode 100644 index 00000000..e1308f14 --- /dev/null +++ b/WatchList.Core/Model/Sortable/SortFieldWatchItem.cs @@ -0,0 +1,51 @@ +using System.Linq.Expressions; +using Ardalis.SmartEnum; +using WatchList.Core.Model.ItemCinema; +using WatchList.Core.Model.ItemCinema.Components; + +namespace WatchList.Core.Model.Sortable +{ + public abstract class SortFieldWatchItem : SmartEnum, ISortableSmartEnum, ISortableSmartEnumOperation + { + public static readonly SortFieldWatchItem Title = new SortType("Title", 0, e => e.Title); + public static readonly SortFieldWatchItem Sequel = new SortType("Sequel", 1, e => e.Sequel); + public static readonly SortFieldWatchItem Status = new SortType("Status", 2, e => e.Status); + public static readonly SortFieldWatchItem Data = new SortType("Data", 3, e => e.Date); + public static readonly SortFieldWatchItem Grade = new SortType("Grade", 4, e => e.Grade); + public static readonly SortFieldWatchItem Type = new SortType("Type", 5, e => e.Type); + + private readonly Func, IOrderedQueryable> _orderByField; + + private SortFieldWatchItem(string name, int value) + : base(name, value) + { + } + + static ISortableSmartEnum ISortableSmartEnumOperation.DefaultValue => Title; + + static IReadOnlyCollection> ISortableSmartEnumOperation.List => List; + + public abstract IOrderedQueryable OrderBy(IQueryable query, bool asc); + + public abstract IOrderedQueryable ThenBy(IOrderedQueryable query, bool asc); + + public override string ToString() => Name; + + private sealed class SortType : SortFieldWatchItem + { + private readonly Expression> _expression; + + public SortType(string name, int value, Expression> expression) + : base(name, value) + { + _expression = expression; + } + + public override IOrderedQueryable OrderBy(IQueryable query, bool asc) + => asc ? query.OrderBy(_expression) : query.OrderByDescending(_expression); + + public override IOrderedQueryable ThenBy(IOrderedQueryable query, bool asc) + => asc ? query.ThenBy(_expression) : query.ThenByDescending(_expression); + } + } +} diff --git a/WatchList.Core/Model/Sortable/SortWatchItem.cs b/WatchList.Core/Model/Sortable/SortWatchItem.cs new file mode 100644 index 00000000..95591c4d --- /dev/null +++ b/WatchList.Core/Model/Sortable/SortWatchItem.cs @@ -0,0 +1,26 @@ +using System.Collections.ObjectModel; +using Ardalis.SmartEnum; + +namespace WatchList.Core.Model.Sortable +{ + public class SortWatchItem + where TSortField : SmartEnum, ISortableSmartEnum, ISortableSmartEnumOperation + { + public ISortableSmartEnum SortField { get; set; } + + public IEnumerable> SortFields { get; set; } = new HashSet>() { TSortField.DefaultValue }; + + public ObservableCollection> Items { get; set; } = new ObservableCollection>(TSortField.List); + + public IQueryable Apply(IQueryable items, bool? ascending = true) + { + var sorter = new Sorter(TSortField.DefaultValue); + return sorter.Apply(items, SortFields, ascending); + } + + public void Clear() + { + SortFields = new HashSet>() { TSortField.DefaultValue }; + } + } +} diff --git a/WatchList.Core/Model/Sortable/Sorter.cs b/WatchList.Core/Model/Sortable/Sorter.cs new file mode 100644 index 00000000..bcf99617 --- /dev/null +++ b/WatchList.Core/Model/Sortable/Sorter.cs @@ -0,0 +1,35 @@ +namespace WatchList.Core.Model.Sortable +{ + public sealed class Sorter + { + private readonly ISortableSmartEnum _defaultValue; + + public Sorter(ISortableSmartEnum defaultValue) + { + _defaultValue = defaultValue; + } + + public IQueryable Apply(IQueryable items, IEnumerable> sortFields, bool? ascending = true) + { + if (ascending == null) + { + return items; + } + + var asc = ascending.Value; + var actualSortFields = sortFields.ToList(); + if (actualSortFields.Count == 0) + { + actualSortFields.Add(_defaultValue); + } + + var query = actualSortFields[0].OrderBy(items, asc); + foreach (var item in actualSortFields.Skip(1)) + { + query = item.ThenBy(query, asc); + } + + return query; + } + } +} diff --git a/WatchList.Core/Model/Sorting/SortField.cs b/WatchList.Core/Model/Sorting/SortField.cs deleted file mode 100644 index 5c5e86a7..00000000 --- a/WatchList.Core/Model/Sorting/SortField.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Ardalis.SmartEnum; -using WatchList.Core.Model.ItemCinema; - -namespace WatchList.Core.Model.Sorting -{ - public class SortField : SmartEnum - { - public static readonly SortField Title = new SortField("Title", 0, x => x.OrderBy(y => y.Title)); - public static readonly SortField Sequel = new SortField("Sequel", 1, x => x.OrderBy(y => y.Sequel)); - public static readonly SortField Status = new SortField("Status", 2, x => x.OrderBy(y => y.Status)); - public static readonly SortField Data = new SortField("Data", 3, x => x.OrderByDescending(y => y.Date)); - public static readonly SortField Grade = new SortField("Grade", 4, x => x.OrderByDescending(y => y.Grade)); - public static readonly SortField Type = new SortField("Type", 5, x => x.OrderBy(y => y.Type)); - - private readonly Func, IOrderedQueryable> _orderByField; - - private SortField(string name, int value, Func, IOrderedQueryable> orderByField) - : base(name, value) - => _orderByField = orderByField; - - public IOrderedQueryable Apply(IQueryable items) => _orderByField(items); - } -} diff --git a/WatchList.Core/Model/Sorting/WatchItemSortField.cs b/WatchList.Core/Model/Sorting/WatchItemSortField.cs new file mode 100644 index 00000000..d31250cd --- /dev/null +++ b/WatchList.Core/Model/Sorting/WatchItemSortField.cs @@ -0,0 +1,23 @@ +using Ardalis.SmartEnum; +using WatchList.Core.Model.ItemCinema; + +namespace WatchList.Core.Model.Sorting +{ + public class WatchItemSortField : SmartEnum + { + public static readonly WatchItemSortField Title = new WatchItemSortField("Title", 0, x => x.OrderBy(y => y.Title)); + public static readonly WatchItemSortField Sequel = new WatchItemSortField("Sequel", 1, x => x.OrderBy(y => y.Sequel)); + public static readonly WatchItemSortField Status = new WatchItemSortField("Status", 2, x => x.OrderBy(y => y.Status)); + public static readonly WatchItemSortField Data = new WatchItemSortField("Data", 3, x => x.OrderByDescending(y => y.Date)); + public static readonly WatchItemSortField Grade = new WatchItemSortField("Grade", 4, x => x.OrderByDescending(y => y.Grade)); + public static readonly WatchItemSortField Type = new WatchItemSortField("Type", 5, x => x.OrderBy(y => y.Type)); + + private readonly Func, IOrderedQueryable> _orderByField; + + private WatchItemSortField(string name, int value, Func, IOrderedQueryable> orderByField) + : base(name, value) + => _orderByField = orderByField; + + public IOrderedQueryable Apply(IQueryable items) => _orderByField(items); + } +} diff --git a/WatchList.Core/PageItem/ItemSearchRequest.cs b/WatchList.Core/PageItem/ItemSearchRequest.cs new file mode 100644 index 00000000..c9c5ae66 --- /dev/null +++ b/WatchList.Core/PageItem/ItemSearchRequest.cs @@ -0,0 +1,36 @@ +using WatchList.Core.Model.Filter; +using WatchList.Core.Model.ItemCinema; +using WatchList.Core.Model.Sortable; + +namespace WatchList.Core.PageItem +{ + public class ItemSearchRequest + { + public ItemSearchRequest() + : this(new FilterItem(), new SortWatchItem(), new Page(), true) + { + } + + public ItemSearchRequest(FilterItem filter, SortWatchItem sort, Page page, bool isAscending = true) + { + Filter = filter; + Sort = sort; + Page = page; + IsAscending = isAscending; + } + + public FilterItem Filter { get; set; } + + public SortWatchItem Sort { get; set; } + + public Page Page { get; set; } + + public bool IsAscending { get; set; } = true; + + public bool CompareFilter(FilterItem filter) => Filter.Equals(filter); + + public IQueryable ApplyFilter(IQueryable items) => Filter.Apply(items); + + public IQueryable ApplyOrderBy(IQueryable items) => Sort.Apply(items, IsAscending); + } +} diff --git a/WatchList.Core/PageItem/WatchItemSearchRequest.cs b/WatchList.Core/PageItem/WatchItemSearchRequest.cs index cfe53e01..cf8f9dd4 100644 --- a/WatchList.Core/PageItem/WatchItemSearchRequest.cs +++ b/WatchList.Core/PageItem/WatchItemSearchRequest.cs @@ -7,11 +7,11 @@ namespace WatchList.Core.PageItem public class WatchItemSearchRequest { public WatchItemSearchRequest() - : this(new FilterItem(), SortField.Title, new Page()) + : this(new FilterItem(), WatchItemSortField.Title, new Page()) { } - public WatchItemSearchRequest(FilterItem filter, SortField sort, Page page) + public WatchItemSearchRequest(FilterItem filter, WatchItemSortField sort, Page page) { Filter = filter; Sort = sort; @@ -20,7 +20,7 @@ public WatchItemSearchRequest(FilterItem filter, SortField sort, Page page) public FilterItem Filter { get; set; } - public SortField Sort { get; set; } + public WatchItemSortField Sort { get; set; } public Page Page { get; set; } diff --git a/WatchList.Core/Repository/WatchItemRepository.cs b/WatchList.Core/Repository/WatchItemRepository.cs index a6e9bff8..623dfdc8 100644 --- a/WatchList.Core/Repository/WatchItemRepository.cs +++ b/WatchList.Core/Repository/WatchItemRepository.cs @@ -25,6 +25,13 @@ public PagedList GetPage(WatchItemSearchRequest searchRequest) return query.GetPagedList(searchRequest.Page); } + public PagedList GetPage(ItemSearchRequest searchRequest) + { + var query = searchRequest.ApplyFilter(_db.WatchItem); + query = searchRequest.ApplyOrderBy(query); + return query.GetPagedList(searchRequest.Page); + } + public void Add(WatchItem item) { item.Id = _db.ReplaceIdIsNotFree(item); diff --git a/WatchList.Core/Service/DataLoading/DownloadDataService.cs b/WatchList.Core/Service/DataLoading/DownloadDataService.cs index 3c3b7929..6a5c3255 100644 --- a/WatchList.Core/Service/DataLoading/DownloadDataService.cs +++ b/WatchList.Core/Service/DataLoading/DownloadDataService.cs @@ -27,7 +27,7 @@ public DownloadDataService(WatchItemRepository repository, IMessageBox messageBo public void Download(WatchItemRepository repository, ILoadRule loadRule) { - var searchRequest = new WatchItemSearchRequest(new FilterItem(), SortField.Title, new Page(1, NumberOfItemPerPage)); + var searchRequest = new WatchItemSearchRequest(new FilterItem(), WatchItemSortField.Title, new Page(1, NumberOfItemPerPage)); var pagedList = repository.GetPage(searchRequest); while (searchRequest.Page.Number <= pagedList.PageCount) diff --git a/WatchList.Core/Service/WatchItemService.cs b/WatchList.Core/Service/WatchItemService.cs index 6466f804..318e40f5 100644 --- a/WatchList.Core/Service/WatchItemService.cs +++ b/WatchList.Core/Service/WatchItemService.cs @@ -21,6 +21,8 @@ public WatchItemService(WatchItemRepository itemRepository, IMessageBox messageB public PagedList GetPage(WatchItemSearchRequest itemSearchRequest) => _repository.GetPage(itemSearchRequest); + public PagedList GetPage(ItemSearchRequest itemSearchRequest) => _repository.GetPage(itemSearchRequest); + public void Remove(Guid id) => _repository.Remove(id); public async Task AddAsync(WatchItem item) diff --git a/WatchList.MudBlazors/Model/SortModel.cs b/WatchList.MudBlazors/Model/SortModel.cs index 405cfe17..ce82f468 100644 --- a/WatchList.MudBlazors/Model/SortModel.cs +++ b/WatchList.MudBlazors/Model/SortModel.cs @@ -5,12 +5,12 @@ namespace WatchList.MudBlazors.Model { public class SortModel { - public ObservableCollection Items { get; set; } = new ObservableCollection(SortField.List); + public ObservableCollection Items { get; set; } = new ObservableCollection(WatchItemSortField.List); - public SortField Type { get; set; } = SortField.Title; + public WatchItemSortField Type { get; set; } = WatchItemSortField.Title; - public SortField GetSortItem() => Type; + public WatchItemSortField GetSortItem() => Type; - public void Clear() => Type = SortField.Title; + public void Clear() => Type = WatchItemSortField.Title; } } diff --git a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor index a2dab5ba..0fbeb8c9 100644 --- a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor +++ b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor @@ -1,6 +1,7 @@ @using Bromix.MudBlazor.MaterialDesignIcons @using WatchList.Core.Model.Filter.Components @using WatchList.Core.Model.ItemCinema +@using WatchList.Core.Model.Sortable @using WatchList.Core.Model.Sorting @page "/watchlist" @@ -11,15 +12,22 @@ -
- - @foreach (var item in Sort.Items.ToList()) + + @foreach (var item in SortField.Items) { - + }
+ +
+ + +
diff --git a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor.cs b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor.cs index 95ae72fb..e023070c 100644 --- a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor.cs +++ b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor.cs @@ -2,7 +2,7 @@ using MudBlazor; using WatchList.Core.Model.Filter; using WatchList.Core.Model.ItemCinema; -using WatchList.Core.Model.Sorting; +using WatchList.Core.Model.Sortable; using WatchList.Core.PageItem; using WatchList.Core.Service; using WatchList.Core.Service.Component; @@ -20,6 +20,7 @@ public partial class WatchCinemaTable [Inject] WatchItemService WatchItemService { get; set; } = null!; [Inject] IDialogService DialogService { get; set; } = null!; [Inject] IMessageBox MessageBoxDialog { get; set; } = null!; + [Inject] SortWatchItem SortField { get; set; } = null!; private readonly PageModel _pageModel = new PageModel(); @@ -27,15 +28,15 @@ public partial class WatchCinemaTable private HashSet _selectedItems = new HashSet(); private bool _isSelectItems = true; - private WatchItemSearchRequest _itemsSearchRequest = new WatchItemSearchRequest(); + private ItemSearchRequest _itemsSearchRequest = new ItemSearchRequest(); private PagedList? _pagedList = null; private FilterModel Filter { get; set; } = new FilterModel(); - private SortModel Sort { get; set; } = new SortModel(); + //private SortModel Sort { get; set; } = new SortModel(); protected override void OnInitialized() { - _itemsSearchRequest = new WatchItemSearchRequest(new FilterItem(), SortField.Title, _pageModel); + _itemsSearchRequest = new ItemSearchRequest(new FilterItem(), SortField, _pageModel); LoadData(); } @@ -85,7 +86,6 @@ private async Task RemoveItemDialog(Guid id) private void LoadData() { _itemsSearchRequest.Filter = Filter.GetFilter(); - _itemsSearchRequest.Sort = Sort.GetSortItem(); _pagedList = WatchItemService.GetPage(_itemsSearchRequest); _items = _pagedList.Items; StateHasChanged(); @@ -99,8 +99,15 @@ private void OnSelectItems(HashSet items) private void ClearFilter() { + _itemsSearchRequest.IsAscending = true; Filter.Clear(); - Sort.Clear(); + SortField.Clear(); + LoadData(); + } + + public void OnToggledChanged(bool toggled) + { + _itemsSearchRequest.IsAscending = toggled; LoadData(); } } diff --git a/WatchList.MudBlazors/Program.cs b/WatchList.MudBlazors/Program.cs index d8fcf2d7..1f303e6f 100644 --- a/WatchList.MudBlazors/Program.cs +++ b/WatchList.MudBlazors/Program.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.Hosting.StaticWebAssets; using MudBlazor.Services; using WatchList.Core.Logger; +using WatchList.Core.Model.ItemCinema; +using WatchList.Core.Model.Sortable; using WatchList.Core.Repository; using WatchList.Core.Repository.Db; using WatchList.Core.Service; @@ -23,6 +25,7 @@ builder.Services.AddScoped(e => new WatchItemRepository(e.GetRequiredService(), e.GetRequiredService())); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped>(); builder.Logging.AddConsole(); var app = builder.Build(); diff --git a/WatchList.MudBlazors/app.db-shm b/WatchList.MudBlazors/app.db-shm index fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10..b3ac11c5b07930d6bf0ddc6c061759585ef490af 100644 GIT binary patch delta 205 zcmZo@U}|V!s+V}A%K!t63=9GsKn@!aGp}Shu=?b&NTu9ZVY9bRX|5Aoi@j>Z-APps zGaF>?e5au6GZzp*jco1GCT#>&94vGF?-GZO>b#>U@F%*+hz I8yh+G0FT!!AOHXW delta 89 zcmZo@U}|V!;+1%$%K!t66E{kWTChv7nNGgVi7uf4lYpuIj|MhwbobtPz|jN%(k&k? diff --git a/WatchList.MudBlazors/app.db-wal b/WatchList.MudBlazors/app.db-wal index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..4581a1cc688b670722e04513965ee876f18a5faa 100644 GIT binary patch literal 32992 zcmeI*&ubiI7zgm#OfuO?vYAw^tDrda(n@&YecyTKop-7jW@cv)u?LY&YcZ6fDJI%% z6UCMuw9P?5FD-cYED8;^LR<02N$o{u4~kIm;K?3(@Gq$E%#!UUY3PEIhV6IQg`M4* zdG`IxB(TrE&$GdY)~Z4NdcrWKjAGe$a_&)iJ3jGuid;Lt@ju1h82Wz!dN$~mH)7DbLV+us$NE`XNm{O;Ssy3*DGG7v(6Jq zXr$k}P?V8a(3Hq1;ao&47McHQYiZ$)o{x9p*>gxS4i z%)QhPIi;Q?GS$0^La;5aCTD}*b*9|gMY_hVlSh1Va<#DG&eSLVT z;-n#!)W=OiJ(j>D)Kh61s5EMD`r`4W_vTmJBW~xxy~e@2A*ye6wm#!_9_TTQ6j$_m zzF_g#%~x7`hL4XGFF3+*=A28;N6sIP&?{IV009U<00Izz00bZa0SG_<0s}4ZtP~#g z^v7@F3l`|l;jvCuNEwmX&sdhoOf>|n9x$7gGRtwFtj0*OVL!>A@`(LpX-119_LKLp z_M1(+pQM65omO#Q&kO87bfi7D@2R;lr>*k>7Y*mO^RvzjT-2F?O9OrCF*pc700Izz z00bZa0SG_<0uX?}ZV-6doT+R(*`r!FJ^kvInG2R})+^<_iQ>!uQ9TvlF_Tm_Pu>`P z#GJOb&KD@^2)cQJ%g>yj{r$OQ3Uvg#;bOphfB*y_009U<00Izz00ah7U}x$G#hNGkn|2o~zPfBgiZB8rrrMV|U;V{^5=%QqH`>oR|zu%2q} z&0pEN9C-os7g+BK(P_`HtNII!AH8+>tC!#2Q}h?GobgV7f$>g%YA;@%68@sOT?XIcIzO3!EM3vyZ_+00Izz z00bZa0SG_<0uX=z1a?&5ag%N70N`@_0rwa9arg`KwV!T$i8_KEy%-P-0uX=z1Rwwb e2tWV=5O{C`|8pI|=HKnBqK*K+Q@E>tr|@4nV&P8! literal 0 HcmV?d00001 diff --git a/WatchList.Test/CoreTest/FilterItemTest/FilterByStatusTest.cs b/WatchList.Test/CoreTest/FilterItemTest/FilterByStatusTest.cs index 9757c8b7..52096283 100644 --- a/WatchList.Test/CoreTest/FilterItemTest/FilterByStatusTest.cs +++ b/WatchList.Test/CoreTest/FilterItemTest/FilterByStatusTest.cs @@ -124,7 +124,7 @@ private void CheckListEqualAfterApplyFilter(List watchItems, StatusFi { // Arrange var dbContext = new TestAppDbContextFactory().Create(); - var watchItemSearchRequest = new WatchItemSearchRequest(new FilterItem(TypeFilter.AllCinema, statusFilter), SortField.Title, new Page()); + var watchItemSearchRequest = new WatchItemSearchRequest(new FilterItem(TypeFilter.AllCinema, statusFilter), WatchItemSortField.Title, new Page()); dbContext.AddRange(watchItems); dbContext.SaveChanges(); diff --git a/WatchList.Test/CoreTest/FilterItemTest/FilterByTypeTest.cs b/WatchList.Test/CoreTest/FilterItemTest/FilterByTypeTest.cs index f03eb9dc..fdcefa72 100644 --- a/WatchList.Test/CoreTest/FilterItemTest/FilterByTypeTest.cs +++ b/WatchList.Test/CoreTest/FilterItemTest/FilterByTypeTest.cs @@ -124,7 +124,7 @@ private void CheckListEqualAfterApplyFilter(List watchItems, TypeFilt { // Arrange var dbContext = new TestAppDbContextFactory().Create(); - var watchItemSearchRequest = new WatchItemSearchRequest(new FilterItem(typeFilter, StatusFilter.AllCinema), SortField.Title, new Page()); + var watchItemSearchRequest = new WatchItemSearchRequest(new FilterItem(typeFilter, StatusFilter.AllCinema), WatchItemSortField.Title, new Page()); dbContext.AddRange(watchItems); dbContext.SaveChanges(); diff --git a/WatchList.Test/CoreTest/SortFieldTest.cs b/WatchList.Test/CoreTest/SortFieldTest.cs index a700939f..29cb071f 100644 --- a/WatchList.Test/CoreTest/SortFieldTest.cs +++ b/WatchList.Test/CoreTest/SortFieldTest.cs @@ -168,7 +168,7 @@ public class SortFieldTest public void Order_List_Items_By_Title(List watchItems, List expectWatchItem) { // Assert - CheckListEqualAfterApplyOrderBy(watchItems, SortField.Title, expectWatchItem); + CheckListEqualAfterApplyOrderBy(watchItems, WatchItemSortField.Title, expectWatchItem); } [Theory] @@ -176,7 +176,7 @@ public void Order_List_Items_By_Title(List watchItems, List watchItems, List expectWatchItem) { // Assert - CheckListEqualAfterApplyOrderBy(watchItems, SortField.Sequel, expectWatchItem); + CheckListEqualAfterApplyOrderBy(watchItems, WatchItemSortField.Sequel, expectWatchItem); } [Theory] @@ -184,7 +184,7 @@ public void Order_List_Items_By_Sequel(List watchItems, List watchItems, List expectWatchItem) { // Assert - CheckListEqualAfterApplyOrderBy(watchItems, SortField.Status, expectWatchItem); + CheckListEqualAfterApplyOrderBy(watchItems, WatchItemSortField.Status, expectWatchItem); } [Theory] @@ -192,7 +192,7 @@ public void Order_List_Items_By_Status(List watchItems, List watchItems, List expectWatchItem) { // Assert - CheckListEqualAfterApplyOrderBy(watchItems, SortField.Data, expectWatchItem); + CheckListEqualAfterApplyOrderBy(watchItems, WatchItemSortField.Data, expectWatchItem); } [Theory] @@ -200,7 +200,7 @@ public void Order_List_Items_By_Data(List watchItems, List public void Order_List_Items_By_Grade(List watchItems, List expectWatchItem) { // Assert - CheckListEqualAfterApplyOrderBy(watchItems, SortField.Grade, expectWatchItem); + CheckListEqualAfterApplyOrderBy(watchItems, WatchItemSortField.Grade, expectWatchItem); } [Theory] @@ -208,10 +208,10 @@ public void Order_List_Items_By_Grade(List watchItems, List watchItems, List expectWatchItem) { // Assert - CheckListEqualAfterApplyOrderBy(watchItems, SortField.Type, expectWatchItem); + CheckListEqualAfterApplyOrderBy(watchItems, WatchItemSortField.Type, expectWatchItem); } - private void CheckListEqualAfterApplyOrderBy(List watchItems, SortField sortField, List expectWatchItem) + private void CheckListEqualAfterApplyOrderBy(List watchItems, WatchItemSortField sortField, List expectWatchItem) { // Arrange var appDbContext = new TestAppDbContextFactory().Create(); diff --git a/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs b/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs index 3f2e7b3f..fd6b4a96 100644 --- a/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs +++ b/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs @@ -28,7 +28,7 @@ public class WatchItemRepositoryTest new WatchItemSearchRequest( new FilterItem(TypeFilter.Movie, StatusFilter.ViewedCinema), - SortField.Grade, + WatchItemSortField.Grade, new Page(1, 5)), new List() diff --git a/WatchList.WinForms/BindingItem/ModelBoxForm/SortModel.cs b/WatchList.WinForms/BindingItem/ModelBoxForm/SortModel.cs index d9b7aa3b..192bd248 100644 --- a/WatchList.WinForms/BindingItem/ModelBoxForm/SortModel.cs +++ b/WatchList.WinForms/BindingItem/ModelBoxForm/SortModel.cs @@ -5,16 +5,16 @@ namespace WatchList.WinForms.BindingItem.ModelBoxForm { public class SortModel : ModelBase { - private SortField _type = SortField.Title; + private WatchItemSortField _type = WatchItemSortField.Title; - public ObservableCollection Items { get; set; } = new ObservableCollection(SortField.List); + public ObservableCollection Items { get; set; } = new ObservableCollection(WatchItemSortField.List); - public SortField Type + public WatchItemSortField Type { get => _type; set => SetField(ref _type, value); } - public SortField GetSortItem() => Type; + public WatchItemSortField GetSortItem() => Type; } } From 7e1924f0e8b8ddf376ca442fff3c3d590b261367 Mon Sep 17 00:00:00 2001 From: Stan___Kudri <53861218+Stan-Kudri@users.noreply.github.com> Date: Fri, 16 Feb 2024 21:47:31 +0300 Subject: [PATCH 2/4] WatchList.Core - Fixed linter. --- WatchList.Core/Model/Sortable/ISortableSmartEnumOperation.cs | 1 + WatchList.Core/Model/Sortable/SortFieldWatchItem.cs | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/WatchList.Core/Model/Sortable/ISortableSmartEnumOperation.cs b/WatchList.Core/Model/Sortable/ISortableSmartEnumOperation.cs index 864e6af4..f8add53b 100644 --- a/WatchList.Core/Model/Sortable/ISortableSmartEnumOperation.cs +++ b/WatchList.Core/Model/Sortable/ISortableSmartEnumOperation.cs @@ -3,6 +3,7 @@ namespace WatchList.Core.Model.Sortable public interface ISortableSmartEnumOperation { static abstract ISortableSmartEnum DefaultValue { get; } + static abstract IReadOnlyCollection> List { get; } } } diff --git a/WatchList.Core/Model/Sortable/SortFieldWatchItem.cs b/WatchList.Core/Model/Sortable/SortFieldWatchItem.cs index e1308f14..91d926e1 100644 --- a/WatchList.Core/Model/Sortable/SortFieldWatchItem.cs +++ b/WatchList.Core/Model/Sortable/SortFieldWatchItem.cs @@ -14,8 +14,6 @@ public abstract class SortFieldWatchItem : SmartEnum, ISorta public static readonly SortFieldWatchItem Grade = new SortType("Grade", 4, e => e.Grade); public static readonly SortFieldWatchItem Type = new SortType("Type", 5, e => e.Type); - private readonly Func, IOrderedQueryable> _orderByField; - private SortFieldWatchItem(string name, int value) : base(name, value) { From 2da0763b3b83c45ce8d8ca7548549979fd50d4fa Mon Sep 17 00:00:00 2001 From: Stan___Kudri <53861218+Stan-Kudri@users.noreply.github.com> Date: Sat, 17 Feb 2024 08:35:01 +0300 Subject: [PATCH 3/4] WatchList - Add filter MudSelector in project "MudBlazor". --- .../Model/Filter/FilterWatchItem.cs | 50 +++++++++++++++++++ .../Model/Sortable/SortWatchItem.cs | 7 +-- WatchList.Core/PageItem/ItemSearchRequest.cs | 6 +-- WatchList.MudBlazors/Model/FilterModel.cs | 28 ----------- .../Pages/WatchCinemaTable.razor | 22 ++++---- .../Pages/WatchCinemaTable.razor.cs | 9 ++-- WatchList.MudBlazors/Program.cs | 2 + 7 files changed, 73 insertions(+), 51 deletions(-) create mode 100644 WatchList.Core/Model/Filter/FilterWatchItem.cs delete mode 100644 WatchList.MudBlazors/Model/FilterModel.cs diff --git a/WatchList.Core/Model/Filter/FilterWatchItem.cs b/WatchList.Core/Model/Filter/FilterWatchItem.cs new file mode 100644 index 00000000..9db238c5 --- /dev/null +++ b/WatchList.Core/Model/Filter/FilterWatchItem.cs @@ -0,0 +1,50 @@ +using System.Collections.ObjectModel; +using WatchList.Core.Model.ItemCinema; +using WatchList.Core.Model.ItemCinema.Components; + +namespace WatchList.Core.Model.Filter +{ + public class FilterWatchItem : IEquatable + { + public FilterWatchItem() + { + } + + public IEnumerable FilterTypeField { get; set; } + = new HashSet(TypeCinema.List.Where(e => e != TypeCinema.AllType)); + + public IEnumerable FilterStatusField { get; set; } + = new HashSet(StatusCinema.List.Where(e => e != StatusCinema.AllStatus)); + + public ObservableCollection TypeItems { get; set; } + = new ObservableCollection(TypeCinema.List.Where(e => e != TypeCinema.AllType)); + + public ObservableCollection StatusItems { get; set; } + = new ObservableCollection(StatusCinema.List.Where(e => e != StatusCinema.AllStatus)); + + public TypeCinema TypeFilter { get; set; } + + public StatusCinema StatusFilter { get; set; } + + public IQueryable Apply(IQueryable items) + { + items = items.Where(x => FilterTypeField.Contains(x.Type)); + items = items.Where(x => FilterStatusField.Contains(x.Status)); + + return items; + } + + public void Clear() + { + FilterTypeField = new HashSet(TypeCinema.List.Where(e => e != TypeCinema.AllType)); + FilterStatusField = new HashSet(StatusCinema.List.Where(e => e != StatusCinema.AllStatus)); + } + + public override int GetHashCode() => HashCode.Combine(FilterTypeField, FilterStatusField); + + public override bool Equals(object? obj) => Equals(obj as FilterWatchItem); + + public bool Equals(FilterWatchItem? other) + => other != null && FilterTypeField == other.FilterTypeField && FilterStatusField == other.FilterStatusField; + } +} diff --git a/WatchList.Core/Model/Sortable/SortWatchItem.cs b/WatchList.Core/Model/Sortable/SortWatchItem.cs index 95591c4d..f932bc0d 100644 --- a/WatchList.Core/Model/Sortable/SortWatchItem.cs +++ b/WatchList.Core/Model/Sortable/SortWatchItem.cs @@ -19,8 +19,9 @@ public IQueryable Apply(IQueryable items, bool? ascending = true) } public void Clear() - { - SortFields = new HashSet>() { TSortField.DefaultValue }; - } + => SortFields = new HashSet>() + { + TSortField.DefaultValue, + }; } } diff --git a/WatchList.Core/PageItem/ItemSearchRequest.cs b/WatchList.Core/PageItem/ItemSearchRequest.cs index c9c5ae66..558ea4cd 100644 --- a/WatchList.Core/PageItem/ItemSearchRequest.cs +++ b/WatchList.Core/PageItem/ItemSearchRequest.cs @@ -7,11 +7,11 @@ namespace WatchList.Core.PageItem public class ItemSearchRequest { public ItemSearchRequest() - : this(new FilterItem(), new SortWatchItem(), new Page(), true) + : this(new FilterWatchItem(), new SortWatchItem(), new Page(), true) { } - public ItemSearchRequest(FilterItem filter, SortWatchItem sort, Page page, bool isAscending = true) + public ItemSearchRequest(FilterWatchItem filter, SortWatchItem sort, Page page, bool isAscending = true) { Filter = filter; Sort = sort; @@ -19,7 +19,7 @@ public ItemSearchRequest(FilterItem filter, SortWatchItem Sort { get; set; } diff --git a/WatchList.MudBlazors/Model/FilterModel.cs b/WatchList.MudBlazors/Model/FilterModel.cs deleted file mode 100644 index 2f926ee6..00000000 --- a/WatchList.MudBlazors/Model/FilterModel.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System.Collections.ObjectModel; -using Microsoft.AspNetCore.Components; -using WatchList.Core.Model.Filter; -using WatchList.Core.Model.Filter.Components; - -namespace WatchList.MudBlazors.Model -{ - public class FilterModel - { - public ObservableCollection TypeItem { get; set; } = new ObservableCollection(TypeFilter.List); - - public ObservableCollection StatusItem { get; set; } = new ObservableCollection(StatusFilter.List); - - [Parameter] - public TypeFilter Type { get; set; } = TypeFilter.AllCinema; - - [Parameter] - public StatusFilter Status { get; set; } = StatusFilter.AllCinema; - - public FilterItem GetFilter() => new FilterItem(Type, Status); - - public void Clear() - { - Type = TypeFilter.AllCinema; - Status = StatusFilter.AllCinema; - } - } -} diff --git a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor index 0fbeb8c9..0bd569e4 100644 --- a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor +++ b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor @@ -1,6 +1,7 @@ @using Bromix.MudBlazor.MaterialDesignIcons @using WatchList.Core.Model.Filter.Components @using WatchList.Core.Model.ItemCinema +@using WatchList.Core.Model.ItemCinema.Components @using WatchList.Core.Model.Sortable @using WatchList.Core.Model.Sorting @@ -31,27 +32,26 @@ -
- - @foreach (var item in Filter.TypeItem.ToList()) +
+ + @foreach (var item in FilterWatchItem.TypeItems) { - + }
-
- - @foreach (var item in Filter.StatusItem.ToList()) +
+ + @foreach (var item in FilterWatchItem.StatusItems) { - + }
- - - diff --git a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor.cs b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor.cs index e023070c..768285e2 100644 --- a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor.cs +++ b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor.cs @@ -21,6 +21,7 @@ public partial class WatchCinemaTable [Inject] IDialogService DialogService { get; set; } = null!; [Inject] IMessageBox MessageBoxDialog { get; set; } = null!; [Inject] SortWatchItem SortField { get; set; } = null!; + [Inject] FilterWatchItem FilterWatchItem { get; set; } = null!; private readonly PageModel _pageModel = new PageModel(); @@ -31,12 +32,9 @@ public partial class WatchCinemaTable private ItemSearchRequest _itemsSearchRequest = new ItemSearchRequest(); private PagedList? _pagedList = null; - private FilterModel Filter { get; set; } = new FilterModel(); - //private SortModel Sort { get; set; } = new SortModel(); - protected override void OnInitialized() { - _itemsSearchRequest = new ItemSearchRequest(new FilterItem(), SortField, _pageModel); + _itemsSearchRequest = new ItemSearchRequest(FilterWatchItem, SortField, _pageModel); LoadData(); } @@ -85,7 +83,6 @@ private async Task RemoveItemDialog(Guid id) private void LoadData() { - _itemsSearchRequest.Filter = Filter.GetFilter(); _pagedList = WatchItemService.GetPage(_itemsSearchRequest); _items = _pagedList.Items; StateHasChanged(); @@ -100,7 +97,7 @@ private void OnSelectItems(HashSet items) private void ClearFilter() { _itemsSearchRequest.IsAscending = true; - Filter.Clear(); + FilterWatchItem.Clear(); SortField.Clear(); LoadData(); } diff --git a/WatchList.MudBlazors/Program.cs b/WatchList.MudBlazors/Program.cs index 1f303e6f..693e9106 100644 --- a/WatchList.MudBlazors/Program.cs +++ b/WatchList.MudBlazors/Program.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Hosting.StaticWebAssets; using MudBlazor.Services; using WatchList.Core.Logger; +using WatchList.Core.Model.Filter; using WatchList.Core.Model.ItemCinema; using WatchList.Core.Model.Sortable; using WatchList.Core.Repository; @@ -26,6 +27,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped>(); +builder.Services.AddScoped(); builder.Logging.AddConsole(); var app = builder.Build(); From 73f7e01123f731267002507d05385e15c09a2239 Mon Sep 17 00:00:00 2001 From: Stan___Kudri <53861218+Stan-Kudri@users.noreply.github.com> Date: Sat, 17 Feb 2024 08:36:54 +0300 Subject: [PATCH 4/4] WatchList - Remove unused code. --- WatchList.Core/Model/Filter/FilterWatchItem.cs | 5 ----- WatchList.MudBlazors/Pages/WatchCinemaTable.razor | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/WatchList.Core/Model/Filter/FilterWatchItem.cs b/WatchList.Core/Model/Filter/FilterWatchItem.cs index 9db238c5..ae88a2a7 100644 --- a/WatchList.Core/Model/Filter/FilterWatchItem.cs +++ b/WatchList.Core/Model/Filter/FilterWatchItem.cs @@ -22,15 +22,10 @@ public FilterWatchItem() public ObservableCollection StatusItems { get; set; } = new ObservableCollection(StatusCinema.List.Where(e => e != StatusCinema.AllStatus)); - public TypeCinema TypeFilter { get; set; } - - public StatusCinema StatusFilter { get; set; } - public IQueryable Apply(IQueryable items) { items = items.Where(x => FilterTypeField.Contains(x.Type)); items = items.Where(x => FilterStatusField.Contains(x.Status)); - return items; } diff --git a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor index 0bd569e4..e26d3bdc 100644 --- a/WatchList.MudBlazors/Pages/WatchCinemaTable.razor +++ b/WatchList.MudBlazors/Pages/WatchCinemaTable.razor @@ -33,7 +33,7 @@
- @foreach (var item in FilterWatchItem.TypeItems) { @@ -43,7 +43,7 @@
- @foreach (var item in FilterWatchItem.StatusItems) {