Add creation and deletion of calendars

This commit is contained in:
Sijmen 2021-07-04 12:16:51 +02:00
parent ff9a1fff18
commit 64fb38e1ad
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
2 changed files with 59 additions and 17 deletions

View file

@ -22,6 +22,14 @@ namespace BlazorApp.Data
{
await using var context = new MyContext();
await context.Calendars.AddAsync(calendar);
await context.SaveChangesAsync();
}
public static async Task DeleteCalendar(Calendar calendar)
{
await using var context = new MyContext();
context.Calendars.Remove(calendar);
await context.SaveChangesAsync();
}
}
}

View file

@ -1,6 +1,7 @@
@page "/"
@using BlazorApp.Data
@using System.Text.Json
@inject CalendarService _calendarService
@if (_calendars == null)
@ -17,35 +18,54 @@ else
{
<li>
<a href="/calendars/@calendar.Id">@calendar.Name</a>
(<a href="#" @onclick="() => DeleteCalendar(calendar)">
@if (_confirmDeleteId == calendar.Id)
{
<span>Confirm deletion?</span>
}
else
{
<span class="oi oi-x"></span>
}
</a>)
</li>
}
</ul>
@if (_showAddCalendar)
@if (_calendarModel != null)
{
<h2>Add New Calendar</h2>
<EditForm Model="@_addCalendarModel" OnValidSubmit="@HandleValidSubmit">
<EditForm Model="@_calendarModel" OnValidSubmit="@HandleValidSubmit">
<div class="form-group">
<label for="url">URL</label>
<InputText id="url" class="form-control" placeholder="Enter URL" @bind-Value="_addCalendarModel.Url"/>
<InputText id="url" class="form-control" placeholder="Enter URL" @bind-Value="_calendarModel.Url" />
</div>
<div class="form-group">
<label for="username">Username</label>
<InputText id="username" class="form-control" placeholder="Enter username" @bind-Value="_addCalendarModel.Username"/>
<InputText
id="username" class="form-control" placeholder="Enter username"
@bind-Value="_calendarModel.Username"
/>
</div>
<div class="form-group">
<label for="password">Password</label>
<InputText type="password" id="password" class="form-control" placeholder="Enter password" @bind-Value="_addCalendarModel.Password"/>
<InputText
type="password" id="password" class="form-control" placeholder="Enter password"
@bind-Value="_calendarModel.Password"
/>
</div>
<div class="form-group">
<label for="name">Name</label>
<InputText type="text" id="name" class="form-control" placeholder="Enter name" @bind-Value="_addCalendarModel.Name"/>
<InputText
type="text" id="name" class="form-control" placeholder="Enter name"
@bind-Value="_calendarModel.Name"
/>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
}
@ -58,9 +78,8 @@ else
@code
{
private List<Data.Calendar> _calendars;
private bool _showAddCalendar;
private Data.Calendar _addCalendarModel = new();
private int? _confirmDeleteId = null;
private Data.Calendar _calendarModel = null;
protected override async Task OnInitializedAsync()
{
@ -69,12 +88,27 @@ else
private void ShowAddCalendar()
{
_showAddCalendar = true;
_calendarModel = new Data.Calendar();
}
private async void HandleValidSubmit()
private async Task HandleValidSubmit()
{
Console.WriteLine(_addCalendarModel.Name);
await CalendarService.CreateCalendar(_addCalendarModel);
await CalendarService.CreateCalendar(_calendarModel);
_calendarModel = null;
_calendars = await CalendarService.GetCalendars();
}
private async Task DeleteCalendar(Data.Calendar calendar)
{
if (_confirmDeleteId != calendar.Id)
{
_confirmDeleteId = calendar.Id;
return;
}
await CalendarService.DeleteCalendar(calendar);
_calendars = await CalendarService.GetCalendars();
_confirmDeleteId = null;
}
}