From 64fb38e1adb10a79d5d4b6688e75a8d8e9af4a79 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sun, 4 Jul 2021 12:16:51 +0200 Subject: [PATCH] Add creation and deletion of calendars --- BlazorApp/Data/CalendarService.cs | 8 ++++ BlazorApp/Pages/Index.razor | 68 +++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/BlazorApp/Data/CalendarService.cs b/BlazorApp/Data/CalendarService.cs index 09a6674..37672eb 100644 --- a/BlazorApp/Data/CalendarService.cs +++ b/BlazorApp/Data/CalendarService.cs @@ -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(); } } } \ No newline at end of file diff --git a/BlazorApp/Pages/Index.razor b/BlazorApp/Pages/Index.razor index 9f9c4f5..83a0193 100644 --- a/BlazorApp/Pages/Index.razor +++ b/BlazorApp/Pages/Index.razor @@ -1,6 +1,7 @@ @page "/" @using BlazorApp.Data +@using System.Text.Json @inject CalendarService _calendarService @if (_calendars == null) @@ -17,35 +18,54 @@ else {
  • @calendar.Name + ( + @if (_confirmDeleteId == calendar.Id) + { + Confirm deletion? + } + else + { + + } + )
  • } - @if (_showAddCalendar) + @if (_calendarModel != null) {

    Add New Calendar

    - - + +
    - +
    - +
    - +
    - +
    - +
    - +
    - +
    } @@ -58,9 +78,8 @@ else @code { private List _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; } } \ No newline at end of file