-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from netcorepal/refact-dto-data-objects
break change: move PagedData、ResponseData to NetCorePal.Extensions.Dto
- Loading branch information
Showing
23 changed files
with
312 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using NetCorePal.Extensions.Dto; | ||
|
||
namespace NetCorePal.Extensions.AspNetCore; | ||
|
||
public static class PageQueryableExtensions | ||
{ | ||
public static async Task<PagedData<T>> ToPagedDataAsync<T>( | ||
this IQueryable<T> query, | ||
int? index, | ||
int? size, | ||
bool? countTotal, | ||
CancellationToken cancellationToken) | ||
{ | ||
var pageIndex = index ?? 1; // 默认取第1页 | ||
var pageSize = size ?? 10; // 默认每页10条 | ||
|
||
// isTotalNeeded为true时才查询总数。默认不需要总数 | ||
var totalCount = countTotal ?? false ? await query.CountAsync(cancellationToken) : 0; | ||
|
||
var items = await query.Skip((pageIndex - 1) * pageSize) | ||
.Take(pageSize) | ||
.ToListAsync(cancellationToken); | ||
|
||
return new PagedData<T>(items, totalCount, pageIndex, pageSize); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
src/NetCorePal.Extensions.Dto/NetCorePal.Extensions.Dto.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0;net9.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace NetCorePal.Extensions.Dto; | ||
|
||
public static class PageQueryableExtensions | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="query"></param> | ||
/// <param name="pageIndex"></param> | ||
/// <param name="pageSize"></param> | ||
/// <param name="countTotal"></param> | ||
/// <returns></returns> | ||
public static PagedData<T> ToPagedData<T>( | ||
this IQueryable<T> query, | ||
int pageIndex = 1, | ||
int pageSize = 10, | ||
bool countTotal = false) | ||
{ | ||
if (pageIndex <= 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(pageIndex), "页码必须大于 0"); | ||
} | ||
|
||
if (pageSize <= 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(pageSize), "每页条数必须大于 0"); | ||
} | ||
|
||
|
||
// isTotalNeeded为true时才查询总数。默认不需要总数 | ||
var totalCount = countTotal ? query.Count() : 0; | ||
|
||
var items = query.Skip((pageIndex - 1) * pageSize) | ||
.Take(pageSize) | ||
.ToList(); | ||
|
||
return new PagedData<T>(items, totalCount, pageIndex, pageSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace NetCorePal.Extensions.Dto; | ||
|
||
/// <summary> | ||
/// 分页请求模型 | ||
/// </summary> | ||
public class PageRequest | ||
{ | ||
/// <summary> | ||
/// 请求的页码,从1开始 | ||
/// </summary> | ||
public int Index { get; set; } | ||
/// <summary> | ||
/// 请求的每页条数 | ||
/// </summary> | ||
public int Size { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace NetCorePal.Extensions.Dto; | ||
|
||
/// <summary> | ||
/// 分页数据模型 | ||
/// </summary> | ||
/// <typeparam name="T">数据类型</typeparam> | ||
public class PagedData<T> | ||
{ | ||
/// <summary> | ||
/// 构造分页数据 | ||
/// </summary> | ||
/// <param name="items">分页的数据</param> | ||
/// <param name="total">总数据条数</param> | ||
/// <param name="index">当前页码,从1开始</param> | ||
/// <param name="size">每页条数</param> | ||
public PagedData(IEnumerable<T> items, int total, int index, int size) | ||
{ | ||
Items = items; | ||
Total = total; | ||
Index = index; | ||
Size = size; | ||
} | ||
/// <summary> | ||
/// 分页数据 | ||
/// </summary> | ||
public IEnumerable<T> Items { get; private set; } | ||
/// <summary> | ||
/// 数据总数 | ||
/// </summary> | ||
public int Total { get; private set; } | ||
/// <summary> | ||
/// 当前页码,从1开始 | ||
/// </summary> | ||
public int Index { get; private set; } | ||
/// <summary> | ||
/// 每页数据条数 | ||
/// </summary> | ||
public int Size { get; private set; } | ||
} |
Oops, something went wrong.