diff --git a/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountQueryParameters.cs b/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountQueryParameters.cs new file mode 100644 index 0000000..88221db --- /dev/null +++ b/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountQueryParameters.cs @@ -0,0 +1,67 @@ +using Microsoft.AspNetCore.Mvc; +using WatchIt.Common.Query; + +namespace WatchIt.Common.Model.Accounts; + +public class AccountQueryParameters : QueryParameters +{ + #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 +} \ No newline at end of file diff --git a/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountResponse.cs b/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountResponse.cs index fb3bc77..8ab8756 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountResponse.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountResponse.cs @@ -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 { #region PROPERTIES + [JsonIgnore] + public static IDictionary> OrderableProperties { get; } = new Dictionary> + { + { "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; } diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/AccountsController.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/AccountsController.cs index a2b60a5..a57a424 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/AccountsController.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/AccountsController.cs @@ -94,6 +94,11 @@ public class AccountsController(IAccountsControllerService accountsControllerSer #region Info + [HttpGet] + [AllowAnonymous] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAccounts(AccountQueryParameters query) => await accountsControllerService.GetAccounts(query); + [HttpGet("{id}")] [AllowAnonymous] [ProducesResponseType(typeof(AccountResponse), StatusCodes.Status200OK)] diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Accounts/AccountsControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Accounts/AccountsControllerService.cs index 0570ff3..14fc4ba 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Accounts/AccountsControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Accounts/AccountsControllerService.cs @@ -238,6 +238,14 @@ public class AccountsControllerService( #region Info + public async Task GetAccounts(AccountQueryParameters query) + { + IEnumerable accounts = await database.Accounts.ToListAsync(); + IEnumerable accountsData = accounts.Select(x => new AccountResponse(x)); + accountsData = query.PrepareData(accountsData); + return RequestResult.Ok(accountsData); + } + public async Task GetAccount(long id) { Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id); diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Accounts/IAccountsControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Accounts/IAccountsControllerService.cs index 34996cb..bb03dc5 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Accounts/IAccountsControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Accounts/IAccountsControllerService.cs @@ -19,6 +19,7 @@ public interface IAccountsControllerService Task GetAccountProfileBackground(long id); Task PutAccountProfileBackground(AccountProfileBackgroundRequest data); Task DeleteAccountProfileBackground(); + Task GetAccounts(AccountQueryParameters query); Task GetAccount(long id); Task PutAccountProfileInfo(AccountProfileInfoRequest data); Task PatchAccountUsername(AccountUsernameRequest data); diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Client/Accounts/AccountsClientService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Client/Accounts/AccountsClientService.cs index 01e50de..df8649f 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Client/Accounts/AccountsClientService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Client/Accounts/AccountsClientService.cs @@ -150,6 +150,19 @@ public class AccountsClientService(IHttpClientService httpClientService, IConfig .ExecuteAction(); } + public async Task GetAccounts(AccountQueryParameters query, Action>? successAction = null) + { + 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? successAction = null, Action? notFoundAction = null) { string url = GetUrl(EndpointsConfiguration.Accounts.GetAccount, id); diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Client/Accounts/IAccountsClientService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Client/Accounts/IAccountsClientService.cs index 7b0e4cc..09543c6 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Client/Accounts/IAccountsClientService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Client/Accounts/IAccountsClientService.cs @@ -18,6 +18,7 @@ public interface IAccountsClientService Task GetAccountProfileBackground(long id, Action? successAction = null, Action>? badRequestAction = null, Action? notFoundAction = null); Task PutAccountProfileBackground(AccountProfileBackgroundRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null); Task DeleteAccountProfileBackground(Action? successAction = null, Action? unauthorizedAction = null); + Task GetAccounts(AccountQueryParameters query, Action>? successAction = null); Task GetAccount(long id, Action? successAction = null, Action? notFoundAction = null); Task PutAccountProfileInfo(AccountProfileInfoRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null); Task PatchAccountUsername(AccountUsernameRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null); diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Configuration/Model/Accounts.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Configuration/Model/Accounts.cs index 1479b8d..eae345a 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Configuration/Model/Accounts.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Configuration/Model/Accounts.cs @@ -13,6 +13,7 @@ public class Accounts public string GetAccountProfileBackground { get; set; } public string PutAccountProfileBackground { get; set; } public string DeleteAccountProfileBackground { get; set; } + public string GetAccounts { get; set; } public string GetAccount { get; set; } public string PutAccountProfileInfo { get; set; } public string PatchAccountUsername { get; set; } diff --git a/WatchIt.Website/WatchIt.Website/appsettings.json b/WatchIt.Website/WatchIt.Website/appsettings.json index 8099a36..9f2429a 100644 --- a/WatchIt.Website/WatchIt.Website/appsettings.json +++ b/WatchIt.Website/WatchIt.Website/appsettings.json @@ -27,6 +27,7 @@ "GetAccountProfileBackground": "/{0}/profile_background", "PutAccountProfileBackground": "/profile_background", "DeleteAccountProfileBackground": "/profile_background", + "GetAccounts": "", "GetAccount": "/{0}", "PutAccountProfileInfo": "/profile_info", "PatchAccountUsername": "/username",