Skip to content

Commit

Permalink
重构代码以改进查询逻辑和异步处理
Browse files Browse the repository at this point in the history
- 将 `<Indicator Type="@status"/>` 替换为 `<Indicator Type="@type"/>`。
- 在 `ServiceItem.razor.cs` 文件中,添加了 `using Microsoft.EntityFrameworkCore;` 引用。
- 将 `private EventType status { get; set; }` 更改为 `private EventType type { get; set; }`。
- 将 `OnParametersSetAsync` 方法改为异步方法,并使用 `await` 关键字。
- 修改了查询逻辑,使用 `OrderByDescending` 和 `FirstOrDefaultAsync` 替代 `MaxBy` 方法。
- 删除了 `return Task.CompletedTask;`。
  • Loading branch information
Aloento committed Nov 13, 2024
1 parent 7bbda13 commit 0a04979
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Components/Home/ServiceItem.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
{
@if (id is null)
{
<Indicator Type="@status"/>
<Indicator Type="@type"/>
}
else
{
<a class="flex items-center" href="/Event/@id">
<Indicator Type="@status"/>
<Indicator Type="@type"/>
</a>
}
}
Expand Down
16 changes: 9 additions & 7 deletions Components/Home/ServiceItem.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using Event;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using Services;

public partial class ServiceItem {
Expand All @@ -11,25 +12,26 @@ public partial class ServiceItem {
[EditorRequired]
public RegionService? RegionService { get; set; }

private EventType status { get; set; }
private EventType type { get; set; }

private bool future { get; set; }

private int? id { get; set; }

protected override Task OnParametersSetAsync() {
var res = this.RegionService.Events
protected override async Task OnParametersSetAsync() {
var res = await this.db.EventRegionService
.Where(x => x.RegionServiceId == this.RegionService.Id)
.Select(x => x.Event)
.Where(x =>
(x.Type == EventType.Maintenance || x.End == null) &&
x.Status != EventStatus.Completed &&
x.Status != EventStatus.Resolved &&
x.Status != EventStatus.Cancelled)
.MaxBy(x => x.Type);
.OrderByDescending(x => x.Type)
.FirstOrDefaultAsync();

this.id = res?.Id;
this.status = res?.Type ?? default;
this.type = res?.Type ?? default;
this.future = res?.Start.ToUniversalTime() > DateTime.UtcNow;

return Task.CompletedTask;
}
}

0 comments on commit 0a04979

Please sign in to comment.