Refactoring, database structure changed

This commit is contained in:
2025-03-03 00:56:32 +01:00
Unverified
parent d3805ef3db
commit c603c41c0b
913 changed files with 21764 additions and 32775 deletions

View File

@@ -0,0 +1,154 @@
@using Blazorise.Extensions
@using WatchIt.DTO.Models.Controllers.Media.Medium.Response
@using WatchIt.DTO.Models.Controllers.Roles.Role.Response
@using WatchIt.Website.Components.Subcomponents.Common
@using Blazorise.Components
@inherits Component
<div class="panel @(Class)">
@if (_loaded)
{
<div class="vstack gap-3">
<div class="container-grid">
<div class="row gx-2">
<div class="col align-self-center">
<h4 class="m-0"><strong>Actor roles</strong></h4>
</div>
@if (!_editingMode)
{
<div class="col-auto">
<button type="button" class="btn btn-secondary" disabled="@(Disabled)" @onclick="@(() => ActivateEditData())">Add</button>
</div>
}
else
{
<div class="col-auto">
<button type="button" class="btn btn-secondary" @onclick="@(CancelEditData)">Cancel</button>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-secondary" disabled="@(_saving)" @onclick="@(SaveData)">
<LoadingButtonContent IsLoading="@(_saving)" Content="Save" LoadingContent="Saving..."/>
</button>
</div>
}
</div>
</div>
@if (!_editingMode)
{
if (_roles.IsNullOrEmpty())
{
<span class="text-center">No items</span>
}
else
{
<table class="table table-sm table-transparent">
<thead>
<tr>
<th scope="col">
Media name
</th>
<th scope="col">
Media type
</th>
<th scope="col">
Role type
</th>
<th scope="col">
Role name
</th>
<th class="table-cell-fit" scope="col">
Actions
</th>
</tr>
</thead>
<tbody class="table-group-divider">
@foreach (Guid roleId in _roles.Keys)
{
RoleActorResponse role = _roles[roleId].Data;
MediumResponse medium = _mediaDict[role.MediumId];
<tr>
<td class="align-middle">
@(medium.Title)@(medium.ReleaseDate.HasValue ? $" ({medium.ReleaseDate!.Value.Year})" : string.Empty)
</td>
<td class="align-middle">
@(medium.Type == MediumResponseType.Movie ? "Movie" : "TV Series")
</td>
<td class="align-middle">
@(_roleTypes[role.TypeId])
</td>
<td class="align-middle">
@(role.Name)
</td>
<td class="align-middle table-cell-fit">
<div class="hstack gap-1">
<button class="btn btn-outline-secondary btn-sm" type="button" disabled="@(Disabled || _roles[roleId].Deleting)" @onclick="@(() => ActivateEditData(roleId))"><i class="fas fa-edit"></i></button>
<button class="btn btn-outline-danger btn-sm" type="button" disabled="@(Disabled || _roles[roleId].Deleting)" @onclick="@(() => DeleteData(roleId))">
@if (_roles[roleId].Deleting)
{
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
}
else
{
<i class="fa-solid fa-trash"></i>
}
</button>
</div>
</td>
</tr>
}
</tbody>
</table>
}
}
else
{
<EditForm Model="@(_roleRequest)">
<AntiforgeryToken/>
<div class="container-grid">
<div class="row form-group mb-1">
<label for="actorFormMedia" class="col-1 col-form-label">Media:</label>
<div class="col">
<Autocomplete ElementId="actorFormMedia"
TItem="MediumResponse"
TValue="long"
Data="@(_mediaDict.Values)"
TextField="@(item => item.ReleaseDate.HasValue ? $"{item.Title} ({item.ReleaseDate.Value.Year})" : item.Title)"
ValueField="@(item => item.Id)"
@bind-SelectedValue="@(_roleRequest.MediumId)"
Placeholder="Search..."
Filter="AutocompleteFilter.Contains">
<NotFoundContent Context="not_found_context"> Sorry... @not_found_context was not found</NotFoundContent>
</Autocomplete>
</div>
</div>
<div class="row form-group my-1">
<label for="actorFormType" class="col-1 col-form-label">Type:</label>
<div class="col">
<InputSelect id="actorFormType" class="form-control" TValue="short" @bind-Value="@(_roleRequest.TypeId)">
@foreach (KeyValuePair<short, string> type in _roleTypes)
{
<option value="@(type.Key)">@(type.Value)</option>
}
</InputSelect>
</div>
</div>
<div class="row form-group my-1">
<label for="actorFormName" class="col-1 col-form-label">Name:</label>
<div class="col">
<InputText id="actorFormName" class="form-control" @bind-Value="@(_roleRequest.Name)"/>
</div>
</div>
</div>
</EditForm>
}
</div>
}
else
{
<Loading Color="@(Loading.Colors.Light)"/>
}
</div>

View File

@@ -0,0 +1,222 @@
using System.Net;
using Blazorise.Snackbar;
using Microsoft.AspNetCore.Components;
using Refit;
using WatchIt.DTO.Models.Controllers.Media.Medium.Response;
using WatchIt.DTO.Models.Controllers.People.Person;
using WatchIt.DTO.Models.Controllers.Roles.Role.Query;
using WatchIt.DTO.Models.Controllers.Roles.Role.Request;
using WatchIt.DTO.Models.Controllers.Roles.Role.Response;
using WatchIt.DTO.Models.Controllers.Roles.RoleActorType;
using WatchIt.DTO.Query;
using WatchIt.Website.Clients;
using WatchIt.Website.Components.Layout;
using WatchIt.Website.Services.Authentication;
namespace WatchIt.Website.Components.Panels.Pages.PersonEditPage;
public partial class ActorRolesEditPanel : Component
{
#region SERVICES
[Inject] private IAuthenticationService AuthenticationService { get; set; } = null!;
[Inject] private IMediaClient MediaClient { get; set; } = null!;
[Inject] private IRolesClient RolesClient { get; set; } = null!;
#endregion
#region PARAMETERS
[Parameter] public required PersonResponse Data { get; set; }
[Parameter] public List<MediumResponse>? Media { get; set; }
[Parameter] public bool Disabled { get; set; }
[Parameter] public string Class { get; set; } = string.Empty;
#endregion
#region FIELDS
private bool _loaded;
private bool _editingMode;
private Guid? _editedId;
private RoleActorRequest _roleRequest = null!;
private bool _saving;
private Dictionary<long, MediumResponse> _mediaDict = new Dictionary<long, MediumResponse>();
private Dictionary<short, string> _roleTypes = new Dictionary<short, string>();
private Dictionary<Guid, (RoleActorResponse Data, bool Deleting)> _roles = new Dictionary<Guid, (RoleActorResponse Data, bool Deleting)>();
#endregion
#region PRIVATE METHODS
protected override async Task OnFirstRenderAsync()
{
await base.OnFirstRenderAsync();
ResetRequest();
await Task.WhenAll(
[
LoadRoleTypes(),
LoadMedia()
]);
if (Data is not null)
{
await LoadRoles();
}
_loaded = true;
StateHasChanged();
}
private async Task LoadRoleTypes()
{
IApiResponse<IEnumerable<RoleActorTypeResponse>> response = await RolesClient.GetRoleActorTypes();
if (response.IsSuccessful)
{
_roleTypes = response.Content.ToDictionary(x => x.Id, x => x.Name);
}
else
{
await Base.SnackbarStack.PushAsync("An error occured. Actor role types list could not be obtained.", SnackbarColor.Danger);
}
}
private async Task LoadMedia()
{
IEnumerable<MediumResponse>? media = Media;
if (media is null)
{
IApiResponse<IEnumerable<MediumResponse>> response = await MediaClient.GetMedia(includePictures: true);
if (response.IsSuccessful)
{
media = response.Content;
}
else
{
await Base.SnackbarStack.PushAsync("An error occured. Media list could not be obtained.", SnackbarColor.Danger);
return;
}
}
_mediaDict = media.ToDictionary(x => x.Id, x => x);
}
private async Task LoadRoles()
{
RoleActorFilterQuery filter = new RoleActorFilterQuery
{
PersonId = Data.Id
};
OrderQuery order = new OrderQuery
{
OrderBy = "medium.release_date",
};
IApiResponse<IEnumerable<RoleActorResponse>> response = await RolesClient.GetRoleActors(filter, order);
if (response.IsSuccessful)
{
_roles = response.Content.ToDictionary(x => x.Id, x => (x, false));
}
else
{
await Base.SnackbarStack.PushAsync("An error occured. Actor roles could not be obtained.", SnackbarColor.Danger);
}
}
private void CancelEditData()
{
_editingMode = false;
}
private async Task SaveData()
{
string token = await AuthenticationService.GetRawAccessTokenAsync() ?? string.Empty;
_saving = true;
IApiResponse<RoleActorResponse> response = await (_editedId.HasValue switch
{
true => RolesClient.PutRoleActor(token, _editedId.Value, _roleRequest),
false => RolesClient.PostRoleActor(token, _roleRequest),
});
switch (response)
{
case { IsSuccessful: true }:
_roles[response.Content.Id] = (response.Content, false);
_roles = _roles.OrderBy(x => _mediaDict[x.Value.Data.MediumId].ReleaseDate)
.ToDictionary(x => x.Key, x => x.Value);
await Base.SnackbarStack.PushAsync("Role saved successfully.", SnackbarColor.Success);
break;
case { StatusCode: HttpStatusCode.Forbidden } or { StatusCode: HttpStatusCode.Unauthorized }:
await Base.SnackbarStack.PushAsync("You are not authorized to edit roles data.", SnackbarColor.Danger);
break;
case { StatusCode: HttpStatusCode.BadRequest }:
string? content = "An unknown error occured.";
if (response.Error is ValidationApiException ex)
{
string? exContent = ex.Content?.Errors.SelectMany(x => x.Value).FirstOrDefault();
if (exContent is not null)
{
content = exContent;
}
}
await Base.SnackbarStack.PushAsync(content, SnackbarColor.Danger);
break;
default:
await Base.SnackbarStack.PushAsync("An unknown error occured.", SnackbarColor.Danger);
break;
}
_saving = false;
_editingMode = false;
}
private void ActivateEditData(Guid? id = null)
{
_editedId = id;
ResetRequest();
if (id is not null && _roles.TryGetValue(id.Value, out (RoleActorResponse Data, bool Deleting) role))
{
_roleRequest.Name = role.Data.Name;
_roleRequest.MediumId = role.Data.MediumId;
_roleRequest.TypeId = role.Data.TypeId;
}
_editingMode = true;
}
private async Task DeleteData(Guid id)
{
_roles[id] = (_roles[id].Data, true);
string token = await AuthenticationService.GetRawAccessTokenAsync() ?? string.Empty;
IApiResponse response = await RolesClient.DeleteRole(token, id);
switch (response)
{
case {IsSuccessful: true}:
_roles.Remove(id);
await Base.SnackbarStack.PushAsync("Role removed successfully.", SnackbarColor.Success);
break;
case {StatusCode: HttpStatusCode.Forbidden} or {StatusCode: HttpStatusCode.Unauthorized}:
await Base.SnackbarStack.PushAsync("You are not authorized to remove roles.", SnackbarColor.Danger);
break;
default:
await Base.SnackbarStack.PushAsync("An unknown error occured.", SnackbarColor.Danger);
break;
}
}
private void ResetRequest() => _roleRequest = Data is null ? new RoleActorRequest() : new RoleActorRequest
{
PersonId = Data.Id,
};
#endregion
}

View File

@@ -0,0 +1,141 @@
@using Blazorise.Extensions
@using WatchIt.DTO.Models.Controllers.Media.Medium.Response
@using WatchIt.DTO.Models.Controllers.Roles.Role.Response
@using WatchIt.Website.Components.Subcomponents.Common
@using Blazorise.Components
@inherits Component
<div class="panel @(Class)">
@if (_loaded)
{
<div class="vstack gap-3">
<div class="container-grid">
<div class="row gx-2">
<div class="col align-self-center">
<h4 class="m-0"><strong>Creator roles</strong></h4>
</div>
@if (!_editingMode)
{
<div class="col-auto">
<button type="button" class="btn btn-secondary" disabled="@(Disabled)" @onclick="@(() => ActivateEditData())">Add</button>
</div>
}
else
{
<div class="col-auto">
<button type="button" class="btn btn-secondary" @onclick="@(CancelEditData)">Cancel</button>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-secondary" disabled="@(_saving)" @onclick="@(SaveData)">
<LoadingButtonContent IsLoading="@(_saving)" Content="Save" LoadingContent="Saving..."/>
</button>
</div>
}
</div>
</div>
@if (!_editingMode)
{
if (_roles.IsNullOrEmpty())
{
<span class="text-center">No items</span>
}
else
{
<table class="table table-sm table-transparent">
<thead>
<tr>
<th scope="col">
Media name
</th>
<th scope="col">
Media type
</th>
<th scope="col">
Role type
</th>
<th class="table-cell-fit" scope="col">
Actions
</th>
</tr>
</thead>
<tbody class="table-group-divider">
@foreach (Guid roleId in _roles.Keys)
{
RoleCreatorResponse role = _roles[roleId].Data;
MediumResponse medium = _mediaDict[role.MediumId];
<tr>
<td class="align-middle">
@(medium.Title)@(medium.ReleaseDate.HasValue ? $" ({medium.ReleaseDate!.Value.Year})" : string.Empty)
</td>
<td class="align-middle">
@(medium.Type == MediumResponseType.Movie ? "Movie" : "TV Series")
</td>
<td class="align-middle">
@(_roleTypes[role.TypeId])
</td>
<td class="align-middle table-cell-fit">
<div class="hstack gap-1">
<button class="btn btn-outline-secondary btn-sm" type="button" disabled="@(Disabled || _roles[roleId].Deleting)" @onclick="@(() => ActivateEditData(roleId))"><i class="fas fa-edit"></i></button>
<button class="btn btn-outline-danger btn-sm" type="button" disabled="@(Disabled || _roles[roleId].Deleting)" @onclick="@(() => DeleteData(roleId))">
@if (_roles[roleId].Deleting)
{
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
}
else
{
<i class="fa-solid fa-trash"></i>
}
</button>
</div>
</td>
</tr>
}
</tbody>
</table>
}
}
else
{
<EditForm Model="@(_roleRequest)">
<AntiforgeryToken/>
<div class="container-grid">
<div class="row form-group mb-1">
<label for="actorFormMedia" class="col-1 col-form-label">Media:</label>
<div class="col">
<Autocomplete ElementId="actorFormMedia"
TItem="MediumResponse"
TValue="long"
Data="@(_mediaDict.Values)"
TextField="@(item => item.ReleaseDate.HasValue ? $"{item.Title} ({item.ReleaseDate.Value.Year})" : item.Title)"
ValueField="@(item => item.Id)"
@bind-SelectedValue="@(_roleRequest.MediumId)"
Placeholder="Search..."
Filter="AutocompleteFilter.Contains">
<NotFoundContent Context="not_found_context"> Sorry... @not_found_context was not found</NotFoundContent>
</Autocomplete>
</div>
</div>
<div class="row form-group my-1">
<label for="actorFormType" class="col-1 col-form-label">Type:</label>
<div class="col">
<InputSelect id="actorFormType" class="form-control" TValue="short" @bind-Value="@(_roleRequest.TypeId)">
@foreach (KeyValuePair<short, string> type in _roleTypes)
{
<option value="@(type.Key)">@(type.Value)</option>
}
</InputSelect>
</div>
</div>
</div>
</EditForm>
}
</div>
}
else
{
<Loading Color="@(Loading.Colors.Light)"/>
}
</div>

View File

@@ -0,0 +1,221 @@
using System.Net;
using Blazorise.Snackbar;
using Microsoft.AspNetCore.Components;
using Refit;
using WatchIt.DTO.Models.Controllers.Media.Medium.Response;
using WatchIt.DTO.Models.Controllers.People.Person;
using WatchIt.DTO.Models.Controllers.Roles.Role.Query;
using WatchIt.DTO.Models.Controllers.Roles.Role.Request;
using WatchIt.DTO.Models.Controllers.Roles.Role.Response;
using WatchIt.DTO.Models.Controllers.Roles.RoleCreatorType;
using WatchIt.DTO.Query;
using WatchIt.Website.Clients;
using WatchIt.Website.Components.Layout;
using WatchIt.Website.Services.Authentication;
namespace WatchIt.Website.Components.Panels.Pages.PersonEditPage;
public partial class CreatorRolesEditPanel : Component
{
#region SERVICES
[Inject] private IAuthenticationService AuthenticationService { get; set; } = null!;
[Inject] private IPeopleClient PersonsClient { get; set; } = null!;
[Inject] private IMediaClient MediaClient { get; set; } = null!;
[Inject] private IRolesClient RolesClient { get; set; } = null!;
#endregion
#region PARAMETERS
[Parameter] public required PersonResponse Data { get; set; }
[Parameter] public List<MediumResponse>? Media { get; set; }
[Parameter] public bool Disabled { get; set; }
[Parameter] public string Class { get; set; } = string.Empty;
#endregion
#region FIELDS
private bool _loaded;
private bool _editingMode;
private Guid? _editedId;
private RoleCreatorRequest _roleRequest = null!;
private bool _saving;
private Dictionary<long, MediumResponse> _mediaDict = new Dictionary<long, MediumResponse>();
private Dictionary<short, string> _roleTypes = new Dictionary<short, string>();
private Dictionary<Guid, (RoleCreatorResponse Data, bool Deleting)> _roles = new Dictionary<Guid, (RoleCreatorResponse Data, bool Deleting)>();
#endregion
#region PRIVATE METHODS
protected override async Task OnFirstRenderAsync()
{
await base.OnFirstRenderAsync();
ResetRequest();
await Task.WhenAll(
[
LoadRoleTypes(),
LoadMedia(),
]);
if (Data is not null)
{
await LoadRoles();
}
_loaded = true;
StateHasChanged();
}
private async Task LoadRoleTypes()
{
IApiResponse<IEnumerable<RoleCreatorTypeResponse>> response = await RolesClient.GetRoleCreatorTypes();
if (response.IsSuccessful)
{
_roleTypes = response.Content.ToDictionary(x => x.Id, x => x.Name);
}
else
{
await Base.SnackbarStack.PushAsync("An error occured. Creator role types list could not be obtained.", SnackbarColor.Danger);
}
}
private async Task LoadMedia()
{
IEnumerable<MediumResponse>? media = Media;
if (media is null)
{
IApiResponse<IEnumerable<MediumResponse>> response = await MediaClient.GetMedia();
if (response.IsSuccessful)
{
media = response.Content;
}
else
{
await Base.SnackbarStack.PushAsync("An error occured. Media list could not be obtained.", SnackbarColor.Danger);
return;
}
}
_mediaDict = media.ToDictionary(x => x.Id, x => x);
}
private async Task LoadRoles()
{
RoleCreatorFilterQuery filter = new RoleCreatorFilterQuery
{
PersonId = Data.Id
};
OrderQuery order = new OrderQuery
{
OrderBy = "medium.release_date",
};
IApiResponse<IEnumerable<RoleCreatorResponse>> response = await RolesClient.GetRoleCreators(filter, order);
if (response.IsSuccessful)
{
_roles = response.Content.ToDictionary(x => x.Id, x => (x, false));
}
else
{
await Base.SnackbarStack.PushAsync("An error occured. Creator roles could not be obtained.", SnackbarColor.Danger);
}
}
private void CancelEditData()
{
_editingMode = false;
}
private async Task SaveData()
{
string token = await AuthenticationService.GetRawAccessTokenAsync() ?? string.Empty;
_saving = true;
IApiResponse<RoleCreatorResponse> response = await (_editedId.HasValue switch
{
true => RolesClient.PutRoleCreator(token, _editedId.Value, _roleRequest),
false => RolesClient.PostRoleCreator(token, _roleRequest),
});
switch (response)
{
case { IsSuccessful: true }:
_roles[response.Content.Id] = (response.Content, false);
_roles = _roles.OrderBy(x => _mediaDict[x.Value.Data.MediumId].ReleaseDate)
.ToDictionary(x => x.Key, x => x.Value);
await Base.SnackbarStack.PushAsync("Role saved successfully.", SnackbarColor.Success);
break;
case { StatusCode: HttpStatusCode.Forbidden } or { StatusCode: HttpStatusCode.Unauthorized }:
await Base.SnackbarStack.PushAsync("You are not authorized to edit roles data.", SnackbarColor.Danger);
break;
case { StatusCode: HttpStatusCode.BadRequest }:
string? content = "An unknown error occured.";
if (response.Error is ValidationApiException ex)
{
string? exContent = ex.Content?.Errors.SelectMany(x => x.Value).FirstOrDefault();
if (exContent is not null)
{
content = exContent;
}
}
await Base.SnackbarStack.PushAsync(content, SnackbarColor.Danger);
break;
default:
await Base.SnackbarStack.PushAsync("An unknown error occured.", SnackbarColor.Danger);
break;
}
_saving = false;
_editingMode = false;
}
private void ActivateEditData(Guid? id = null)
{
_editedId = id;
ResetRequest();
if (id is not null && _roles.TryGetValue(id.Value, out (RoleCreatorResponse Data, bool Deleting) role))
{
_roleRequest.MediumId = role.Data.MediumId;
_roleRequest.TypeId = role.Data.TypeId;
}
_editingMode = true;
}
private async Task DeleteData(Guid id)
{
_roles[id] = (_roles[id].Data, true);
string token = await AuthenticationService.GetRawAccessTokenAsync() ?? string.Empty;
IApiResponse response = await RolesClient.DeleteRole(token, id);
switch (response)
{
case {IsSuccessful: true}:
_roles.Remove(id);
await Base.SnackbarStack.PushAsync("Role removed successfully.", SnackbarColor.Success);
break;
case {StatusCode: HttpStatusCode.Forbidden} or {StatusCode: HttpStatusCode.Unauthorized}:
await Base.SnackbarStack.PushAsync("You are not authorized to remove roles.", SnackbarColor.Danger);
break;
default:
await Base.SnackbarStack.PushAsync("An unknown error occured.", SnackbarColor.Danger);
break;
}
}
private void ResetRequest() => _roleRequest = Data is null ? new RoleCreatorRequest() : new RoleCreatorRequest
{
PersonId = Data.Id,
};
#endregion
}

View File

@@ -0,0 +1,68 @@
@using WatchIt.DTO.Models.Controllers.Genders.Gender
@using WatchIt.Website.Components.Subcomponents.Common
@inherits Component
<div class="panel @(Class)">
@if (_loaded)
{
<EditForm Model="@(_request)">
<AntiforgeryToken/>
<div class="container-grid">
<div class="row form-group mb-1">
<label for="name" class="col-2 col-form-label">Name*</label>
<div class="col-10">
<InputText id="name" class="form-control" @bind-Value="_request!.Name"/>
</div>
</div>
<div class="row form-group mb-1">
<label for="fullName" class="col-2 col-form-label">Full name</label>
<div class="col-10">
<InputText id="fullName" class="form-control" @bind-Value="_request!.FullName"/>
</div>
</div>
<div class="row form-group my-1">
<label for="desc" class="col-2 col-form-label">Description</label>
<div class="col-10">
<InputTextArea id="desc" class="form-control" @bind-Value="_request!.Description"/>
</div>
</div>
<div class="row form-group mb-1">
<label for="birthDeathDates" class="col-2 col-form-label">Birth and death</label>
<div class="col-10">
<div id="birthDeathDates" class="input-group">
<InputDate TValue="DateOnly?" class="form-control" @bind-Value="_request!.BirthDate"/>
<span class="input-group-text">-</span>
<InputDate TValue="DateOnly?" class="form-control" @bind-Value="_request!.DeathDate"/>
</div>
</div>
</div>
<div class="row form-group my-1">
<label for="desc" class="col-2 col-form-label">Gender</label>
<div class="col-10">
<InputSelect TValue="short?" id="desc" class="form-control" @bind-Value="_request!.GenderId">
<option value="@(default(short?))">No choice</option>
@foreach (GenderResponse gender in _genders)
{
<option value="@(gender.Id)">@(gender.Name)</option>
}
</InputSelect>
</div>
</div>
<div class="row mt-2">
<div class="col d-flex flex-column align-items-end">
<button type="submit" class="btn btn-secondary" disabled="@(_saving)" @onclick="@(SaveData)">
<LoadingButtonContent IsLoading="@(_saving)" Content="Save" LoadingContent="Saving..."/>
</button>
</div>
</div>
</div>
</EditForm>
}
else
{
<Loading Color="@(Loading.Colors.Light)"/>
}
</div>

View File

@@ -0,0 +1,117 @@
using System.Net;
using Blazorise.Snackbar;
using Microsoft.AspNetCore.Components;
using Refit;
using WatchIt.DTO.Models.Controllers.Genders.Gender;
using WatchIt.DTO.Models.Controllers.People;
using WatchIt.DTO.Models.Controllers.People.Person;
using WatchIt.Website.Clients;
using WatchIt.Website.Components.Layout;
using WatchIt.Website.Services.Authentication;
namespace WatchIt.Website.Components.Panels.Pages.PersonEditPage;
public partial class EditFormPanel : Component
{
#region SERVICES
[Inject] private NavigationManager NavigationManager { get; set; } = null!;
[Inject] private IAuthenticationService AuthenticationService { get; set; } = null!;
[Inject] private IPeopleClient PeopleClient { get; set; } = null!;
[Inject] private IGendersClient GendersClient { get; set; } = null!;
#endregion
#region PARAMETERS
[Parameter] public PersonResponse? Data { get; set; }
[Parameter] public string Class { get; set; } = string.Empty;
#endregion
#region FIELDS
private bool _loaded;
private bool _saving;
private List<GenderResponse> _genders = [];
private PersonRequest _request = new PersonRequest();
#endregion
#region PRIVATE METHODS
protected override async Task OnFirstRenderAsync()
{
await base.OnFirstRenderAsync();
if (Data is not null)
{
_request = Data.ToRequest();
}
IApiResponse<IEnumerable<GenderResponse>> gendersResponse = await GendersClient.GetGenders();
if (gendersResponse.IsSuccessful)
{
_genders.AddRange(gendersResponse.Content);
}
else
{
await Base.SnackbarStack.PushAsync("An error has occured. List of genders could not be obtained.", SnackbarColor.Danger);
}
_loaded = true;
StateHasChanged();
}
private async Task SaveData()
{
_saving = true;
string token = await AuthenticationService.GetRawAccessTokenAsync() ?? string.Empty;
IApiResponse<PersonResponse> response = await (Data switch
{
null => PeopleClient.PostPerson(token, _request),
_ => PeopleClient.PutPerson(token, Data.Id, _request),
});
switch (response)
{
case { IsSuccessful: true }:
switch (Data)
{
case null: NavigationManager.NavigateTo($"people/{response.Content.Id}/edit", true); break;
default: await Base.SnackbarStack.PushAsync("Data saved successfully.", SnackbarColor.Success); break;
}
break;
case { StatusCode: HttpStatusCode.Forbidden } or { StatusCode: HttpStatusCode.Unauthorized }:
await Base.SnackbarStack.PushAsync("You are not authorized to edit people data.", SnackbarColor.Danger);
break;
case { StatusCode: HttpStatusCode.BadRequest }:
string? content = "An unknown error occured.";
if (response.Error is ValidationApiException ex)
{
string? exContent = ex.Content?.Errors.SelectMany(x => x.Value).FirstOrDefault();
if (exContent is not null)
{
content = exContent;
}
}
await Base.SnackbarStack.PushAsync(content, SnackbarColor.Danger);
break;
default:
await Base.SnackbarStack.PushAsync("An unknown error occured.", SnackbarColor.Danger);
break;
}
_saving = false;
}
#endregion
}

View File

@@ -0,0 +1,27 @@
@using WatchIt.Website.Components.Subcomponents.Common
@inherits Component
<div class="panel" role="button" @onclick="@(Data is not null ? () => NavigationManager.NavigateTo($"/people/{Data.Id}") : null)" style="cursor: @(Data is null ? "default" : "pointer")">
<div class="d-flex gap-3 align-items-center">
<Image Content="@(Data?.Picture)" Height="60" Placeholder="/assets/placeholders/person.png" AlternativeText="poster"/>
<div class="d-flex-inline flex-column">
<h2 id="primaryText" class="m-0">
@if (Data is null)
{
<span class="fw-bold">New person</span>
}
else
{
<span class="fw-bold">@(Data.Name)</span>
}
</h2>
@if (Data is not null)
{
<span id="secondaryText" class="text-secondary">Person settings</span>
}
</div>
</div>
</div>

View File

@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Components;
using WatchIt.DTO.Models.Controllers.People.Person;
using WatchIt.Website.Clients;
namespace WatchIt.Website.Components.Panels.Pages.PersonEditPage;
public partial class HeaderPanel : Component
{
#region SERVICES
[Inject] public IPeopleClient PeopleClient { get; set; } = null!;
[Inject] public NavigationManager NavigationManager { get; set; } = null!;
#endregion
#region PARAMETERS
[Parameter] public required PersonResponse? Data { get; set; }
#endregion
}

View File

@@ -0,0 +1,9 @@
/* IDS */
#primaryText {
margin-top: -8px !important;
}
#secondaryText {
color: lightgray !important;
}