using System; using System.Collections.Generic; using System.Data; using System.Linq; using BathHouseManagmet.Database; using BathHouseManagmet.Models; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.DependencyInjection; namespace BathHouseManagmet.ViewModels; public partial class DiscountViewModel : ViewModelBase { private readonly IServiceProvider _serviceProvider; [ObservableProperty] List _discounts; [ObservableProperty] int _currentPageSize; [ObservableProperty] List _pageSizes; [ObservableProperty] string _pageInfo; private int totalPages; private int currentPage = 0; public DiscountViewModel(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; using (var rep = serviceProvider.GetRequiredService()) Discounts = new List(rep.GetPage()); PageSizes = new List([5, 10, 15]); CurrentPageSize = PageSizes.First(); } partial void OnCurrentPageSizeChanged(int value) { CalculatePage(); } void CalculatePage() { using var db = new DiscountRepository(); var rowsCount = db.GetRowsCount(); totalPages = (int)Math.Ceiling(((double)rowsCount / CurrentPageSize)); ShowFirstPage(); } void ShowPage(int pageIndex) { currentPage = pageIndex; using var db = new DiscountRepository(); Discounts.Clear(); var rows = db.GetPage(pageIndex, CurrentPageSize); rows.ForEach(d => Discounts.Add(d)); PageInfo = $"Страница {currentPage + 1} из {totalPages}"; } [RelayCommand] private void ShowFirstPage() { ShowPage(0); } [RelayCommand] private void ShowLastPage() { ShowPage(totalPages - 1); } [RelayCommand] private void ShowNextPage() { if (currentPage < totalPages - 1) ShowPage(currentPage + 1); } [RelayCommand] private void ShowPreviousPage() { if (currentPage > 0) ShowPage(currentPage - 1); } }