From b43b31985572f8f47b245d6fda35f1b2bc8d58a5 Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Fri, 4 Oct 2024 19:32:25 +0200 Subject: [PATCH] PersonEditFormComponent added --- .../Persons/PersonRequest.cs | 24 +++++++ .../Persons/PersonRequestValidator.cs | 12 +++- .../PersonEditFormComponent.razor | 55 +++++++++++++++ .../PersonEditFormComponent.razor.cs | 69 +++++++++++++++++++ .../PersonEditFormComponent.razor.css | 0 .../Pages/PersonEditPage.razor | 5 +- .../WatchIt.Website/_Imports.razor | 1 + 7 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor create mode 100644 WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.cs create mode 100644 WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.css diff --git a/WatchIt.Common/WatchIt.Common.Model/Persons/PersonRequest.cs b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonRequest.cs index 5379e05..7e7052b 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Persons/PersonRequest.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonRequest.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; namespace WatchIt.Common.Model.Persons; @@ -10,6 +11,29 @@ public class PersonRequest : Person public short? GenderId { get; set; } #endregion + + + + #region CONSTRUCTORS + + [SetsRequiredMembers] + public PersonRequest() + { + Name = string.Empty; + } + + [SetsRequiredMembers] + public PersonRequest(PersonResponse person) + { + Name = person.Name; + FullName = person.FullName; + Description = person.Description; + BirthDate = person.BirthDate; + DeathDate = person.DeathDate; + GenderId = person.Gender?.Id; + } + + #endregion diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Persons/PersonRequestValidator.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Persons/PersonRequestValidator.cs index f244081..2f3618e 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Persons/PersonRequestValidator.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Persons/PersonRequestValidator.cs @@ -1,9 +1,19 @@ using FluentValidation; using WatchIt.Common.Model.Persons; +using WatchIt.Database; namespace WatchIt.WebAPI.Validators.Persons; public class PersonRequestValidator : AbstractValidator { - + public PersonRequestValidator(DatabaseContext database) + { + RuleFor(x => x.Name).NotEmpty().MaximumLength(100); + RuleFor(x => x.FullName).MaximumLength(200); + RuleFor(x => x.Description).MaximumLength(1000); + When(x => x.GenderId.HasValue, () => + { + RuleFor(x => x.GenderId!.Value).MustBeIn(database.Genders.Select(g => g.Id)); + }); + } } \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor b/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor new file mode 100644 index 0000000..321b5a9 --- /dev/null +++ b/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor @@ -0,0 +1,55 @@ +@using WatchIt.Common.Model.Genders +
+ @if (_loaded) + { + + +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+ + - + +
+
+
+
+ +
+ + + @foreach (GenderResponse gender in _genders) + { + + } + +
+
+
+
+ } + else + { + + } +
\ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.cs b/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.cs new file mode 100644 index 0000000..aa805c1 --- /dev/null +++ b/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.cs @@ -0,0 +1,69 @@ +using Microsoft.AspNetCore.Components; +using WatchIt.Common.Model.Genders; +using WatchIt.Common.Model.Persons; +using WatchIt.Website.Services.WebAPI.Persons; + +namespace WatchIt.Website.Components.PersonEditPage; + +public partial class PersonEditFormComponent : ComponentBase +{ + #region SERVICES + + [Inject] private IPersonsWebAPIService PersonsWebAPIService { get; set; } = default!; + + #endregion + + + + #region PARAMETERS + + [Parameter] public long? Id { get; set; } + [Parameter] public string Class { get; set; } = string.Empty; + + #endregion + + + + #region FIELDS + + private bool _loaded; + + private IEnumerable _genders = []; + + private PersonRequest _person = new PersonRequest(); + + #endregion + + + + #region PRIVATE METHODS + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + List endTasks = new List(); + + // STEP 0 + endTasks.AddRange( + [ + // TODO: Add gender fetch + ]); + if (Id.HasValue) + { + endTasks.AddRange( + [ + PersonsWebAPIService.GetPerson(Id.Value, data => _person = new PersonRequest(data)) + ]); + } + + // END + await Task.WhenAll(endTasks); + + _loaded = true; + StateHasChanged(); + } + } + + #endregion +} \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.css b/WatchIt.Website/WatchIt.Website/Components/PersonEditPage/PersonEditFormComponent.razor.css new file mode 100644 index 0000000..e69de29 diff --git a/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor b/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor index 18d837e..4984505 100644 --- a/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor +++ b/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor @@ -11,7 +11,7 @@
-
+

@(Id is not null ? "Edit" : "Create new") person @(_person is not null ? $" \"{_person.Name}\"" : string.Empty)

@@ -25,7 +25,8 @@ Class="h-100"/>
-
+
diff --git a/WatchIt.Website/WatchIt.Website/_Imports.razor b/WatchIt.Website/WatchIt.Website/_Imports.razor index 3a478f4..e8981f0 100644 --- a/WatchIt.Website/WatchIt.Website/_Imports.razor +++ b/WatchIt.Website/WatchIt.Website/_Imports.razor @@ -9,6 +9,7 @@ @using WatchIt.Website @using WatchIt.Website.Layout @using WatchIt.Website.Components +@using WatchIt.Website.Components.PersonEditPage @using WatchIt.Common.Model.Accounts @using WatchIt.Common.Model.Media @using WatchIt.Website.Services.Utility.Tokens