Skip to content

Commit

Permalink
Merge pull request #231 from dncuug/230
Browse files Browse the repository at this point in the history
Update implementation for IQueryable
  • Loading branch information
ernado-x authored Feb 14, 2023
2 parents 6950d94 + 91ca9d8 commit 3c7e2dd
Show file tree
Hide file tree
Showing 41 changed files with 344 additions and 78 deletions.
9 changes: 8 additions & 1 deletion X.PagedList.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "X.PagedList.Mvc.Core", "src
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "X.PagedList.Tests", "tests\X.PagedList.Tests\X.PagedList.Tests.csproj", "{C78B1316-1EF9-45C3-A3FD-9A131BA3DD62}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "X.PagedList.Mvc.Example.Core", "examples\X.PagedList.Mvc.Example.Core\X.PagedList.Mvc.Example.Core.csproj", "{288F5726-904F-48B8-8363-EA1A22D331D1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.Website", "examples\Example.Website\Example.Website.csproj", "{288F5726-904F-48B8-8363-EA1A22D331D1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.DAL", "examples\Example.DAL\Example.DAL.csproj", "{AD16A8D1-EAF0-4947-BCEC-A8B423B2F117}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -39,6 +41,10 @@ Global
{288F5726-904F-48B8-8363-EA1A22D331D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{288F5726-904F-48B8-8363-EA1A22D331D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{288F5726-904F-48B8-8363-EA1A22D331D1}.Release|Any CPU.Build.0 = Release|Any CPU
{AD16A8D1-EAF0-4947-BCEC-A8B423B2F117}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD16A8D1-EAF0-4947-BCEC-A8B423B2F117}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD16A8D1-EAF0-4947-BCEC-A8B423B2F117}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD16A8D1-EAF0-4947-BCEC-A8B423B2F117}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -48,6 +54,7 @@ Global
{3FB17E16-B671-450F-81BD-FD607D6A78C4} = {BDDADD09-D112-418E-8469-BC762EC09936}
{C78B1316-1EF9-45C3-A3FD-9A131BA3DD62} = {0170B742-C624-4C22-9DE1-2A93CF9C12D6}
{288F5726-904F-48B8-8363-EA1A22D331D1} = {309A8FC8-4784-4D8D-903F-BD54EBB0F1D7}
{AD16A8D1-EAF0-4947-BCEC-A8B423B2F117} = {309A8FC8-4784-4D8D-903F-BD54EBB0F1D7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1A82D446-6F26-48B2-8085-DFA5F87453FC}
Expand Down
11 changes: 11 additions & 0 deletions examples/Example.DAL/Animal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;

namespace Example.DAL;

public partial class Animal
{
public long Id { get; set; }

public string Name { get; set; } = null!;
}
48 changes: 48 additions & 0 deletions examples/Example.DAL/DatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace Example.DAL;

public partial class DatabaseContext : DbContext
{
public DatabaseContext()
{
}

public DatabaseContext(DbContextOptions<DatabaseContext> options)
: base(options)
{
}

public virtual DbSet<Animal> Animals { get; set; }

public virtual DbSet<User> Users { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Only configure the DbContext if it hasn't been configured yet
if (!optionsBuilder.IsConfigured)
{
// DbContext is not yet configured, configure it now
optionsBuilder.UseSqlite("Data Source=<path_to_database_file>");
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Animal>(entity =>
{
entity.ToTable("Animal");
});

modelBuilder.Entity<User>(entity =>
{
entity.ToTable("User");
});

OnModelCreatingPartial(modelBuilder);
}

partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
17 changes: 17 additions & 0 deletions examples/Example.DAL/Example.DAL.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions examples/Example.DAL/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;

namespace Example.DAL;

public partial class User
{
public long Id { get; set; }

public string Name { get; set; } = null!;
}
10 changes: 10 additions & 0 deletions examples/Example.Website/Controllers/Bootstrap41Controller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Example.DAL;

namespace Example.Website.Controllers;

public class Bootstrap41Controller : HomeController
{
public Bootstrap41Controller(DatabaseContext databaseContext) : base(databaseContext)
{
}
}
102 changes: 102 additions & 0 deletions examples/Example.Website/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Example.DAL;
using Microsoft.AspNetCore.Mvc;
using X.PagedList;

namespace Example.Website.Controllers;

public class HomeController : Controller
{
private const int PageSize = 10;

private readonly DatabaseContext _databaseContext;

public HomeController(DatabaseContext databaseContext)
{
_databaseContext = databaseContext;
}

public IActionResult Index(int page = 1)
{
ViewBag.Names = GetPagedNames(page);
return View();
}

public IActionResult AjaxIndex(int page = 1)
{
var listPaged = GetPagedNames(page);
ViewBag.Names = listPaged;
return View();
}

public async Task<IActionResult> EFCore(int page = 1)
{
// return a 404 if user browses to before the first page
if (page < 1)
{
return NotFound();
}

var records = await _databaseContext.Animals
.Select(o => o.Name)
.ToPagedListAsync(page, PageSize);

// return a 404 if user browses to pages beyond last page. special case first page if no items exist
if (records.PageNumber != 1 && page > records.PageCount)
{
return NotFound();
}

ViewBag.Names = records;

return View();
}

public IActionResult GetOnePageOfNames(int page = 1)
{
var listPaged = GetPagedNames(page);
ViewBag.Names = listPaged;
return PartialView("_NameListPartial", ViewBag.Names);
}

public IActionResult Error()
{
return View();
}

private IPagedList<string> GetPagedNames(int? page)
{
// return a 404 if user browses to before the first page
if (page.HasValue && page < 1)
{
return null;
}

// retrieve list from database/whereverand
var listUnPaged = GetStuffFromFile();

// page the list

var listPaged = listUnPaged.ToPagedList(page ?? 1, PageSize);

// return a 404 if user browses to pages beyond last page. special case first page if no items exist
if (listPaged.PageNumber != 1 && page.HasValue && page > listPaged.PageCount)
{
return null;
}

return listPaged;
}

/// <summary>
/// In this case we return array of string, but in most DB situations you'll want to return IQueryable
/// </summary>
/// <returns></returns>
private IEnumerable<string> GetStuffFromFile()
{
var sampleData = System.IO.File.ReadAllText("Names.txt");
return sampleData.Split('\n');
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\X.PagedList.Mvc.Core\X.PagedList.Mvc.Core.csproj" />
<ProjectReference Include="..\..\src\X.PagedList\X.PagedList.csproj" />
<ProjectReference Include="..\Example.DAL\Example.DAL.csproj" />
</ItemGroup>
</Project>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.IO;
using Example.DAL;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace X.PagedList.Mvc.Example.Core;
namespace Example.Website;

public class Program
{
Expand All @@ -12,7 +14,15 @@ public static void Main(string[] args)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();

builder.Services.AddDbContext<DatabaseContext>(options =>
{
var connectionString = $"Data Source={Path.Combine(builder.Environment.ContentRootPath, "data", "example.sqlite")}";

options.UseSqlite(connectionString);
});

var app = builder.Build();

if (builder.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand All @@ -23,7 +33,7 @@ public static void Main(string[] args)
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();
Expand Down
58 changes: 58 additions & 0 deletions examples/Example.Website/Views/Home/EFCore.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@{
ViewBag.Title = "Product Listing";
var pagedList = (IPagedList)ViewBag.Names;
}

@using X.PagedList.Mvc.Core; @*import this so we get our HTML Helper*@
@using X.PagedList; @*import this so we can cast our list to IPagedList (only necessary because ViewBag is dynamic)*@
@using X.PagedList.Mvc.Core.Fluent
@using X.PagedList.Web.Common


<!-- import the included stylesheet for some (very basic) default styling -->
<link href="/css/PagedList.css" rel="stylesheet" type="text/css" />

<!-- loop through each of your products and display it however you want. we're just printing the name here -->
<h2>List of Products from EF Core</h2>
<ul>
@foreach (var name in ViewBag.Names)
{
<li>@name</li>
}
</ul>

<!-- output a paging control that lets the user navigation to the previous page, next page, etc -->
@Html.PagedListPager(pagedList, page => Url.Action("EFCore", new { page }))

<h3>Fluent pager</h3>
@(Html.Pager(pagedList)
.Url(page => Url.Action("EFCore", new { page }))
.Build())

<h3>Pager for #85</h3>
@(Html.Pager(pagedList)
.Url(page => Url.Action("EFCore", new { page }))
.WithPartialView("Paging/_Pager_85")
.DisplayLinkToFirstPage (PagedListDisplayMode.IfNeeded)
.DisplayLinkToLastPage(PagedListDisplayMode.IfNeeded)
.DisplayLinkToPreviousPage()
.DisplayLinkToNextPage()
.MaximumPageNumbersToDisplay(3)
.Build())

<h3>Pager with ItemSliceAndTotalPosition at the end</h3>
@Html.PagedListPager(pagedList,
page => Url.Action("EFCore", new { page }),
new PagedListRenderOptions()
{
DisplayItemSliceAndTotal = true,
ItemSliceAndTotalPosition = ItemSliceAndTotalPosition.End
})

<h3>Pager with ItemSliceAndTotalPosition at the beginning</h3>
@Html.PagedListPager(pagedList,
page => Url.Action("EFCore", new { page }),
new PagedListRenderOptions()
{
DisplayItemSliceAndTotal = true,
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - X.PagedList.Mvc.Example.Core</title>
<title>@ViewData["Title"] - Example.Website</title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />

Expand All @@ -19,7 +19,7 @@
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">X.PagedList.Mvc.Example.Core</a>
<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Example.Website</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarHeader" aria-controls="navbarHeader" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
Expand All @@ -40,7 +40,7 @@
@RenderBody()
<hr />
<footer>
<p>&copy; @DateTime.Now.Year - X.PagedList.Mvc.Example.Core</p>
<p>&copy; @DateTime.Now.Year - Example.Website</p>
</footer>
</div>

Expand Down
Loading

0 comments on commit 3c7e2dd

Please sign in to comment.