From a3c6ab04ebc0ae461c1303e18866e51b292e9c28 Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Sat, 5 Oct 2024 12:24:38 +0200 Subject: [PATCH] PersonEditPage finished --- .../Genders/GenderResponse.cs | 3 ++ .../Persons/PersonResponse.cs | 3 ++ .../PersonEditFormComponent.razor | 21 +++++++++ .../PersonEditFormComponent.razor.cs | 43 ++++++++++++++++++- .../WatchIt.Website/Pages/DatabasePage.razor | 11 +++-- .../Pages/PersonEditPage.razor | 29 +++++++++++++ WatchIt.Website/WatchIt.Website/Program.cs | 2 + .../WatchIt.Website/WatchIt.Website.csproj | 1 + 8 files changed, 109 insertions(+), 4 deletions(-) diff --git a/WatchIt.Common/WatchIt.Common.Model/Genders/GenderResponse.cs b/WatchIt.Common/WatchIt.Common.Model/Genders/GenderResponse.cs index d22d610..16eeeec 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Genders/GenderResponse.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Genders/GenderResponse.cs @@ -23,6 +23,9 @@ public class GenderResponse : Gender, IQueryOrderable #region CONSTRUCTORS + + [JsonConstructor] + public GenderResponse() { } [SetsRequiredMembers] public GenderResponse(Database.Model.Common.Gender gender) diff --git a/WatchIt.Common/WatchIt.Common.Model/Persons/PersonResponse.cs b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonResponse.cs index 3ff33ee..a81e5ab 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Persons/PersonResponse.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonResponse.cs @@ -40,6 +40,9 @@ public class PersonResponse : Person, IQueryOrderable #region CONSTRUCTORS + [JsonConstructor] + public PersonResponse() { } + [SetsRequiredMembers] public PersonResponse(Database.Model.Person.Person person) { diff --git a/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor b/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor index 321b5a9..c365b9d 100644 --- a/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor +++ b/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor @@ -45,6 +45,27 @@ +
+
+ @if (!string.IsNullOrWhiteSpace(_error)) + { + @(_error) + } +
+
+ +
+
} diff --git a/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.cs b/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.cs index aa805c1..ad07799 100644 --- a/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.cs +++ b/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Components; using WatchIt.Common.Model.Genders; using WatchIt.Common.Model.Persons; +using WatchIt.Website.Services.WebAPI.Genders; using WatchIt.Website.Services.WebAPI.Persons; namespace WatchIt.Website.Components.PersonEditPage; @@ -9,7 +10,9 @@ public partial class PersonEditFormComponent : ComponentBase { #region SERVICES + [Inject] private NavigationManager NavigationManager { get; set; } = default!; [Inject] private IPersonsWebAPIService PersonsWebAPIService { get; set; } = default!; + [Inject] private IGendersWebAPIService GendersWebAPIService { get; set; } = default!; #endregion @@ -27,6 +30,8 @@ public partial class PersonEditFormComponent : ComponentBase #region FIELDS private bool _loaded; + private bool _saving; + private string? _error; private IEnumerable _genders = []; @@ -47,7 +52,7 @@ public partial class PersonEditFormComponent : ComponentBase // STEP 0 endTasks.AddRange( [ - // TODO: Add gender fetch + GendersWebAPIService.GetAllGenders(successAction: data => _genders = data) ]); if (Id.HasValue) { @@ -65,5 +70,41 @@ public partial class PersonEditFormComponent : ComponentBase } } + private async Task Save() + { + void PutSuccess() + { + _error = null; + _saving = false; + } + + void PostSuccess(PersonResponse data) + { + NavigationManager.NavigateTo($"person/{data.Id}/edit"); + } + + void BadRequest(IDictionary errors) + { + _error = errors.SelectMany(x => x.Value).FirstOrDefault() ?? "Unknown error"; + _saving = false; + } + + void AuthError() + { + _error = "Authentication error"; + _saving = false; + } + + _saving = true; + if (Id.HasValue) + { + await PersonsWebAPIService.PutPerson(Id.Value, _person, PutSuccess, BadRequest, AuthError, AuthError); + } + else + { + await PersonsWebAPIService.PostPerson(_person, PostSuccess, BadRequest, AuthError, AuthError); + } + } + #endregion } \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website/Pages/DatabasePage.razor b/WatchIt.Website/WatchIt.Website/Pages/DatabasePage.razor index 66a49ac..61a64d9 100644 --- a/WatchIt.Website/WatchIt.Website/Pages/DatabasePage.razor +++ b/WatchIt.Website/WatchIt.Website/Pages/DatabasePage.razor @@ -7,12 +7,17 @@ + @("WatchIt - ") @switch (Type) { - case "movies": @("Movies"); break; - case "series": @("Series"); break; + case "movies": + @("Movies"); + break; + case "series": + @("Series"); + break; } - @(" database - WatchIt") + @(" database") diff --git a/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor b/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor index 4984505..3930c19 100644 --- a/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor +++ b/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor @@ -4,6 +4,35 @@ @page "/person/new" + + + WatchIt - + @if (_loaded) + { + if (string.IsNullOrWhiteSpace(_error) && _user?.IsAdmin == true) + { + if (_person is not null) + { + @("Edit \"")@(_person.Name)@("\"") + } + else + { + @("Create new person") + } + } + else + { + @("Error") + } + } + else + { + @("Loading") + } + + + + @if (_loaded) { if (_user?.IsAdmin == true) diff --git a/WatchIt.Website/WatchIt.Website/Program.cs b/WatchIt.Website/WatchIt.Website/Program.cs index 12b0924..6975f44 100644 --- a/WatchIt.Website/WatchIt.Website/Program.cs +++ b/WatchIt.Website/WatchIt.Website/Program.cs @@ -9,6 +9,7 @@ using WatchIt.Website.Services.Utility.Authentication; using WatchIt.Website.Services.Utility.Configuration; using WatchIt.Website.Services.Utility.Tokens; using WatchIt.Website.Services.WebAPI.Accounts; +using WatchIt.Website.Services.WebAPI.Genders; using WatchIt.Website.Services.WebAPI.Media; using WatchIt.Website.Services.WebAPI.Movies; using WatchIt.Website.Services.WebAPI.Persons; @@ -72,6 +73,7 @@ public static class Program // WebAPI builder.Services.AddScoped(); + builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/WatchIt.Website/WatchIt.Website/WatchIt.Website.csproj b/WatchIt.Website/WatchIt.Website/WatchIt.Website.csproj index 521dc46..7a61124 100644 --- a/WatchIt.Website/WatchIt.Website/WatchIt.Website.csproj +++ b/WatchIt.Website/WatchIt.Website/WatchIt.Website.csproj @@ -20,6 +20,7 @@ +