profile editing finished

This commit is contained in:
2024-11-05 20:04:15 +01:00
Unverified
parent 314fceb120
commit 26a5e1e558
17 changed files with 475 additions and 16 deletions

View File

@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc;
using WatchIt.Common.Model.Accounts;
using WatchIt.Common.Model.Movies;
using WatchIt.Common.Model.Persons;
using WatchIt.Common.Model.Photos;
using WatchIt.Common.Model.Series;
using WatchIt.WebAPI.Services.Controllers.Accounts;
@@ -14,6 +15,8 @@ namespace WatchIt.WebAPI.Controllers;
[Route("accounts")]
public class AccountsController(IAccountsControllerService accountsControllerService) : ControllerBase
{
#region Basic
[HttpPost("register")]
[AllowAnonymous]
[ProducesResponseType(typeof(RegisterResponse), StatusCodes.Status201Created)]
@@ -39,6 +42,10 @@ public class AccountsController(IAccountsControllerService accountsControllerSer
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
public async Task<ActionResult> Logout() => await accountsControllerService.Logout();
#endregion
#region Profile picture
[HttpGet("{id}/profile_picture")]
[AllowAnonymous]
[ProducesResponseType(typeof(AccountProfilePictureResponse), StatusCodes.Status200OK)]
@@ -59,6 +66,32 @@ public class AccountsController(IAccountsControllerService accountsControllerSer
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> DeleteAccountProfilePicture() => await accountsControllerService.DeleteAccountProfilePicture();
#endregion
#region Profile background
[HttpGet("{id}/profile_background")]
[AllowAnonymous]
[ProducesResponseType(typeof(PhotoResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> GetAccountProfileBackground([FromRoute(Name = "id")]long id) => await accountsControllerService.GetAccountProfileBackground(id);
[HttpPut("profile_background")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ProducesResponseType(typeof(PhotoResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> PutAccountProfileBackground([FromBody]AccountProfileBackgroundRequest body) => await accountsControllerService.PutAccountProfileBackground(body);
[HttpDelete("profile_background")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> DeleteAccountProfileBackground() => await accountsControllerService.DeleteAccountProfileBackground();
#endregion
[HttpGet("{id}/info")]
[AllowAnonymous]
[ProducesResponseType(typeof(AccountResponse), StatusCodes.Status200OK)]

View File

@@ -8,6 +8,7 @@ using WatchIt.Common.Model.Accounts;
using WatchIt.Common.Model.Media;
using WatchIt.Common.Model.Movies;
using WatchIt.Common.Model.Persons;
using WatchIt.Common.Model.Photos;
using WatchIt.Common.Model.Series;
using WatchIt.Database;
using WatchIt.Database.Model.Account;
@@ -32,6 +33,8 @@ public class AccountsControllerService(
{
#region PUBLIC METHODS
#region Basic
public async Task<RequestResult> Register(RegisterRequest data)
{
string leftSalt = StringExtensions.CreateRandom(20);
@@ -125,6 +128,10 @@ public class AccountsControllerService(
return RequestResult.NoContent();
}
#endregion
#region Profile picture
public async Task<RequestResult> GetAccountProfilePicture(long id)
{
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
@@ -185,6 +192,55 @@ public class AccountsControllerService(
return RequestResult.NoContent();
}
#endregion
#region Profile background
public async Task<RequestResult> GetAccountProfileBackground(long id)
{
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
if (account is null)
{
return RequestResult.BadRequest()
.AddValidationError("id", "Account with this id does not exists");
}
if (account.BackgroundPicture is null)
{
return RequestResult.NotFound();
}
PhotoResponse response = new PhotoResponse(account.BackgroundPicture);
return RequestResult.Ok(response);
}
public async Task<RequestResult> PutAccountProfileBackground(AccountProfileBackgroundRequest data)
{
Account account = await database.Accounts.FirstAsync(x => x.Id == userService.GetUserId());
account.BackgroundPictureId = data.Id;
await database.SaveChangesAsync();
PhotoResponse returnData = new PhotoResponse(account.BackgroundPicture!);
return RequestResult.Ok(returnData);
}
public async Task<RequestResult> DeleteAccountProfileBackground()
{
Account account = await database.Accounts.FirstAsync(x => x.Id == userService.GetUserId());
if (account.BackgroundPicture is not null)
{
account.BackgroundPictureId = null;
await database.SaveChangesAsync();
}
return RequestResult.NoContent();
}
#endregion
public async Task<RequestResult> GetAccountInfo(long id)
{
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);

View File

@@ -16,6 +16,9 @@ public interface IAccountsControllerService
Task<RequestResult> GetAccountProfilePicture(long id);
Task<RequestResult> PutAccountProfilePicture(AccountProfilePictureRequest data);
Task<RequestResult> DeleteAccountProfilePicture();
Task<RequestResult> GetAccountProfileBackground(long id);
Task<RequestResult> PutAccountProfileBackground(AccountProfileBackgroundRequest data);
Task<RequestResult> DeleteAccountProfileBackground();
Task<RequestResult> GetAccountInfo(long id);
Task<RequestResult> PutAccountProfileInfo(AccountProfileInfoRequest data);
Task<RequestResult> GetAccountRatedMovies(long id, MovieRatedQueryParameters query);

View File

@@ -0,0 +1,14 @@
using FluentValidation;
using WatchIt.Common.Model.Accounts;
using WatchIt.Database;
namespace WatchIt.WebAPI.Validators.Accounts;
public class AccountProfileBackgroundRequestValidator : AbstractValidator<AccountProfileBackgroundRequest>
{
public AccountProfileBackgroundRequestValidator(DatabaseContext database)
{
RuleFor(x => x.Id).MustBeIn(database.MediaPhotoImages.Where(x => x.MediaPhotoImageBackground != null), x => x.Id)
.WithMessage("Image has to be background");
}
}