From 89dc74f76778c8d348348fa7061c8b9c2d5077d8 Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Sun, 27 Oct 2024 20:22:55 +0100 Subject: [PATCH] client methods for account info endpoints --- .../Accounts/AccountRequest.cs | 2 +- .../Accounts/AccountRequestValidator.cs | 21 ++++++++++ WatchIt.WebAPI/WatchIt.WebAPI/Program.cs | 1 + .../Model/Accounts.cs | 3 ++ .../AccountsWebAPIService.cs | 39 +++++++++++++++++++ .../IAccountsWebAPIService.cs | 3 ++ .../WatchIt.Website/appsettings.json | 5 ++- 7 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 WatchIt.WebAPI/WatchIt.WebAPI.Validators/Accounts/AccountRequestValidator.cs diff --git a/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountRequest.cs b/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountRequest.cs index 37bfeae..30b1d7b 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountRequest.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Accounts/AccountRequest.cs @@ -7,7 +7,7 @@ public class AccountRequest : Account #region PROPERTIES [JsonPropertyName("gender_id")] - public short GenderId { get; set; } + public short? GenderId { get; set; } #endregion diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Accounts/AccountRequestValidator.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Accounts/AccountRequestValidator.cs new file mode 100644 index 0000000..a5b3265 --- /dev/null +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Accounts/AccountRequestValidator.cs @@ -0,0 +1,21 @@ +using FluentValidation; +using WatchIt.Common.Model.Accounts; +using WatchIt.Database; + +namespace WatchIt.WebAPI.Validators.Accounts; + +public class AccountRequestValidator : AbstractValidator +{ + public AccountRequestValidator(DatabaseContext database) + { + RuleFor(x => x.Username).NotEmpty() + .MaximumLength(50); + RuleFor(x => x.Email).EmailAddress() + .MaximumLength(320); + RuleFor(x => x.Description).MaximumLength(1000); + When(x => x.GenderId.HasValue, () => + { + RuleFor(x => x.GenderId!.Value).MustBeIn(database.Genders.Select(x => x.Id)); + }); + } +} \ No newline at end of file diff --git a/WatchIt.WebAPI/WatchIt.WebAPI/Program.cs b/WatchIt.WebAPI/WatchIt.WebAPI/Program.cs index d6c6797..581c7c6 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI/Program.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI/Program.cs @@ -46,6 +46,7 @@ public static class Program while (!dbContext.Database.CanConnect()) { + app.Logger.LogInformation("Waiting for database..."); Thread.Sleep(1000); } diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Accounts.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Accounts.cs index ba1f2db..c8f50a5 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Accounts.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Accounts.cs @@ -8,4 +8,7 @@ public class Accounts public string AuthenticateRefresh { get; set; } public string Logout { get; set; } public string GetProfilePicture { get; set; } + public string GetAccountInfoById { get; set; } + public string GetAccountInfo { get; set; } + public string PutAccountInfo { get; set; } } \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Accounts/AccountsWebAPIService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Accounts/AccountsWebAPIService.cs index f56757e..5e1c092 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Accounts/AccountsWebAPIService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Accounts/AccountsWebAPIService.cs @@ -80,6 +80,45 @@ public class AccountsWebAPIService(IHttpClientService httpClientService, IConfig .ExecuteAction(); } + public async Task GetAccountInfoById(long id, Action? successAction = null, Action? notFoundAction = null) + { + string url = GetUrl(EndpointsConfiguration.Accounts.GetAccountInfoById, id); + HttpRequest request = new HttpRequest(HttpMethodType.Get, url); + + HttpResponse response = await httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor404NotFound(notFoundAction) + .ExecuteAction(); + } + + public async Task GetAccountInfo(Action? successAction = null, Action? unauthorizedAction = null, Action? notFoundAction = null) + { + string url = GetUrl(EndpointsConfiguration.Accounts.GetAccountInfo); + HttpRequest request = new HttpRequest(HttpMethodType.Get, url); + + HttpResponse response = await httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor401Unauthorized(unauthorizedAction) + .RegisterActionFor404NotFound(notFoundAction) + .ExecuteAction(); + } + + public async Task PutAccountInfo(AccountRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null, Action? notFoundAction = null) + { + string url = GetUrl(EndpointsConfiguration.Accounts.PutAccountInfo); + HttpRequest request = new HttpRequest(HttpMethodType.Put, url) + { + Body = data, + }; + + HttpResponse response = await httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor400BadRequest(badRequestAction) + .RegisterActionFor401Unauthorized(unauthorizedAction) + .RegisterActionFor404NotFound(notFoundAction) + .ExecuteAction(); + } + #endregion diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Accounts/IAccountsWebAPIService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Accounts/IAccountsWebAPIService.cs index a2e0df5..8aed63e 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Accounts/IAccountsWebAPIService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Accounts/IAccountsWebAPIService.cs @@ -9,4 +9,7 @@ public interface IAccountsWebAPIService Task AuthenticateRefresh(Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null); Task Logout(Action? successAction = null); Task GetAccountProfilePicture(long id, Action? successAction = null, Action>? badRequestAction = null, Action? notFoundAction = null); + Task GetAccountInfoById(long id, Action? successAction = null, Action? notFoundAction = null); + Task GetAccountInfo(Action? successAction = null, Action? unauthorizedAction = null, Action? notFoundAction = null); + Task PutAccountInfo(AccountRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null, Action? notFoundAction = null); } \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website/appsettings.json b/WatchIt.Website/WatchIt.Website/appsettings.json index 2e65e00..3613545 100644 --- a/WatchIt.Website/WatchIt.Website/appsettings.json +++ b/WatchIt.Website/WatchIt.Website/appsettings.json @@ -21,7 +21,10 @@ "Authenticate": "/authenticate", "AuthenticateRefresh": "/authenticate-refresh", "Logout": "/logout", - "GetProfilePicture": "/{0}/profile-picture" + "GetProfilePicture": "/{0}/profile-picture", + "GetAccountInfoById": "/{0}/info", + "GetAccountInfo": "/info", + "PutAccountInfo": "/info" }, "Genders": { "Base": "/genders",