| Name | Message | Date |
|---|---|---|
| 📄 Date.razor | 11 days ago | |
| 📄 Login.razor | 11 days ago | |
| 📄 Logout.razor | 11 days ago | |
| 📄 Participants.razor | 11 days ago | |
| 📄 Questions.razor | 11 days ago |
📄
MatDenDagen/Components/Pages/Admin/Participants.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@page "/admin/participants"
@attribute [Authorize(Roles = "Admin")]
@using MatDenDagen.Infrastructure.Storage.Database
@using MatDenDagen.Models
@using Microsoft.AspNetCore.Authorization
@using Microsoft.EntityFrameworkCore
@inject TimeProvider timeProvider
@inject QuestionnaireContext questionnaireContext
<ul>
@foreach (var participant in questionnaireContext.Participants)
{
<li>
<span>@participant.Name (@participant.PhoneNumber)</span>
<EditForm FormName="@($"RemoveParticipant-{participant.Id}")" Model="@removeParticipantModel" OnSubmit="@RemoveParticipant"
Enhance>
<input type="hidden" name="removeParticipantModel.ParticipantId" value="@participant.Id" />
<input type="submit" value="Ta bort" />
</EditForm>
</li>
}
</ul>
<hr />
<EditForm FormName="AddParticipant" Model="@addParticipantModel" OnSubmit="@AddParticipant" Enhance>
<p>
<label>
<span>Namn:</span>
<input type="text" name="addParticipantModel.Name" required />
</label>
</p>
<p>
<label>
<span>Telefonnummer:</span>
<input type="text" name="addParticipantModel.PhoneNumber" required />
</label>
</p>
<p>
<input type="submit" value="Lägg till" />
</p>
</EditForm>
@code {
[SupplyParameterFromForm]
private AddParticipantModel? addParticipantModel { get; set; }
[SupplyParameterFromForm]
private RemoveParticipantModel? removeParticipantModel { get; set; }
protected override void OnInitialized()
{
addParticipantModel ??= new();
removeParticipantModel ??= new();
}
private async Task AddParticipant()
{
if (addParticipantModel?.Name is not string name || addParticipantModel?.PhoneNumber is not string phoneNumber)
{
return;
}
var participant = new Participant { Id = Guid.CreateVersion7(timeProvider.GetUtcNow()), Name = name, PhoneNumber = phoneNumber };
questionnaireContext.Participants.Add(participant);
await questionnaireContext.SaveChangesAsync();
}
private async Task RemoveParticipant()
{
if (removeParticipantModel?.ParticipantId is not string participantId || !Guid.TryParse(participantId, out var id))
{
return;
}
var participant = questionnaireContext.Participants.SingleOrDefault(p => p.Id == id);
if (participant is null)
{
return;
}
questionnaireContext.Participants.Remove(participant);
await questionnaireContext.SaveChangesAsync();
}
private sealed class AddParticipantModel
{
public string? Name { get; set; }
public string? PhoneNumber { get; set; }
}
private sealed class RemoveParticipantModel
{
public string? ParticipantId { get; set; }
}
}