Add creation and deletion of calendars
This commit is contained in:
parent
ff9a1fff18
commit
64fb38e1ad
2 changed files with 59 additions and 17 deletions
|
@ -22,6 +22,14 @@ namespace BlazorApp.Data
|
||||||
{
|
{
|
||||||
await using var context = new MyContext();
|
await using var context = new MyContext();
|
||||||
await context.Calendars.AddAsync(calendar);
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
@page "/"
|
@page "/"
|
||||||
|
|
||||||
@using BlazorApp.Data
|
@using BlazorApp.Data
|
||||||
|
@using System.Text.Json
|
||||||
@inject CalendarService _calendarService
|
@inject CalendarService _calendarService
|
||||||
|
|
||||||
@if (_calendars == null)
|
@if (_calendars == null)
|
||||||
|
@ -17,33 +18,52 @@ else
|
||||||
{
|
{
|
||||||
<li>
|
<li>
|
||||||
<a href="/calendars/@calendar.Id">@calendar.Name</a>
|
<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>
|
</li>
|
||||||
}
|
}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
@if (_showAddCalendar)
|
@if (_calendarModel != null)
|
||||||
{
|
{
|
||||||
<h2>Add New Calendar</h2>
|
<h2>Add New Calendar</h2>
|
||||||
|
|
||||||
<EditForm Model="@_addCalendarModel" OnValidSubmit="@HandleValidSubmit">
|
<EditForm Model="@_calendarModel" OnValidSubmit="@HandleValidSubmit">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="url">URL</label>
|
<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>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="username">Username</label>
|
<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>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="password">Password</label>
|
<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>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="name">Name</label>
|
<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>
|
</div>
|
||||||
|
|
||||||
<button type="submit" class="btn btn-primary">Submit</button>
|
<button type="submit" class="btn btn-primary">Submit</button>
|
||||||
|
@ -58,9 +78,8 @@ else
|
||||||
@code
|
@code
|
||||||
{
|
{
|
||||||
private List<Data.Calendar> _calendars;
|
private List<Data.Calendar> _calendars;
|
||||||
private bool _showAddCalendar;
|
private int? _confirmDeleteId = null;
|
||||||
|
private Data.Calendar _calendarModel = null;
|
||||||
private Data.Calendar _addCalendarModel = new();
|
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
|
@ -69,12 +88,27 @@ else
|
||||||
|
|
||||||
private void ShowAddCalendar()
|
private void ShowAddCalendar()
|
||||||
{
|
{
|
||||||
_showAddCalendar = true;
|
_calendarModel = new Data.Calendar();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void HandleValidSubmit()
|
private async Task HandleValidSubmit()
|
||||||
{
|
{
|
||||||
Console.WriteLine(_addCalendarModel.Name);
|
await CalendarService.CreateCalendar(_calendarModel);
|
||||||
await CalendarService.CreateCalendar(_addCalendarModel);
|
_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;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue