From 9f3bbcf987fbc382650269927cbb02bba1dab51a Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Thu, 3 Oct 2024 21:05:04 +0200 Subject: [PATCH] PictureEditorComponent created --- .../Media/MediaPosterRequest.cs | 6 +- .../Persons/PersonPhoto.cs | 6 + .../Persons/PersonPhotoRequest.cs | 41 ++++++ .../Persons/PersonPhotoResponse.cs | 36 ++++++ .../WatchIt.Common.Model/Picture.cs | 2 +- .../PersonsController.cs | 26 ++++ .../IPersonsControllerService.cs | 5 + .../PersonsControllerService.cs | 76 +++++++++++ .../Model/Persons.cs | 3 + .../IPersonsWebAPIService.cs | 5 + .../PersonsWebAPIService.cs | 47 +++++++ .../Components/PictureEditorComponent.razor | 65 ++++++++++ .../PictureEditorComponent.razor.cs | 122 ++++++++++++++++++ .../PictureEditorComponent.razor.css | 0 .../WatchIt.Website/Pages/AdminPage.razor | 14 +- .../Pages/PersonEditPage.razor | 43 ++++++ .../Pages/PersonEditPage.razor.cs | 77 +++++++++++ .../Pages/PersonEditPage.razor.css | 0 WatchIt.Website/WatchIt.Website/Program.cs | 2 + .../WatchIt.Website/WatchIt.Website.csproj | 1 + .../WatchIt.Website/appsettings.json | 5 +- 21 files changed, 570 insertions(+), 12 deletions(-) create mode 100644 WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhoto.cs create mode 100644 WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhotoRequest.cs create mode 100644 WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhotoResponse.cs create mode 100644 WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor create mode 100644 WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor.cs create mode 100644 WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor.css create mode 100644 WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor create mode 100644 WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor.cs create mode 100644 WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor.css diff --git a/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterRequest.cs b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterRequest.cs index 5a9fa95..1b83f1a 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterRequest.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterRequest.cs @@ -10,10 +10,10 @@ public class MediaPosterRequest : MediaPoster public MediaPosterRequest() {} [SetsRequiredMembers] - public MediaPosterRequest(MediaPosterResponse response) + public MediaPosterRequest(Picture image) { - Image = response.Image; - MimeType = response.MimeType; + Image = image.Image; + MimeType = image.MimeType; } #endregion diff --git a/WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhoto.cs b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhoto.cs new file mode 100644 index 0000000..67094f6 --- /dev/null +++ b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhoto.cs @@ -0,0 +1,6 @@ +namespace WatchIt.Common.Model.Persons; + +public class PersonPhoto : Picture +{ + +} \ No newline at end of file diff --git a/WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhotoRequest.cs b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhotoRequest.cs new file mode 100644 index 0000000..8397887 --- /dev/null +++ b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhotoRequest.cs @@ -0,0 +1,41 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; +using WatchIt.Database.Model.Person; + +namespace WatchIt.Common.Model.Persons; + +public class PersonPhotoRequest : PersonPhoto +{ + #region CONSTRUCTORS + + [JsonConstructor] + public PersonPhotoRequest() {} + + [SetsRequiredMembers] + public PersonPhotoRequest(Picture image) + { + Image = image.Image; + MimeType = image.MimeType; + } + + #endregion + + + + #region PUBLIC METHODS + + public PersonPhotoImage CreatePersonPhotoImage() => new PersonPhotoImage + { + Image = Image, + MimeType = MimeType, + }; + + public void UpdatePersonPhotoImage(PersonPhotoImage item) + { + item.Image = Image; + item.MimeType = MimeType; + item.UploadDate = DateTime.UtcNow; + } + + #endregion +} \ No newline at end of file diff --git a/WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhotoResponse.cs b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhotoResponse.cs new file mode 100644 index 0000000..957e76c --- /dev/null +++ b/WatchIt.Common/WatchIt.Common.Model/Persons/PersonPhotoResponse.cs @@ -0,0 +1,36 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; +using WatchIt.Database.Model.Person; + +namespace WatchIt.Common.Model.Persons; + +public class PersonPhotoResponse : PersonPhoto +{ + #region PROPERTIES + + [JsonPropertyName("id")] + public Guid Id { get; set; } + + [JsonPropertyName("upload_date")] + public DateTime UploadDate { get; set; } + + #endregion + + + + #region CONSTRUCTORS + + [JsonConstructor] + public PersonPhotoResponse() {} + + [SetsRequiredMembers] + public PersonPhotoResponse(PersonPhotoImage personPhotoImage) + { + Id = personPhotoImage.Id; + Image = personPhotoImage.Image; + MimeType = personPhotoImage.MimeType; + UploadDate = personPhotoImage.UploadDate; + } + + #endregion +} \ No newline at end of file diff --git a/WatchIt.Common/WatchIt.Common.Model/Picture.cs b/WatchIt.Common/WatchIt.Common.Model/Picture.cs index ab0e8ee..b16065e 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Picture.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Picture.cs @@ -2,7 +2,7 @@ using System.Text.Json.Serialization; namespace WatchIt.Common.Model; -public abstract class Picture +public class Picture { #region PROPERTIES diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/PersonsController.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/PersonsController.cs index aaf1d45..258763a 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/PersonsController.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/PersonsController.cs @@ -80,5 +80,31 @@ public class PersonsController : ControllerBase #endregion + #region Photo + + [HttpGet("{id}/photo")] + [AllowAnonymous] + [ProducesResponseType(typeof(PersonPhotoResponse), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task GetPersonPhoto([FromRoute] long id) => await _personsControllerService.GetPersonPhoto(id); + + [HttpPut("{id}/photo")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + [ProducesResponseType(typeof(PersonPhotoResponse), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] + public async Task PutPersonPhoto([FromRoute]long id, [FromBody]PersonPhotoRequest body) => await _personsControllerService.PutPersonPhoto(id, body); + + [HttpDelete("{id}/photo")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + [ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)] + [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] + public async Task DeletePersonPhoto([FromRoute]long id) => await _personsControllerService.DeletePersonPhoto(id); + + #endregion + #endregion } \ No newline at end of file diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/IPersonsControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/IPersonsControllerService.cs index f9b5f6f..d3bd360 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/IPersonsControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/IPersonsControllerService.cs @@ -10,5 +10,10 @@ public interface IPersonsControllerService Task PostPerson(PersonRequest data); Task PutPerson(long id, PersonRequest data); Task DeletePerson(long id); + Task GetPersonsViewRank(int first, int days); + + Task GetPersonPhoto(long id); + Task PutPersonPhoto(long id, PersonPhotoRequest data); + Task DeletePersonPhoto(long id); } \ No newline at end of file diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/PersonsControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/PersonsControllerService.cs index 09137fe..549a5c1 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/PersonsControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/PersonsControllerService.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using WatchIt.Common.Model.Persons; using WatchIt.Database; +using WatchIt.Database.Model.Person; using WatchIt.WebAPI.Services.Controllers.Common; using WatchIt.WebAPI.Services.Utility.User; using Person = WatchIt.Database.Model.Person.Person; @@ -142,5 +143,80 @@ public class PersonsControllerService : IPersonsControllerService #endregion + #region Photo + + public async Task GetPersonPhoto(long id) + { + Person? person = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id); + if (person is null) + { + return RequestResult.BadRequest(); + } + + PersonPhotoImage? photo = person.PersonPhoto; + if (photo is null) + { + return RequestResult.NotFound(); + } + + PersonPhotoResponse data = new PersonPhotoResponse(photo); + return RequestResult.Ok(data); + } + + public async Task PutPersonPhoto(long id, PersonPhotoRequest data) + { + UserValidator validator = _userService.GetValidator().MustBeAdmin(); + if (!validator.IsValid) + { + return RequestResult.Forbidden(); + } + + Person? person = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id); + if (person is null) + { + return RequestResult.BadRequest(); + } + + if (person.PersonPhoto is null) + { + PersonPhotoImage image = data.CreatePersonPhotoImage(); + await _database.PersonPhotoImages.AddAsync(image); + await _database.SaveChangesAsync(); + + person.PersonPhotoId = image.Id; + } + else + { + data.UpdatePersonPhotoImage(person.PersonPhoto); + } + + await _database.SaveChangesAsync(); + + PersonPhotoResponse returnData = new PersonPhotoResponse(person.PersonPhoto); + return RequestResult.Ok(returnData); + } + + public async Task DeletePersonPhoto(long id) + { + UserValidator validator = _userService.GetValidator().MustBeAdmin(); + if (!validator.IsValid) + { + return RequestResult.Forbidden(); + } + + Person? person = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id); + + if (person?.PersonPhoto != null) + { + _database.PersonPhotoImages.Attach(person.PersonPhoto); + _database.PersonPhotoImages.Remove(person.PersonPhoto); + await _database.SaveChangesAsync(); + } + + return RequestResult.NoContent(); + } + + #endregion + #endregion } \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Persons.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Persons.cs index 1bf760a..a1c1ab6 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Persons.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Persons.cs @@ -9,4 +9,7 @@ public class Persons public string PutPerson { get; set; } public string DeletePerson { get; set; } public string GetPersonsViewRank { get; set; } + public string GetPersonPhoto { get; set; } + public string PutPersonPhoto { get; set; } + public string DeletePersonPhoto { get; set; } } \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/IPersonsWebAPIService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/IPersonsWebAPIService.cs index 2f2181f..3572536 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/IPersonsWebAPIService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/IPersonsWebAPIService.cs @@ -9,5 +9,10 @@ public interface IPersonsWebAPIService Task PostPerson(PersonRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null); Task PutPerson(long id, PersonRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null); Task DeletePerson(long id, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null); + Task GetPersonsViewRank(int? first = null, int? days = null, Action>? successAction = null, Action>? badRequestAction = null); + + Task GetPersonPhoto(long id, Action? successAction = null, Action>? badRequestAction = null, Action? notFoundAction = null); + Task PutPersonPhoto(long id, PersonPhotoRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null); + Task DeletePersonPhoto(long id, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null); } \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/PersonsWebAPIService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/PersonsWebAPIService.cs index b19d450..cd40eff 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/PersonsWebAPIService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/PersonsWebAPIService.cs @@ -138,6 +138,53 @@ public class PersonsWebAPIService : BaseWebAPIService, IPersonsWebAPIService .ExecuteAction(); } + #endregion + + #region Photo + + public async Task GetPersonPhoto(long id, Action? successAction = null, Action>? badRequestAction = null, Action? notFoundAction = null) + { + string url = GetUrl(EndpointsConfiguration.Persons.GetPersonPhoto, id); + + HttpRequest request = new HttpRequest(HttpMethodType.Get, url); + + HttpResponse response = await _httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor400BadRequest(badRequestAction) + .RegisterActionFor404NotFound(notFoundAction) + .ExecuteAction(); + } + + public async Task PutPersonPhoto(long id, PersonPhotoRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null) + { + string url = GetUrl(EndpointsConfiguration.Persons.PutPersonPhoto, id); + + HttpRequest request = new HttpRequest(HttpMethodType.Put, url) + { + Body = data + }; + + HttpResponse response = await _httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor400BadRequest(badRequestAction) + .RegisterActionFor401Unauthorized(unauthorizedAction) + .RegisterActionFor403Forbidden(forbiddenAction) + .ExecuteAction(); + } + + public async Task DeletePersonPhoto(long id, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null) + { + string url = GetUrl(EndpointsConfiguration.Persons.DeletePersonPhoto, id); + + HttpRequest request = new HttpRequest(HttpMethodType.Delete, url); + + HttpResponse response = await _httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor401Unauthorized(unauthorizedAction) + .RegisterActionFor403Forbidden(forbiddenAction) + .ExecuteAction(); + } + #endregion #endregion diff --git a/WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor b/WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor new file mode 100644 index 0000000..eab6080 --- /dev/null +++ b/WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor @@ -0,0 +1,65 @@ +
+ @if (_loaded) + { +
+ poster + + @if (_pictureChanged || _pictureSaved is not null) + { +
+ @if (_pictureChanged) + { +
+ +
+
+ +
+ } + else if (_pictureSaved is not null) + { +
+ +
+ } +
+ } +
+ } + else + { +
+ +
+ } +
+ + + + \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor.cs b/WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor.cs new file mode 100644 index 0000000..4f9fa02 --- /dev/null +++ b/WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor.cs @@ -0,0 +1,122 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; +using WatchIt.Common.Model; + +namespace WatchIt.Website.Components; + +public partial class PictureEditorComponent : ComponentBase +{ + #region PARAMETERS + + [Parameter] public long? Id { get; set; } + [Parameter] public int ContentWidth { get; set; } = 300; + [Parameter] public string PicturePlaceholder { get; set; } = "assets/poster.png"; + [Parameter] public string Class { get; set; } = string.Empty; + [Parameter] public required Func, Task> PictureGetTask { get; set; } + [Parameter] public required Func, Task> PicturePutTask { get; set; } + [Parameter] public required Func PictureDeleteTask { get; set; } + + #endregion + + + + #region FIELDS + + private bool _loaded; + + private Picture? _pictureSaved; + private Picture? _pictureSelected; + private bool _pictureChanged; + private bool _pictureSaving; + private bool _pictureDeleting; + + #endregion + + + + #region PRIVATE METHODS + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + List endTask = new List(); + + // STEP 0 + if (Id.HasValue) + { + endTask.AddRange( + [ + PictureGetTask(Id.Value, data => + { + _pictureSaved = data; + _pictureSelected = data; + }) + ]); + } + + // END + await Task.WhenAll(endTask); + + _loaded = true; + StateHasChanged(); + } + } + + private async Task Load(InputFileChangeEventArgs args) + { + if (args.File.ContentType.StartsWith("image")) + { + Stream stream = args.File.OpenReadStream(5242880); + byte[] array; + using (MemoryStream ms = new MemoryStream()) + { + await stream.CopyToAsync(ms); + array = ms.ToArray(); + } + + _pictureSelected = new Picture + { + Image = array, + MimeType = args.File.ContentType + }; + _pictureChanged = true; + } + } + + private async Task Save() + { + void Success(Picture data) + { + _pictureSaved = data; + _pictureSelected = data; + _pictureChanged = false; + _pictureSaving = false; + } + + _pictureSaving = true; + await PicturePutTask(Id.Value, _pictureSelected, Success); + } + + private void Cancel() + { + _pictureSelected = _pictureSaved; + _pictureChanged = false; + } + + private async Task Delete() + { + void Success() + { + _pictureSaved = null; + _pictureSelected = null; + _pictureChanged = false; + _pictureDeleting = false; + } + + _pictureDeleting = true; + await PictureDeleteTask(Id.Value, Success); + } + + #endregion +} \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor.css b/WatchIt.Website/WatchIt.Website/Components/PictureEditorComponent.razor.css new file mode 100644 index 0000000..e69de29 diff --git a/WatchIt.Website/WatchIt.Website/Pages/AdminPage.razor b/WatchIt.Website/WatchIt.Website/Pages/AdminPage.razor index c13c42f..b3f992d 100644 --- a/WatchIt.Website/WatchIt.Website/Pages/AdminPage.razor +++ b/WatchIt.Website/WatchIt.Website/Pages/AdminPage.razor @@ -14,14 +14,14 @@
-

New movie

+

New movie

+
+ +

New TV series

+
+ +

New person

-
-

New TV series

-
-
-

New TV series

-
} else diff --git a/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor b/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor new file mode 100644 index 0000000..18d837e --- /dev/null +++ b/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor @@ -0,0 +1,43 @@ +@using WatchIt.Common.Model.Persons + +@page "/person/{id:long}/edit" +@page "/person/new" + + +@if (_loaded) +{ + if (_user?.IsAdmin == true) + { +
+
+
+
+

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

+
+
+
+
+
+ +
+
+
+
+
+
+ } + else + { + + } +} +else +{ +
+ +
+} diff --git a/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor.cs b/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor.cs new file mode 100644 index 0000000..52b0d46 --- /dev/null +++ b/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor.cs @@ -0,0 +1,77 @@ +using Microsoft.AspNetCore.Components; +using WatchIt.Common.Model.Persons; +using WatchIt.Website.Layout; +using WatchIt.Website.Services.Utility.Authentication; +using WatchIt.Website.Services.WebAPI.Persons; + +namespace WatchIt.Website.Pages; + +public partial class PersonEditPage : ComponentBase +{ + #region SERVICES + + [Inject] private IAuthenticationService AuthenticationService { get; set; } = default!; + [Inject] private IPersonsWebAPIService PersonsWebAPIService { get; set; } = default!; + + #endregion + + + + #region PARAMETERS + + [Parameter] public long? Id { get; set; } + + [CascadingParameter] public MainLayout Layout { get; set; } + + #endregion + + + + #region FIELDS + + private bool _loaded; + private string? _error; + + private User? _user; + + private PersonResponse? _person; + + #endregion + + + + #region PRIVATE METHODS + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + List step1Tasks = new List(); + List endTasks = new List(); + + // STEP 0 + step1Tasks.AddRange( + [ + Task.Run(async () => _user = await AuthenticationService.GetUserAsync()) + ]); + + // STEP 1 + await Task.WhenAll(step1Tasks); + if (_user?.IsAdmin == true && Id.HasValue) + { + endTasks.AddRange( + [ + PersonsWebAPIService.GetPerson(Id.Value, data => _person = data) + ]); + } + + // END + await Task.WhenAll(endTasks); + + _loaded = true; + StateHasChanged(); + } + } + + #endregion +} \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor.css b/WatchIt.Website/WatchIt.Website/Pages/PersonEditPage.razor.css new file mode 100644 index 0000000..e69de29 diff --git a/WatchIt.Website/WatchIt.Website/Program.cs b/WatchIt.Website/WatchIt.Website/Program.cs index 283995a..12b0924 100644 --- a/WatchIt.Website/WatchIt.Website/Program.cs +++ b/WatchIt.Website/WatchIt.Website/Program.cs @@ -11,6 +11,7 @@ using WatchIt.Website.Services.Utility.Tokens; using WatchIt.Website.Services.WebAPI.Accounts; using WatchIt.Website.Services.WebAPI.Media; using WatchIt.Website.Services.WebAPI.Movies; +using WatchIt.Website.Services.WebAPI.Persons; using WatchIt.Website.Services.WebAPI.Photos; using WatchIt.Website.Services.WebAPI.Series; @@ -75,6 +76,7 @@ public static class Program builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); return builder; } diff --git a/WatchIt.Website/WatchIt.Website/WatchIt.Website.csproj b/WatchIt.Website/WatchIt.Website/WatchIt.Website.csproj index 3ba0027..521dc46 100644 --- a/WatchIt.Website/WatchIt.Website/WatchIt.Website.csproj +++ b/WatchIt.Website/WatchIt.Website/WatchIt.Website.csproj @@ -22,6 +22,7 @@ + diff --git a/WatchIt.Website/WatchIt.Website/appsettings.json b/WatchIt.Website/WatchIt.Website/appsettings.json index ab714ae..69b7410 100644 --- a/WatchIt.Website/WatchIt.Website/appsettings.json +++ b/WatchIt.Website/WatchIt.Website/appsettings.json @@ -78,7 +78,10 @@ "PostPerson": "", "PutPerson": "/{0}", "DeletePerson": "/{0}", - "GetPersonsViewRank": "/view" + "GetPersonsViewRank": "/view", + "GetPersonPhoto": "/{0}/poster", + "PutPersonPhoto": "/{0}/poster", + "DeletePersonPhoto": "/{0}/poster" } } }