Merge pull request #152 from mateuszskoczek/features/user_search

Features/user search
This commit is contained in:
2024-11-07 00:27:05 +01:00
committed by GitHub
Unverified
17 changed files with 326 additions and 18 deletions

View File

@@ -0,0 +1,67 @@
using Microsoft.AspNetCore.Mvc;
using WatchIt.Common.Query;
namespace WatchIt.Common.Model.Accounts;
public class AccountQueryParameters : QueryParameters<AccountResponse>
{
#region PROPERTIES
[FromQuery(Name = "username")]
public string? Username { get; set; }
[FromQuery(Name = "email")]
public string? Email { get; set; }
[FromQuery(Name = "description")]
public string? Description { get; set; }
[FromQuery(Name = "gender_id")]
public short? GenderId { get; set; }
[FromQuery(Name = "last_active")]
public DateOnly? LastActive { get; set; }
[FromQuery(Name = "last_active_from")]
public DateOnly? LastActiveFrom { get; set; }
[FromQuery(Name = "last_active_to")]
public DateOnly? LastActiveTo { get; set; }
[FromQuery(Name = "creation_date")]
public DateOnly? CreationDate { get; set; }
[FromQuery(Name = "creation_date_from")]
public DateOnly? CreationDateFrom { get; set; }
[FromQuery(Name = "creation_date_to")]
public DateOnly? CreationDateTo { get; set; }
[FromQuery(Name = "is_admin")]
public bool? IsAdmin { get; set; }
#endregion
#region PRIVATE METHODS
protected override bool IsMeetingConditions(AccountResponse item) =>
(
TestStringWithRegex(item.Username, Username)
&&
TestStringWithRegex(item.Email, Email)
&&
TestStringWithRegex(item.Description, Description)
&&
Test(item.Gender?.Id, GenderId)
&&
TestComparable(item.LastActive, LastActive, LastActiveFrom, LastActiveTo)
&&
TestComparable(item.CreationDate, CreationDate, CreationDateFrom, CreationDateTo)
&&
Test(item.IsAdmin, IsAdmin)
);
#endregion
}

View File

@@ -1,13 +1,28 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using WatchIt.Common.Model.Genders;
using WatchIt.Common.Query;
namespace WatchIt.Common.Model.Accounts;
public class AccountResponse : Account
public class AccountResponse : Account, IQueryOrderable<AccountResponse>
{
#region PROPERTIES
[JsonIgnore]
public static IDictionary<string, Func<AccountResponse, IComparable>> OrderableProperties { get; } = new Dictionary<string, Func<AccountResponse, IComparable>>
{
{ "id", x => x.Id },
{ "username", x => x.Username },
{ "email", x => x.Email },
{ "description", x => x.Description },
{ "gender", x => x.Gender.Name },
{ "last_active", x => x.LastActive },
{ "creation_date", x => x.CreationDate },
{ "is_admin", x => x.IsAdmin }
};
[JsonPropertyName("id")]
public required long Id { get; set; }

View File

@@ -94,11 +94,16 @@ public class AccountsController(IAccountsControllerService accountsControllerSer
#region Info
[HttpGet("{id}/info")]
[HttpGet]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<AccountResponse>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetAccounts(AccountQueryParameters query) => await accountsControllerService.GetAccounts(query);
[HttpGet("{id}")]
[AllowAnonymous]
[ProducesResponseType(typeof(AccountResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> GetAccountInfo([FromRoute]long id) => await accountsControllerService.GetAccountInfo(id);
public async Task<ActionResult> GetAccount([FromRoute]long id) => await accountsControllerService.GetAccount(id);
[HttpPut("profile_info")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]

View File

@@ -238,7 +238,15 @@ public class AccountsControllerService(
#region Info
public async Task<RequestResult> GetAccountInfo(long id)
public async Task<RequestResult> GetAccounts(AccountQueryParameters query)
{
IEnumerable<Account> accounts = await database.Accounts.ToListAsync();
IEnumerable<AccountResponse> accountsData = accounts.Select(x => new AccountResponse(x));
accountsData = query.PrepareData(accountsData);
return RequestResult.Ok(accountsData);
}
public async Task<RequestResult> GetAccount(long id)
{
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
if (account is null)

View File

@@ -19,7 +19,8 @@ public interface IAccountsControllerService
Task<RequestResult> GetAccountProfileBackground(long id);
Task<RequestResult> PutAccountProfileBackground(AccountProfileBackgroundRequest data);
Task<RequestResult> DeleteAccountProfileBackground();
Task<RequestResult> GetAccountInfo(long id);
Task<RequestResult> GetAccounts(AccountQueryParameters query);
Task<RequestResult> GetAccount(long id);
Task<RequestResult> PutAccountProfileInfo(AccountProfileInfoRequest data);
Task<RequestResult> PatchAccountUsername(AccountUsernameRequest data);
Task<RequestResult> PatchAccountEmail(AccountEmailRequest data);

View File

@@ -150,9 +150,22 @@ public class AccountsClientService(IHttpClientService httpClientService, IConfig
.ExecuteAction();
}
public async Task GetAccountInfo(long id, Action<AccountResponse>? successAction = null, Action? notFoundAction = null)
public async Task GetAccounts(AccountQueryParameters query, Action<IEnumerable<AccountResponse>>? successAction = null)
{
string url = GetUrl(EndpointsConfiguration.Accounts.GetAccountInfo, id);
string url = GetUrl(EndpointsConfiguration.Accounts.GetAccounts);
HttpRequest request = new HttpRequest(HttpMethodType.Get, url)
{
Query = query
};
HttpResponse response = await httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.ExecuteAction();
}
public async Task GetAccount(long id, Action<AccountResponse>? successAction = null, Action? notFoundAction = null)
{
string url = GetUrl(EndpointsConfiguration.Accounts.GetAccount, id);
HttpRequest request = new HttpRequest(HttpMethodType.Get, url);
HttpResponse response = await httpClientService.SendRequestAsync(request);

View File

@@ -18,7 +18,8 @@ public interface IAccountsClientService
Task GetAccountProfileBackground(long id, Action<PhotoResponse>? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? notFoundAction = null);
Task PutAccountProfileBackground(AccountProfileBackgroundRequest data, Action<PhotoResponse>? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null);
Task DeleteAccountProfileBackground(Action? successAction = null, Action? unauthorizedAction = null);
Task GetAccountInfo(long id, Action<AccountResponse>? successAction = null, Action? notFoundAction = null);
Task GetAccounts(AccountQueryParameters query, Action<IEnumerable<AccountResponse>>? successAction = null);
Task GetAccount(long id, Action<AccountResponse>? successAction = null, Action? notFoundAction = null);
Task PutAccountProfileInfo(AccountProfileInfoRequest data, Action? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null);
Task PatchAccountUsername(AccountUsernameRequest data, Action? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null);
Task PatchAccountEmail(AccountEmailRequest data, Action? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null);

View File

@@ -13,7 +13,8 @@ public class Accounts
public string GetAccountProfileBackground { get; set; }
public string PutAccountProfileBackground { get; set; }
public string DeleteAccountProfileBackground { get; set; }
public string GetAccountInfo { get; set; }
public string GetAccounts { get; set; }
public string GetAccount { get; set; }
public string PutAccountProfileInfo { get; set; }
public string PatchAccountUsername { get; set; }
public string PatchAccountEmail { get; set; }

View File

@@ -0,0 +1,77 @@
@using Blazorise.Extensions
@using WatchIt.Website.Components.Pages.SearchPage.Subcomponents
<div class="panel">
<div class="container-grid">
<div class="row mb-4">
<div class="col">
<h4 class="m-0"><strong>Users</strong></h4>
</div>
</div>
@if (_loaded)
{
if (!_items.IsNullOrEmpty())
{
for (int i = 0; i < _items.Count; i++)
{
if (i > 0)
{
<div class="row">
<div class="col">
<hr/>
</div>
</div>
}
<div class="row">
<div class="col">
@{
int iCopy = i;
}
<UserSearchResultItemComponent Item="@(_items[iCopy])"/>
</div>
</div>
}
if (!_allItemsLoaded)
{
<div class="row mt-3">
<div class="col">
<div class="d-flex justify-content-center">
<button class="btn btn-secondary" @onclick="DownloadItems">
@if (!_itemsLoading)
{
<span>Load more</span>
}
else
{
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<span>Loading...</span>
}
</button>
</div>
</div>
</div>
}
}
else
{
<div class="row">
<div class="col">
<div class="d-flex justify-content-center">
No items found
</div>
</div>
</div>
}
}
else
{
<div class="row">
<div class="col">
<LoadingComponent Color="@(LoadingComponent.LoadingComponentColors.Light)"/>
</div>
</div>
}
</div>
</div>

View File

@@ -0,0 +1,97 @@
using Microsoft.AspNetCore.Components;
using WatchIt.Common.Model.Accounts;
using WatchIt.Website.Services.Client.Accounts;
namespace WatchIt.Website.Components.Pages.SearchPage.Panels;
public partial class UsersSearchResultPanelComponent : ComponentBase
{
#region SERVICES
[Inject] private IAccountsClientService AccountsClientService { get; set; } = default!;
#endregion
#region PARAMETERS
[Parameter] public required string Query { get; set; }
#endregion
#region FIELDS
private bool _loaded;
private AccountQueryParameters _query = new AccountQueryParameters
{
First = 5
};
private List<AccountResponse> _items = [];
private bool _allItemsLoaded;
private bool _itemsLoading;
#endregion
#region PRIVATE METHODS
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
// INIT
_query.Username = Query;
List<Task> endTasks = new List<Task>();
// STEP 0
endTasks.AddRange(
[
AccountsClientService.GetAccounts(_query, data =>
{
_items.AddRange(data);
if (data.Count() < 5)
{
_allItemsLoaded = true;
}
else
{
_query.After = 5;
}
})
]);
// END
await Task.WhenAll(endTasks);
_loaded = true;
StateHasChanged();
}
}
private async Task DownloadItems()
{
_itemsLoading = true;
await AccountsClientService.GetAccounts(_query, data =>
{
_items.AddRange(data);
if (data.Count() < 5)
{
_allItemsLoaded = true;
}
else
{
_query.After += 5;
}
_itemsLoading = false;
});
}
#endregion
}

View File

@@ -0,0 +1,7 @@
<a class="text-decoration-none text-reset" href="/user/@(Item.Id)">
<div class="d-flex align-items-center gap-4">
<AccountPictureComponent Id="@(Item.Id)"
Size="90"/>
<h4 class="fw-bold">@(Item.Username)</h4>
</div>
</a>

View File

@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Components;
using WatchIt.Common.Model.Accounts;
namespace WatchIt.Website.Components.Pages.SearchPage.Subcomponents;
public partial class UserSearchResultItemComponent : ComponentBase
{
#region PROPERTIES
[Parameter] public required AccountResponse Item { get; set; }
#endregion
}

View File

@@ -91,7 +91,7 @@ public partial class MainLayout : LayoutComponentBase
if (_user is not null)
{
await AccountsClientService.GetAccountInfo(_user.Id, data => _accountData = data);
await AccountsClientService.GetAccount(_user.Id, data => _accountData = data);
}
_loaded = true;
@@ -121,7 +121,7 @@ public partial class MainLayout : LayoutComponentBase
if (!string.IsNullOrWhiteSpace(_searchbarText))
{
string query = WebUtility.UrlEncode(_searchbarText);
NavigationManager.NavigateTo($"/search/{query}");
NavigationManager.NavigateTo($"/search/{query}", true);
}
}

View File

@@ -1,3 +1,4 @@
@using System.Net
@using WatchIt.Common.Model.Movies
@using WatchIt.Common.Model.Persons
@using WatchIt.Common.Model.Series
@@ -15,7 +16,7 @@
<div class="rounded-3 panel panel-regular p-3">
<div class="d-flex justify-content-center">
<h3 class="m-0">
<strong>Search results for phrase:</strong> "@(DecodedQuery)"
<strong>Search results for phrase:</strong> "@(WebUtility.UrlDecode(Query))"
</h3>
</div>
</div>
@@ -27,7 +28,7 @@
NameSource="@(item => item.Title)"
AdditionalNameInfoSource="@(item => item.ReleaseDate.HasValue ? $" ({item.ReleaseDate.Value.Year})" : null)"
RatingSource="@(item => item.Rating)"
Query="@(new MovieQueryParameters { Title = DecodedQuery, OrderBy = "rating.count" })"
Query="@(new MovieQueryParameters { Title = WebUtility.UrlDecode(Query), OrderBy = "rating.count" })"
ItemDownloadingTask="@(MoviesClientService.GetAllMovies)"
PictureDownloadingTask="@((id, action) => MediaClientService.GetMediaPoster(id, action))"
PosterPlaceholder="/assets/media_poster.png"
@@ -43,7 +44,7 @@
NameSource="@(item => item.Title)"
AdditionalNameInfoSource="@(item => item.ReleaseDate.HasValue ? $" ({item.ReleaseDate.Value.Year})" : null)"
RatingSource="@(item => item.Rating)"
Query="@(new SeriesQueryParameters { Title = DecodedQuery, OrderBy = "rating.count" })"
Query="@(new SeriesQueryParameters { Title = WebUtility.UrlDecode(Query), OrderBy = "rating.count" })"
ItemDownloadingTask="@(SeriesClientService.GetAllSeries)"
PictureDownloadingTask="@((id, action) => MediaClientService.GetMediaPoster(id, action))"
PosterPlaceholder="/assets/media_poster.png"
@@ -58,9 +59,10 @@
IdSource="@(item => item.Id)"
NameSource="@(item => item.Name)"
RatingSource="@(item => item.Rating)"
Query="@(new PersonQueryParameters { Name = DecodedQuery, OrderBy = "rating.count" })"
Query="@(new PersonQueryParameters { Name = WebUtility.UrlDecode(Query), OrderBy = "rating.count" })"
ItemDownloadingTask="@(PersonsClientService.GetAllPersons)"
PictureDownloadingTask="@((id, action) => PersonsClientService.GetPersonPhoto(id, action))"
PosterPlaceholder="/assets/person_poster.png"
GetGlobalRatingMethod="@((id, action) => PersonsClientService.GetPersonGlobalRating(id, action))"/>
<UsersSearchResultPanelComponent Query="@(WebUtility.UrlDecode(Query))"/>
</div>

View File

@@ -58,7 +58,7 @@ public partial class UserEditPage : ComponentBase
await Task.WhenAll(
[
AccountsClientService.GetAccountInfo(user.Id, data => _accountData = data),
AccountsClientService.GetAccount(user.Id, data => _accountData = data),
AccountsClientService.GetAccountProfileBackground(user.Id, data => Layout.BackgroundPhoto = data)
]);
StateHasChanged();

View File

@@ -91,7 +91,7 @@ public partial class UserPage : ComponentBase
Id = user.Id;
}
await AccountsClientService.GetAccountInfo(Id.Value, data => _accountData = data);
await AccountsClientService.GetAccount(Id.Value, data => _accountData = data);
_owner = Id.Value == user?.Id;
}

View File

@@ -27,7 +27,8 @@
"GetAccountProfileBackground": "/{0}/profile_background",
"PutAccountProfileBackground": "/profile_background",
"DeleteAccountProfileBackground": "/profile_background",
"GetAccountInfo": "/{0}/info",
"GetAccounts": "",
"GetAccount": "/{0}",
"PutAccountProfileInfo": "/profile_info",
"PatchAccountUsername": "/username",
"PatchAccountEmail": "/email",