username change panel added

This commit is contained in:
2024-11-06 00:11:45 +01:00
Unverified
parent 26a5e1e558
commit 4cbc44f9be
21 changed files with 305 additions and 42 deletions

View File

@@ -92,6 +92,8 @@ public class AccountsController(IAccountsControllerService accountsControllerSer
#endregion
#region Info
[HttpGet("{id}/info")]
[AllowAnonymous]
[ProducesResponseType(typeof(AccountResponse), StatusCodes.Status200OK)]
@@ -105,6 +107,15 @@ public class AccountsController(IAccountsControllerService accountsControllerSer
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> PutAccountProfileInfo([FromBody]AccountProfileInfoRequest data) => await accountsControllerService.PutAccountProfileInfo(data);
[HttpPatch("username")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> PatchAccountUsername([FromBody]AccountUsernameRequest data) => await accountsControllerService.PatchAccountUsername(data);
#endregion
[HttpGet("{id}/movies")]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<MovieRatedResponse>), StatusCodes.Status200OK)]

View File

@@ -240,7 +240,9 @@ public class AccountsControllerService(
}
#endregion
#region Info
public async Task<RequestResult> GetAccountInfo(long id)
{
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
@@ -266,6 +268,23 @@ public class AccountsControllerService(
return RequestResult.Ok();
}
public async Task<RequestResult> PatchAccountUsername(AccountUsernameRequest data)
{
Account account = await database.Accounts.FirstAsync(x => x.Id == userService.GetUserId());
if (!ComputeHash(data.Password, account.LeftSalt, account.RightSalt).SequenceEqual(account.Password))
{
return RequestResult.Unauthorized();
}
data.UpdateAccount(account);
await database.SaveChangesAsync();
return RequestResult.Ok();
}
#endregion
public async Task<RequestResult> GetAccountRatedMovies(long id, MovieRatedQueryParameters query)
{

View File

@@ -21,6 +21,7 @@ public interface IAccountsControllerService
Task<RequestResult> DeleteAccountProfileBackground();
Task<RequestResult> GetAccountInfo(long id);
Task<RequestResult> PutAccountProfileInfo(AccountProfileInfoRequest data);
Task<RequestResult> PatchAccountUsername(AccountUsernameRequest data);
Task<RequestResult> GetAccountRatedMovies(long id, MovieRatedQueryParameters query);
Task<RequestResult> GetAccountRatedSeries(long id, SeriesRatedQueryParameters query);
Task<RequestResult> GetAccountRatedPersons(long id, PersonRatedQueryParameters query);

View File

@@ -0,0 +1,16 @@
using FluentValidation;
using WatchIt.Common.Model.Accounts;
using WatchIt.Database;
namespace WatchIt.WebAPI.Validators.Accounts;
public class AccountUsernameRequestValidator : AbstractValidator<AccountUsernameRequest>
{
public AccountUsernameRequestValidator(DatabaseContext database)
{
RuleFor(x => x.NewUsername).MinimumLength(5)
.MaximumLength(50)
.CannotBeIn(database.Accounts, x => x.Username)
.WithMessage("Username is already used");
}
}