Skip to content

Commit

Permalink
Merge pull request #90 from neozhu/improvement
Browse files Browse the repository at this point in the history
LocalTimezoneOffset
  • Loading branch information
neozhu authored Sep 15, 2024
2 parents ad192b1 + 09fbc9e commit cf08d05
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/Templates/Pages/.razor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@
private Task<AuthenticationState> AuthState { get; set; } = default!;
[CascadingParameter]
private UserProfile? UserProfile { get; set; }
[CascadingParameter(Name = "LocalTimezoneOffset")]
private int _localTimezoneOffset { get; set; }

private {nameofPlural}WithPaginationQuery Query { get; set; } = new();
[Inject]
Expand Down Expand Up @@ -235,6 +237,7 @@
Query.SortDirection = state.SortDefinitions.FirstOrDefault()?.Descending ?? true ? SortDirection.Descending.ToString() : SortDirection.Ascending.ToString();
Query.PageNumber = state.Page + 1;
Query.PageSize = state.PageSize;
Query.LocalTimezoneOffset = _localTimezoneOffset;
var result = await Mediator.Send(Query).ConfigureAwait(false);
return new GridData<{itemname}Dto>() { TotalItems = result.TotalItems, Items = result.Items };
}
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Queries/Pagination/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class {nameofPlural}WithPaginationQuery : {itemname}AdvancedFilter, ICach
{
public override string ToString()
{
return $"Listview:{ListView}, Search:{Keyword}, {OrderBy}, {SortDirection}, {PageNumber}, {PageSize}";
return $"Listview:{ListView}-{LocalTimezoneOffset}, Search:{Keyword}, {OrderBy}, {SortDirection}, {PageNumber}, {PageSize}";
}
public string CacheKey => {itemname}CacheKey.GetPaginationCacheKey($"{this}");
public MemoryCacheEntryOptions? Options => {itemname}CacheKey.MemoryCacheEntryOptions;
Expand Down
1 change: 1 addition & 0 deletions src/Templates/Specifications/AdvancedFilter.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum {itemname}ListView
/// </summary>
public class {itemname}AdvancedFilter: PaginationFilter
{
public int LocalTimezoneOffset { get; set; }
public {itemname}ListView ListView { get; set; } = {itemname}ListView.All;
public UserProfile? CurrentUser { get; set; }
}
20 changes: 9 additions & 11 deletions src/Templates/Specifications/AdvancedSpecification.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ public class {itemname}AdvancedSpecification : Specification<{itemname}>
{
public {itemname}AdvancedSpecification({itemname}AdvancedFilter filter)
{
var today = DateTime.Now.ToUniversalTime().Date;
var start = Convert.ToDateTime(today.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture) + " 00:00:00",
CultureInfo.CurrentCulture);
var end = Convert.ToDateTime(today.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture) + " 23:59:59",
CultureInfo.CurrentCulture);
var last30day = Convert.ToDateTime(
today.AddDays(-30).ToString("yyyy-MM-dd", CultureInfo.CurrentCulture) + " 00:00:00",
CultureInfo.CurrentCulture);
var timezoneOffset = filter.LocalTimezoneOffset;
var utcNow = DateTime.UtcNow;
var localNow = utcNow.Date.AddHours(timezoneOffset);
var startOfTodayLocalAsUtc = localNow;
var endOfTodayLocalAsUtc = localNow.AddDays(1);
var startOfLast30DaysLocalAsUtc = localNow.AddDays(-30);

Query.Where(q => q.Name != null)
.Where(q => q.Name!.Contains(filter.Keyword) || q.Description!.Contains(filter.Keyword), !string.IsNullOrEmpty(filter.Keyword))
.Where(filter.Keyword,!string.IsNullOrEmpty(filter.Keyword))
.Where(q => q.CreatedBy == filter.CurrentUser.UserId, filter.ListView == {itemname}ListView.My && filter.CurrentUser is not null)
.Where(q => q.Created >= start && q.Created <= end, filter.ListView == {itemname}ListView.CreatedToday)
.Where(q => q.Created >= last30day, filter.ListView == {itemname}ListView.Created30Days);
.Where(q => q.Created >= startOfTodayLocalAsUtc && q.Created <= endOfTodayLocalAsUtc, filter.ListView == {itemname}ListView.CreatedToday)
.Where(q => q.Created >= startOfLast30DaysLocalAsUtc, filter.ListView == {itemname}ListView.Created30Days);

}
}

0 comments on commit cf08d05

Please sign in to comment.