Refactoring, database structure changed
This commit is contained in:
353
WatchIt.WebAPI/BusinessLogic/Accounts/AccountsBusinessLogic.cs
Normal file
353
WatchIt.WebAPI/BusinessLogic/Accounts/AccountsBusinessLogic.cs
Normal file
@@ -0,0 +1,353 @@
|
||||
using Ardalis.Result;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.Database.Model.People;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.Account;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountBackgroundPicture;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountEmail;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountLogout;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountPassword;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountProfileInfo;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountUsername;
|
||||
using WatchIt.DTO.Models.Controllers.Media;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Response;
|
||||
using WatchIt.DTO.Models.Controllers.People;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Photos;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.Helpers;
|
||||
using WatchIt.WebAPI.Repositories.Accounts;
|
||||
using WatchIt.WebAPI.Repositories.Media;
|
||||
using WatchIt.WebAPI.Repositories.People;
|
||||
using WatchIt.WebAPI.Services.Tokens;
|
||||
using WatchIt.WebAPI.Services.User;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Accounts;
|
||||
|
||||
public class AccountsBusinessLogic : IAccountsBusinessLogic
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IUserService _userService;
|
||||
private readonly ITokensService _tokensService;
|
||||
private readonly IAccountsRepository _accountRepository;
|
||||
private readonly IMediaRepository _mediaRepository;
|
||||
private readonly IPeopleRepository _peopleRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public AccountsBusinessLogic(IUserService userService, ITokensService tokensService, IAccountsRepository accountRepository, IMediaRepository mediaRepository, IPeopleRepository peopleRepository)
|
||||
{
|
||||
_userService = userService;
|
||||
_tokensService = tokensService;
|
||||
_accountRepository = accountRepository;
|
||||
_mediaRepository = mediaRepository;
|
||||
_peopleRepository = peopleRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<Result<IEnumerable<AccountResponse>>> GetAccounts(AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includeProfilePictures)
|
||||
{
|
||||
IEnumerable<Account> entities = await _accountRepository.GetAllAsync(x => IncludeForAccountResponse(x, includeProfilePictures));
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<AccountResponse>> GetAccount(long accountId, bool includeProfilePictures)
|
||||
{
|
||||
Account? account = await _accountRepository.GetAsync(accountId, x => IncludeForAccountResponse(x, includeProfilePictures));
|
||||
return account switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(account.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<AccountResponse>> PostAccount(AccountRequest body)
|
||||
{
|
||||
Account entity = body.ToEntity(PasswordHelpers.GeneratePasswordData);
|
||||
await _accountRepository.AddAsync(entity);
|
||||
AccountResponse response = entity.ToResponse();
|
||||
return Result<AccountResponse>.Created(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile picture
|
||||
|
||||
public async Task<Result<ImageResponse>> GetAccountProfilePicture(long accountId)
|
||||
{
|
||||
AccountProfilePicture? picture = await _accountRepository.GetProfilePictureAsync(accountId);
|
||||
return picture switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(picture.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<ImageResponse>> PutAccountProfilePicture(ImageRequest body)
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
AccountProfilePicture entity = await _accountRepository.UpdateOrAddProfilePictureAsync(account.Id, () => AccountsMappers.ToEntity(body, account.Id), x => x.UpdateWithRequest(body));
|
||||
return Result.Success(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteAccountProfilePicture()
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
await _accountRepository.DeleteProfilePictureAsync(account.Id);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile background
|
||||
|
||||
public async Task<Result<PhotoResponse>> GetAccountBackgroundPicture(long accountId)
|
||||
{
|
||||
AccountBackgroundPicture? picture = await _accountRepository.GetBackgroundPictureAsync(accountId, x => x.Include(y => y.Background)
|
||||
.ThenInclude(y => y.Photo));
|
||||
return picture switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(picture.Background.Photo.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<PhotoResponse>> PutAccountBackgroundPicture(AccountBackgroundPictureRequest body)
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
AccountBackgroundPicture entity = await _accountRepository.UpdateOrAddBackgroundPictureAsync(account.Id, () => body.ToEntity(account.Id), x => x.UpdateWithRequest(body));
|
||||
PhotoResponse photo = await GetAccountBackgroundPicture(account.Id);
|
||||
return Result.Success(photo);
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteAccountBackgroundPicture()
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
await _accountRepository.DeleteBackgroundPictureAsync(account.Id);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile edit
|
||||
|
||||
public async Task<Result> PatchAccountProfileInfo(AccountProfileInfoRequest body)
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
await _accountRepository.UpdateAsync(account, x => x.UpdateWithRequest(body));
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result> PatchAccountUsername(AccountUsernameRequest body)
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
if (!PasswordHelpers.ValidatePassword(body.Password, account.GetPasswordData()))
|
||||
{
|
||||
return Result.Unauthorized();
|
||||
}
|
||||
await _accountRepository.UpdateAsync(account, x => x.UpdateWithRequest(body));
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result> PatchAccountEmail(AccountEmailRequest body)
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
if (!PasswordHelpers.ValidatePassword(body.Password, account.GetPasswordData()))
|
||||
{
|
||||
return Result.Unauthorized();
|
||||
}
|
||||
await _accountRepository.UpdateAsync(account, x => x.UpdateWithRequest(body));
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result> PatchAccountPassword(AccountPasswordRequest body)
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
if (!PasswordHelpers.ValidatePassword(body.Password, account.GetPasswordData()))
|
||||
{
|
||||
return Result.Unauthorized();
|
||||
}
|
||||
await _accountRepository.UpdateAsync(account, x => x.UpdateWithRequest(body, PasswordHelpers.GeneratePasswordData));
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Log out
|
||||
|
||||
public async Task<Result> Logout(AccountLogoutRequest body)
|
||||
{
|
||||
if (body.RefreshToken is not null)
|
||||
{
|
||||
await _tokensService.RevokeRefreshTokenAsync(body.RefreshToken);
|
||||
}
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
public async Task<Result> LogoutAll()
|
||||
{
|
||||
Account accountEntity = await _userService.GetAccountAsync();
|
||||
await _tokensService.RevokeAccountRefreshTokensAsync(accountEntity);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Follows
|
||||
|
||||
public async Task<Result<IEnumerable<AccountResponse>>> GetAccountFollows(long accountId, AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
if (!await _accountRepository.ExistsAsync(accountId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
IEnumerable<Account> accounts = await _accountRepository.GetFollowsAsync(accountId, filterQuery, orderQuery, pagingQuery, x => x.Include(y => y.Gender));
|
||||
return Result.Success(accounts.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<IEnumerable<AccountResponse>>> GetAccountFollowers(long accountId, AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
if (!await _accountRepository.ExistsAsync(accountId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
IEnumerable<Account> accounts = await _accountRepository.GetFollowersAsync(accountId, filterQuery, orderQuery, pagingQuery, x => x.Include(y => y.Gender));
|
||||
return Result.Success(accounts.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result> PostAccountFollow(long followedAccountId)
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
if (account.Id == followedAccountId)
|
||||
{
|
||||
return Result.Error("You cannot follow yourself");
|
||||
}
|
||||
if (!await _accountRepository.ExistsAsync(followedAccountId))
|
||||
{
|
||||
return Result.Error("User with this id doesn't exist");
|
||||
}
|
||||
if (!await _accountRepository.FollowExistsAsync(account.Id, followedAccountId))
|
||||
{
|
||||
await _accountRepository.AddFollowAsync(AccountsMappers.CreateAccountFollowEntity(account.Id, followedAccountId));
|
||||
}
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteAccountFollow(long followedAccountId)
|
||||
{
|
||||
Account account = await _userService.GetAccountAsync();
|
||||
await _accountRepository.DeleteFollowAsync(account.Id, followedAccountId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ratings
|
||||
|
||||
public async Task<Result<IEnumerable<MediumUserRatedResponse>>> GetAccountRatedMedia(long accountId, MediumFilterQuery filterQuery, MediumUserRatedFilterQuery<Medium> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures)
|
||||
{
|
||||
if (!await _accountRepository.ExistsAsync(accountId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<Medium> media = await _mediaRepository.GetAllRatedByAccountAsync(accountId, filterQuery, userRatedFilterQuery, orderQuery, pagingQuery, x => IncludeForMediumResponse(x, includePictures));
|
||||
return Result.Success(media.Select(x => x.ToResponse(accountId)));
|
||||
}
|
||||
|
||||
public async Task<Result<IEnumerable<MediumMovieUserRatedResponse>>> GetAccountRatedMediaMovies(long accountId, MediumMovieFilterQuery filterQuery, MediumUserRatedFilterQuery<MediumMovie> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures)
|
||||
{
|
||||
if (!await _accountRepository.ExistsAsync(accountId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<MediumMovie> mediumMovies = await _mediaRepository.GetAllMoviesRatedByAccountAsync(accountId, filterQuery, userRatedFilterQuery, orderQuery, pagingQuery, x => IncludeForMediumResponse(x, includePictures));
|
||||
return Result.Success(mediumMovies.Select(x => x.ToResponse(accountId)));
|
||||
}
|
||||
|
||||
public async Task<Result<IEnumerable<MediumSeriesUserRatedResponse>>> GetAccountRatedMediaSeries(long accountId, MediumSeriesFilterQuery filterQuery, MediumUserRatedFilterQuery<MediumSeries> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures)
|
||||
{
|
||||
if (!await _accountRepository.ExistsAsync(accountId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<MediumSeries> mediumSeries = await _mediaRepository.GetAllSeriesRatedByAccountAsync(accountId, filterQuery, userRatedFilterQuery, orderQuery, pagingQuery, x => IncludeForMediumResponse(x, includePictures));
|
||||
return Result.Success(mediumSeries.Select(x => x.ToResponse(accountId)));
|
||||
}
|
||||
|
||||
public async Task<Result<IEnumerable<PersonUserRatedResponse>>> GetAccountRatedPeople(long accountId, PersonFilterQuery filterQuery, PersonUserRatedFilterQuery userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures)
|
||||
{
|
||||
if (!await _accountRepository.ExistsAsync(accountId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<Person> people = await _peopleRepository.GetAllRatedByAccountAsync(accountId, filterQuery, userRatedFilterQuery, orderQuery, pagingQuery, x => IncludeForPersonResponse(x, includePictures));
|
||||
return Result.Success(people.Select(x => x.ToResponse(accountId)));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private IQueryable<Account> IncludeForAccountResponse(IQueryable<Account> query, bool includeProfilePictures)
|
||||
{
|
||||
query = query.Include(y => y.Gender);
|
||||
if (includeProfilePictures)
|
||||
{
|
||||
query = query.Include(y => y.ProfilePicture);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
private IQueryable<T> IncludeForMediumResponse<T>(IQueryable<T> query, bool includePictures) where T : Medium
|
||||
{
|
||||
query = query.Include(y => y.Genres)
|
||||
.Include(y => y.Ratings)
|
||||
.Include(y => y.ViewCounts);
|
||||
if (includePictures)
|
||||
{
|
||||
query = query.Include(y => y.Picture);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
private IQueryable<Person> IncludeForPersonResponse(IQueryable<Person> query, bool includeProfilePictures)
|
||||
{
|
||||
query = query.Include(y => y.Gender)
|
||||
.Include(y => y.Roles)
|
||||
.ThenInclude(y => y.Ratings)
|
||||
.Include(y => y.ViewCounts);
|
||||
if (includeProfilePictures)
|
||||
{
|
||||
query = query.Include(y => y.Picture);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.Account;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountBackgroundPicture;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountEmail;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountLogout;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountPassword;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountProfileInfo;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountUsername;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Response;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Accounts;
|
||||
|
||||
public interface IAccountsBusinessLogic
|
||||
{
|
||||
#region Main
|
||||
|
||||
Task<Result<IEnumerable<AccountResponse>>> GetAccounts(AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includeProfilePictures);
|
||||
Task<Result<AccountResponse>> GetAccount(long accountId, bool includeProfilePictures);
|
||||
Task<Result<AccountResponse>> PostAccount(AccountRequest body);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile picture
|
||||
|
||||
Task<Result<ImageResponse>> GetAccountProfilePicture(long accountId);
|
||||
Task<Result<ImageResponse>> PutAccountProfilePicture(ImageRequest body);
|
||||
Task<Result> DeleteAccountProfilePicture();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Background picture
|
||||
|
||||
Task<Result<PhotoResponse>> GetAccountBackgroundPicture(long accountId);
|
||||
Task<Result<PhotoResponse>> PutAccountBackgroundPicture(AccountBackgroundPictureRequest body);
|
||||
Task<Result> DeleteAccountBackgroundPicture();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile edit
|
||||
|
||||
Task<Result> PatchAccountProfileInfo(AccountProfileInfoRequest body);
|
||||
Task<Result> PatchAccountUsername(AccountUsernameRequest body);
|
||||
Task<Result> PatchAccountEmail(AccountEmailRequest body);
|
||||
Task<Result> PatchAccountPassword(AccountPasswordRequest body);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Log out
|
||||
|
||||
Task<Result> Logout(AccountLogoutRequest body);
|
||||
Task<Result> LogoutAll();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Follows
|
||||
|
||||
Task<Result<IEnumerable<AccountResponse>>> GetAccountFollows(long accountId, AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result<IEnumerable<AccountResponse>>> GetAccountFollowers(long accountId, AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result> PostAccountFollow(long followedAccountId);
|
||||
Task<Result> DeleteAccountFollow(long followedAccountId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Media
|
||||
|
||||
Task<Result<IEnumerable<MediumUserRatedResponse>>> GetAccountRatedMedia(long accountId, MediumFilterQuery filterQuery, MediumUserRatedFilterQuery<Medium> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures);
|
||||
Task<Result<IEnumerable<MediumMovieUserRatedResponse>>> GetAccountRatedMediaMovies(long accountId, MediumMovieFilterQuery filterQuery, MediumUserRatedFilterQuery<MediumMovie> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures);
|
||||
Task<Result<IEnumerable<MediumSeriesUserRatedResponse>>> GetAccountRatedMediaSeries(long accountId, MediumSeriesFilterQuery filterQuery, MediumUserRatedFilterQuery<MediumSeries> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures);
|
||||
Task<Result<IEnumerable<PersonUserRatedResponse>>> GetAccountRatedPeople(long accountId, PersonFilterQuery filterQuery, PersonUserRatedFilterQuery userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts;
|
||||
using WatchIt.DTO.Models.Controllers.Authentication;
|
||||
using WatchIt.WebAPI.Helpers;
|
||||
using WatchIt.WebAPI.Repositories.Accounts;
|
||||
using WatchIt.WebAPI.Services.Tokens;
|
||||
using WatchIt.WebAPI.Services.User;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Authentication;
|
||||
|
||||
public class AuthenticationBusinessLogic : IAuthenticationBusinessLogic
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly ITokensService _tokensService;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IAccountsRepository _accountRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public AuthenticationBusinessLogic(ITokensService tokensService, IUserService userService, IAccountsRepository accountRepository)
|
||||
{
|
||||
_tokensService = tokensService;
|
||||
_userService = userService;
|
||||
_accountRepository = accountRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<Result<AuthenticationResponse>> Authenticate(AuthenticationRequest body)
|
||||
{
|
||||
Account? accountEntity = await _accountRepository.GetByUsernameOrEmailAsync(body.UsernameOrEmail);
|
||||
if (accountEntity is null || !PasswordHelpers.ValidatePassword(body.Password, accountEntity.GetPasswordData()))
|
||||
{
|
||||
return Result.Unauthorized();
|
||||
}
|
||||
|
||||
string accessToken = _tokensService.CreateAccessToken(accountEntity);
|
||||
string refreshToken = await _tokensService.CreateRefreshTokenAsync(accountEntity, body.RememberMe);
|
||||
AuthenticationResponse response = AuthenticationMappers.CreateAuthenticationResponse(accessToken, refreshToken);
|
||||
|
||||
await _accountRepository.UpdateAsync(accountEntity, x => x.UpdateActiveDate());
|
||||
|
||||
return Result.Success(response);
|
||||
}
|
||||
|
||||
public async Task<Result<AuthenticationResponse>> AuthenticateRefresh(AuthenticationRefreshRequest body)
|
||||
{
|
||||
Account accountEntity = await _tokensService.ExtendRefreshTokenAsync(body.RefreshToken, body.AccessToken);
|
||||
string accessToken = _tokensService.CreateAccessToken(accountEntity);
|
||||
AuthenticationResponse response = AuthenticationMappers.CreateAuthenticationResponse(accessToken, body.RefreshToken);
|
||||
|
||||
await _accountRepository.UpdateAsync(accountEntity, x => x.UpdateActiveDate());
|
||||
|
||||
return Result.Success(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.Account;
|
||||
using WatchIt.DTO.Models.Controllers.Authentication;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Authentication;
|
||||
|
||||
public interface IAuthenticationBusinessLogic
|
||||
{
|
||||
Task<Result<AuthenticationResponse>> Authenticate(AuthenticationRequest body);
|
||||
Task<Result<AuthenticationResponse>> AuthenticateRefresh(AuthenticationRefreshRequest body);
|
||||
}
|
||||
68
WatchIt.WebAPI/BusinessLogic/Genders/GendersBusinessLogic.cs
Normal file
68
WatchIt.WebAPI/BusinessLogic/Genders/GendersBusinessLogic.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Ardalis.Result;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database.Model.Genders;
|
||||
using WatchIt.DTO;
|
||||
using WatchIt.DTO.Models.Controllers.Genders;
|
||||
using WatchIt.DTO.Models.Controllers.Genders.Gender;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.Repositories.Genders;
|
||||
using WatchIt.WebAPI.Services.User;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Genders;
|
||||
|
||||
public class GendersBusinessLogic : IGendersBusinessLogic
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IUserService _userService;
|
||||
private readonly IGendersRepository _gendersRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public GendersBusinessLogic(IUserService userService, IGendersRepository gendersRepository)
|
||||
{
|
||||
_userService = userService;
|
||||
_gendersRepository = gendersRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<Result<IEnumerable<GenderResponse>>> GetGenders(GenderFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
IEnumerable<Gender> entities = await _gendersRepository.GetAllAsync(filterQuery, orderQuery, pagingQuery);
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<GenderResponse>> GetGender(short genderId)
|
||||
{
|
||||
Gender? entity = await _gendersRepository.GetAsync(genderId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<GenderResponse>> PostGender(GenderRequest body)
|
||||
{
|
||||
Gender entity = body.ToEntity();
|
||||
await _gendersRepository.AddAsync(entity);
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteGender(short genderId)
|
||||
{
|
||||
await _gendersRepository.DeleteAsync(x => x.Id == genderId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.DTO.Models.Controllers.Genders.Gender;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Genders;
|
||||
|
||||
public interface IGendersBusinessLogic
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
Task<Result<IEnumerable<GenderResponse>>> GetGenders(GenderFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result<GenderResponse>> GetGender(short genderId);
|
||||
Task<Result<GenderResponse>> PostGender(GenderRequest body);
|
||||
Task<Result> DeleteGender(short genderId);
|
||||
|
||||
#endregion
|
||||
}
|
||||
67
WatchIt.WebAPI/BusinessLogic/Genres/GenresBusinessLogic.cs
Normal file
67
WatchIt.WebAPI/BusinessLogic/Genres/GenresBusinessLogic.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.Database.Model.Genres;
|
||||
using WatchIt.DTO;
|
||||
using WatchIt.DTO.Models.Controllers.Genres;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.Repositories.Genres;
|
||||
using WatchIt.WebAPI.Services.User;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Genres;
|
||||
|
||||
public class GenresBusinessLogic : IGenresBusinessLogic
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IUserService _userService;
|
||||
private readonly IGenresRepository _genresRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public GenresBusinessLogic(IUserService userService, IGenresRepository genresRepository)
|
||||
{
|
||||
_userService = userService;
|
||||
_genresRepository = genresRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<Result<IEnumerable<GenreResponse>>> GetGenres(GenreFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
IEnumerable<Genre> entities = await _genresRepository.GetAllAsync(filterQuery, orderQuery, pagingQuery);
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<GenreResponse>> GetGenre(short genreId)
|
||||
{
|
||||
Genre? entity = await _genresRepository.GetAsync(genreId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<GenreResponse>> PostGenre(GenreRequest body)
|
||||
{
|
||||
Genre entity = body.ToEntity();
|
||||
await _genresRepository.AddAsync(body.ToEntity());
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteGenre(short genreId)
|
||||
{
|
||||
await _genresRepository.DeleteAsync(x => x.Id == genreId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
13
WatchIt.WebAPI/BusinessLogic/Genres/IGenresBusinessLogic.cs
Normal file
13
WatchIt.WebAPI/BusinessLogic/Genres/IGenresBusinessLogic.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Genres;
|
||||
|
||||
public interface IGenresBusinessLogic
|
||||
{
|
||||
Task<Result<IEnumerable<GenreResponse>>> GetGenres(GenreFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result<GenreResponse>> GetGenre(short genreId);
|
||||
Task<Result<GenreResponse>> PostGenre(GenreRequest body);
|
||||
Task<Result> DeleteGenre(short genreId);
|
||||
}
|
||||
68
WatchIt.WebAPI/BusinessLogic/Media/IMediaBusinessLogic.cs
Normal file
68
WatchIt.WebAPI/BusinessLogic/Media/IMediaBusinessLogic.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Request;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Response;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Response;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Models.Generics.Rating;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Media;
|
||||
|
||||
public interface IMediaBusinessLogic
|
||||
{
|
||||
#region Main
|
||||
|
||||
Task<Result<IEnumerable<MediumResponse>>> GetMedia(MediumFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures);
|
||||
Task<Result<MediumResponse>> GetMedium(long mediumId, bool includePictures);
|
||||
Task<Result<IEnumerable<MediumMovieResponse>>> GetMediumMovies(MediumMovieFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures);
|
||||
Task<Result<MediumMovieResponse>> GetMediumMovie(long mediumId, bool includePictures);
|
||||
Task<Result<IEnumerable<MediumSeriesResponse>>> GetMediumSeries(MediumSeriesFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures);
|
||||
Task<Result<MediumSeriesResponse>> GetMediumSeries(long mediumId, bool includePictures);
|
||||
Task<Result<MediumMovieResponse>> PostMediumMovie(MediumMovieRequest body);
|
||||
Task<Result<MediumSeriesResponse>> PostMediumSeries(MediumSeriesRequest body);
|
||||
Task<Result<MediumMovieResponse>> PutMediumMovie(long mediumId, MediumMovieRequest body);
|
||||
Task<Result<MediumSeriesResponse>> PutMediumSeries(long mediumId, MediumSeriesRequest body);
|
||||
Task<Result> DeleteMedium(long mediumId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Genres
|
||||
|
||||
Task<Result<IEnumerable<GenreResponse>>> GetMediumGenres(long mediumId, GenreFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result> PostMediumGenre(long mediumId, short genreId);
|
||||
Task<Result> DeleteMediumGenre(long mediumId, short genreId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
Task<Result<RatingOverallResponse>> GetMediumRating(long mediumId);
|
||||
Task<Result<RatingUserResponse>> GetMediumUserRating(long mediumId, long accountId);
|
||||
Task<Result> PutMediumRating(long mediumId, RatingRequest body);
|
||||
Task<Result> DeleteMediumRating(long mediumId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
Task<Result> PutMediumViewCount(long mediumId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Picture
|
||||
|
||||
Task<Result<ImageResponse>> GetMediumPicture(long mediumId);
|
||||
Task<Result<ImageResponse>> PutMediumPicture(long mediumId, ImageRequest body);
|
||||
Task<Result> DeleteMediumPicture(long mediumId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Photos
|
||||
|
||||
Task<Result<PhotoResponse?>> GetMediumPhotoBackground(long mediumId);
|
||||
|
||||
#endregion
|
||||
}
|
||||
303
WatchIt.WebAPI/BusinessLogic/Media/MediaBusinessLogic.cs
Normal file
303
WatchIt.WebAPI/BusinessLogic/Media/MediaBusinessLogic.cs
Normal file
@@ -0,0 +1,303 @@
|
||||
using Ardalis.Result;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SimpleToolkit.Extensions;
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
using WatchIt.Database.Model.Genres;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.Database.Model.Photos;
|
||||
using WatchIt.Database.Model.Roles;
|
||||
using WatchIt.DTO.Models.Controllers.Genres;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Models.Controllers.Media;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Request;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Response;
|
||||
using WatchIt.DTO.Models.Controllers.Photos;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Models.Controllers.Roles;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Response;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Models.Generics.Rating;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.Repositories.Genres;
|
||||
using WatchIt.WebAPI.Repositories.Media;
|
||||
using WatchIt.WebAPI.Repositories.Photos;
|
||||
using WatchIt.WebAPI.Services.User;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Media;
|
||||
|
||||
public class MediaBusinessLogic : IMediaBusinessLogic
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IUserService _userService;
|
||||
private readonly IMediaRepository _mediaRepository;
|
||||
private readonly IGenresRepository _genresRepository;
|
||||
private readonly IPhotosRepository _photosRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public MediaBusinessLogic(IUserService userService, IMediaRepository mediaRepository, IGenresRepository genresRepository, IPhotosRepository photosRepository)
|
||||
{
|
||||
_userService = userService;
|
||||
_mediaRepository = mediaRepository;
|
||||
_genresRepository = genresRepository;
|
||||
_photosRepository = photosRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<Result<IEnumerable<MediumResponse>>> GetMedia(MediumFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures)
|
||||
{
|
||||
IEnumerable<Medium> entities = await _mediaRepository.GetAllAsync(filterQuery, orderQuery, pagingQuery, x => IncludeForMediumResponse(x, includePictures));
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<MediumResponse>> GetMedium(long mediumId, bool includePictures)
|
||||
{
|
||||
Medium? entity = await _mediaRepository.GetAsync(mediumId, x => IncludeForMediumResponse(x, includePictures));
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<IEnumerable<MediumMovieResponse>>> GetMediumMovies(MediumMovieFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures)
|
||||
{
|
||||
IEnumerable<MediumMovie> entities = await _mediaRepository.GetAllMoviesAsync(filterQuery, orderQuery, pagingQuery, x => IncludeForMediumResponse(x, includePictures));
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<MediumMovieResponse>> GetMediumMovie(long mediumId, bool includePictures)
|
||||
{
|
||||
MediumMovie? entity = await _mediaRepository.GetAsync<MediumMovie>(mediumId, x => IncludeForMediumResponse(x, includePictures));
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse()),
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<IEnumerable<MediumSeriesResponse>>> GetMediumSeries(MediumSeriesFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures)
|
||||
{
|
||||
IEnumerable<MediumSeries> entities = await _mediaRepository.GetAllSeriesAsync(filterQuery, orderQuery, pagingQuery, x => IncludeForMediumResponse(x, includePictures));
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<MediumSeriesResponse>> GetMediumSeries(long mediumId, bool includePictures)
|
||||
{
|
||||
MediumSeries? entity = await _mediaRepository.GetAsync<MediumSeries>(mediumId, x => IncludeForMediumResponse(x, includePictures));
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse()),
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<MediumMovieResponse>> PostMediumMovie(MediumMovieRequest body)
|
||||
{
|
||||
MediumMovie entity = body.ToEntity();
|
||||
await _mediaRepository.AddAsync(entity);
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result<MediumSeriesResponse>> PostMediumSeries(MediumSeriesRequest body)
|
||||
{
|
||||
MediumSeries entity = body.ToEntity();
|
||||
await _mediaRepository.AddAsync(entity);
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result<MediumMovieResponse>> PutMediumMovie(long mediumId, MediumMovieRequest body)
|
||||
{
|
||||
return await _mediaRepository.UpdateAsync<MediumMovie>(mediumId, x => x.UpdateWithRequest(body)) switch
|
||||
{
|
||||
false => Result.NotFound(),
|
||||
true => Result.Success()
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<MediumSeriesResponse>> PutMediumSeries(long mediumId, MediumSeriesRequest body)
|
||||
{
|
||||
return await _mediaRepository.UpdateAsync<MediumSeries>(mediumId, x => x.UpdateWithRequest(body)) switch
|
||||
{
|
||||
false => Result.NotFound(),
|
||||
true => Result.Success()
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteMedium(long mediumId)
|
||||
{
|
||||
await _mediaRepository.DeleteAsync(x => x.Id == mediumId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Genres
|
||||
|
||||
public async Task<Result<IEnumerable<GenreResponse>>> GetMediumGenres(long mediumId, GenreFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
if (!await _mediaRepository.ExistsAsync(mediumId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
IEnumerable<Genre> genres = await _mediaRepository.GetMediumGenresAsync(mediumId, filterQuery, orderQuery, pagingQuery);
|
||||
return Result.Success(genres.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result> PostMediumGenre(long mediumId, short genreId)
|
||||
{
|
||||
bool mediumExists = await _mediaRepository.ExistsAsync(mediumId);
|
||||
bool genreExists = await _genresRepository.ExistsAsync(genreId);
|
||||
if (mediumExists && genreExists)
|
||||
{
|
||||
await _mediaRepository.AddMediumGenreAsync(mediumId, genreId);
|
||||
return Result.Success();
|
||||
}
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteMediumGenre(long mediumId, short genreId)
|
||||
{
|
||||
await _mediaRepository.DeleteMediumGenreAsync(mediumId, genreId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
public async Task<Result<RatingOverallResponse>> GetMediumRating(long mediumId)
|
||||
{
|
||||
Medium? entity = await _mediaRepository.GetAsync(mediumId, x => x.Include(y => y.Ratings));
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.Ratings.ToOverallResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<RatingUserResponse>> GetMediumUserRating(long mediumId, long accountId)
|
||||
{
|
||||
MediumRating? entity = await _mediaRepository.GetMediumUserRatingAsync(mediumId, accountId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToUserResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result> PutMediumRating(long mediumId, RatingRequest body)
|
||||
{
|
||||
Account accountEntity = await _userService.GetAccountAsync();
|
||||
Medium? mediumEntity = await _mediaRepository.GetAsync(mediumId);
|
||||
if (mediumEntity is null)
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
await _mediaRepository.UpdateOrAddMediumRatingAsync(mediumEntity.Id, accountEntity.Id, () => body.ToEntity(mediumEntity.Id, accountEntity.Id), x => x.UpdateWithRequest(body));
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteMediumRating(long mediumId)
|
||||
{
|
||||
Account accountEntity = await _userService.GetAccountAsync();
|
||||
await _mediaRepository.DeleteMediumUserRatingAsync(mediumId, accountEntity.Id);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
public async Task<Result> PutMediumViewCount(long mediumId)
|
||||
{
|
||||
Medium? entity = await _mediaRepository.GetAsync(mediumId);
|
||||
if (entity is null)
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
DateOnly date = DateOnly.FromDateTime(DateTime.UtcNow);
|
||||
await _mediaRepository.UpdateOrAddMediumViewCountAsync(mediumId, date, () => MediaMappers.CreateMediumViewCountEntity(mediumId), x => x.ViewCount++);
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Pictures
|
||||
|
||||
public async Task<Result<ImageResponse>> GetMediumPicture(long mediumId)
|
||||
{
|
||||
MediumPicture? entity = await _mediaRepository.GetMediumPictureAsync(mediumId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<ImageResponse>> PutMediumPicture(long mediumId, ImageRequest body)
|
||||
{
|
||||
return await _mediaRepository.ExistsAsync(mediumId) switch
|
||||
{
|
||||
true => Result.Success((await _mediaRepository.UpdateOrAddMediumPictureAsync(mediumId, () => body.ToEntity(mediumId), x => x.UpdateWithRequest(body))).ToResponse()),
|
||||
false => Result.NotFound(),
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteMediumPicture(long mediumId)
|
||||
{
|
||||
await _mediaRepository.DeleteMediumPictureAsync(mediumId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Photos
|
||||
|
||||
public async Task<Result<PhotoResponse?>> GetMediumPhotoBackground(long mediumId)
|
||||
{
|
||||
if (!await _mediaRepository.ExistsAsync(mediumId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
Photo photo = await _photosRepository.GetPhotoRandomBackgroundByMediumAsync(mediumId, x => x.Include(y => y.Background));
|
||||
return photo.ToResponse();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private IQueryable<T> IncludeForMediumResponse<T>(IQueryable<T> query, bool includePictures) where T : Medium
|
||||
{
|
||||
query = query.Include(y => y.Genres)
|
||||
.Include(y => y.Ratings)
|
||||
.Include(y => y.ViewCounts);
|
||||
if (includePictures)
|
||||
{
|
||||
query = query.Include(y => y.Picture);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
23
WatchIt.WebAPI/BusinessLogic/People/IPeopleBusinessLogic.cs
Normal file
23
WatchIt.WebAPI/BusinessLogic/People/IPeopleBusinessLogic.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person.Query;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Models.Generics.Rating;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.People;
|
||||
|
||||
public interface IPeopleBusinessLogic
|
||||
{
|
||||
Task<Result<IEnumerable<PersonResponse>>> GetPeople(PersonFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures);
|
||||
Task<Result<PersonResponse>> GetPerson(long personId, bool includePictures);
|
||||
Task<Result<PersonResponse>> PostPerson(PersonRequest body);
|
||||
Task<Result<PersonResponse>> PutPerson(long personId, PersonRequest body);
|
||||
Task<Result> DeletePerson(long personId);
|
||||
Task<Result<RatingOverallResponse>> GetPersonRating(long personId);
|
||||
Task<Result<RatingUserOverallResponse>> GetPersonUserRating(long personId, long accountId);
|
||||
Task<Result> PutPeopleViewCount(long personId);
|
||||
Task<Result<ImageResponse>> GetPersonPicture(long personId);
|
||||
Task<Result<ImageResponse>> PutPersonPicture(long personId, ImageRequest body);
|
||||
Task<Result> DeletePersonPicture(long personId);
|
||||
}
|
||||
179
WatchIt.WebAPI/BusinessLogic/People/PeopleBusinessLogic.cs
Normal file
179
WatchIt.WebAPI/BusinessLogic/People/PeopleBusinessLogic.cs
Normal file
@@ -0,0 +1,179 @@
|
||||
using Ardalis.Result;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
using WatchIt.Database.Model.People;
|
||||
using WatchIt.Database.Model.Roles;
|
||||
using WatchIt.DTO;
|
||||
using WatchIt.DTO.Models.Controllers.People;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Roles;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Response;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Models.Generics.Rating;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.Repositories.People;
|
||||
using WatchIt.WebAPI.Repositories.Roles;
|
||||
using WatchIt.WebAPI.Services.User;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.People;
|
||||
|
||||
public class PeopleBusinessLogic : IPeopleBusinessLogic
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IUserService _userService;
|
||||
private readonly IPeopleRepository _peopleRepository;
|
||||
private readonly IRolesRepository _rolesRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public PeopleBusinessLogic(IUserService userService, IPeopleRepository peopleRepository, IRolesRepository rolesRepository)
|
||||
{
|
||||
_userService = userService;
|
||||
_peopleRepository = peopleRepository;
|
||||
_rolesRepository = rolesRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<Result<IEnumerable<PersonResponse>>> GetPeople(PersonFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, bool includePictures)
|
||||
{
|
||||
IEnumerable<Person> entities = await _peopleRepository.GetAllAsync(filterQuery, orderQuery, pagingQuery, x => IncludeForPersonResponse(x, includePictures));
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<PersonResponse>> GetPerson(long personId, bool includePictures)
|
||||
{
|
||||
Person? entity = await _peopleRepository.GetAsync(personId, x => IncludeForPersonResponse(x, includePictures));
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<PersonResponse>> PostPerson(PersonRequest body)
|
||||
{
|
||||
Person entity = body.ToEntity();
|
||||
await _peopleRepository.AddAsync(entity);
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result<PersonResponse>> PutPerson(long personId, PersonRequest body)
|
||||
{
|
||||
return await _peopleRepository.UpdateAsync(personId, x => x.UpdateWithRequest(body)) switch
|
||||
{
|
||||
false => Result.NotFound(),
|
||||
true => Result.Success()
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result> DeletePerson(long personId)
|
||||
{
|
||||
await _peopleRepository.DeleteAsync(personId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
public async Task<Result<RatingOverallResponse>> GetPersonRating(long personId)
|
||||
{
|
||||
if (!await _peopleRepository.ExistsAsync(personId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
IEnumerable<RoleRating> roleRatings = await _rolesRepository.GetPersonRoleRatingsAsync(personId);
|
||||
return Result.Success(roleRatings.ToOverallResponse());
|
||||
}
|
||||
|
||||
public async Task<Result<RatingUserOverallResponse>> GetPersonUserRating(long personId, long accountId)
|
||||
{
|
||||
if (!await _peopleRepository.ExistsAsync(personId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
IEnumerable<RoleRating> roleRatings = await _rolesRepository.GetPersonRoleRatingsByAccountIdAsync(personId, accountId);
|
||||
return Result.Success(roleRatings.ToUserOverallResponse());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
public async Task<Result> PutPeopleViewCount(long personId)
|
||||
{
|
||||
Person? entity = await _peopleRepository.GetAsync(personId);
|
||||
if (entity is null)
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
DateOnly date = DateOnly.FromDateTime(DateTime.UtcNow);
|
||||
await _peopleRepository.UpdateOrAddPersonViewCountAsync(personId, date, () => PeopleMappers.CreatePersonViewCountEntity(personId), x => x.ViewCount++);
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Pictures
|
||||
|
||||
public async Task<Result<ImageResponse>> GetPersonPicture(long personId)
|
||||
{
|
||||
PersonPicture? entity = await _peopleRepository.GetPersonPictureAsync(personId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<ImageResponse>> PutPersonPicture(long personId, ImageRequest body)
|
||||
{
|
||||
return await _peopleRepository.ExistsAsync(personId) switch
|
||||
{
|
||||
true => Result.Success((await _peopleRepository.UpdateOrAddPersonPictureAsync(personId, () => body.ToEntity(personId), x => x.UpdateWithRequest(body))).ToResponse()),
|
||||
false => Result.NotFound(),
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result> DeletePersonPicture(long personId)
|
||||
{
|
||||
await _peopleRepository.DeletePersonPictureAsync(personId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private IQueryable<Person> IncludeForPersonResponse(IQueryable<Person> query, bool includePictures)
|
||||
{
|
||||
query = query.Include(y => y.Gender)
|
||||
.Include(y => y.ViewCounts)
|
||||
.Include(y => y.Roles).ThenInclude(y => y.Ratings);
|
||||
if (includePictures)
|
||||
{
|
||||
query = query.Include(y => y.Picture);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
19
WatchIt.WebAPI/BusinessLogic/Photos/IPhotosBusinessLogic.cs
Normal file
19
WatchIt.WebAPI/BusinessLogic/Photos/IPhotosBusinessLogic.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.PhotoBackground;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Photos;
|
||||
|
||||
public interface IPhotosBusinessLogic
|
||||
{
|
||||
Task<Result<PhotoResponse>> GetPhoto(Guid photoId);
|
||||
Task<Result<IEnumerable<PhotoResponse>>> GetPhotos(PhotoFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result<PhotoResponse>> GetPhotoBackground();
|
||||
Task<Result<PhotoResponse>> PostPhoto(PhotoRequest body);
|
||||
Task<Result<PhotoResponse>> PutPhoto(Guid photoId, PhotoRequest body);
|
||||
Task<Result> DeletePhoto(Guid photoId);
|
||||
|
||||
Task<Result<PhotoBackgroundResponse>> PutPhotoBackground(Guid photoId, PhotoBackgroundRequest body);
|
||||
Task<Result> DeletePhotoBackground(Guid photoId);
|
||||
}
|
||||
122
WatchIt.WebAPI/BusinessLogic/Photos/PhotosBusinessLogic.cs
Normal file
122
WatchIt.WebAPI/BusinessLogic/Photos/PhotosBusinessLogic.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using Ardalis.Result;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database.Model.Photos;
|
||||
using WatchIt.DTO.Models.Controllers.Photos;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.PhotoBackground;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.Repositories.Photos;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Photos;
|
||||
|
||||
public class PhotosBusinessLogic : IPhotosBusinessLogic
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IPhotosRepository _photosRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public PhotosBusinessLogic(IPhotosRepository photosRepository)
|
||||
{
|
||||
_photosRepository = photosRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<Result<PhotoResponse>> GetPhoto(Guid photoId)
|
||||
{
|
||||
Photo? entity = await _photosRepository.GetAsync(photoId, x => x.Include(y => y.Background));
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<IEnumerable<PhotoResponse>>> GetPhotos(PhotoFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
IEnumerable<Photo> entities = await _photosRepository.GetAllAsync(filterQuery, orderQuery, pagingQuery, x => x.Include(y => y.Background));
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<PhotoResponse>> PostPhoto(PhotoRequest body)
|
||||
{
|
||||
Photo entity = body.ToEntity();
|
||||
await _photosRepository.AddAsync(entity);
|
||||
if (body.BackgroundData is not null)
|
||||
{
|
||||
await _photosRepository.AddPhotoBackgroundAsync(body.BackgroundData.ToEntity(entity.Id));
|
||||
}
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result<PhotoResponse>> PutPhoto(Guid photoId, PhotoRequest body)
|
||||
{
|
||||
bool success = await _photosRepository.UpdateAsync(photoId, x => x.UpdateWithRequest(body));
|
||||
if (success)
|
||||
{
|
||||
if (body.BackgroundData is not null)
|
||||
{
|
||||
await _photosRepository.UpdateOrAddPhotoBackgroundAsync(photoId, () => body.BackgroundData.ToEntity(photoId), x => x.UpdateWithRequest(body.BackgroundData));
|
||||
}
|
||||
else
|
||||
{
|
||||
await _photosRepository.DeletePhotoBackgroundAsync(photoId);
|
||||
}
|
||||
return Result.Success();
|
||||
}
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
public async Task<Result> DeletePhoto(Guid photoId)
|
||||
{
|
||||
await _photosRepository.DeleteAsync(photoId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Background
|
||||
|
||||
public async Task<Result<PhotoResponse>> GetPhotoBackground()
|
||||
{
|
||||
Photo? entity = await _photosRepository.GetPhotoRandomBackgroundAsync(x => x.Include(y => y.Background));
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<PhotoBackgroundResponse>> PutPhotoBackground(Guid photoId, PhotoBackgroundRequest body)
|
||||
{
|
||||
if (await _photosRepository.ExistsAsync(photoId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
PhotoBackground entity = await _photosRepository.UpdateOrAddPhotoBackgroundAsync(photoId, () => body.ToEntity(photoId), x => x.UpdateWithRequest(body));
|
||||
return Result.Success(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result> DeletePhotoBackground(Guid photoId)
|
||||
{
|
||||
await _photosRepository.DeletePhotoBackgroundAsync(photoId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
37
WatchIt.WebAPI/BusinessLogic/Roles/IRolesBusinessLogic.cs
Normal file
37
WatchIt.WebAPI/BusinessLogic/Roles/IRolesBusinessLogic.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Ardalis.Result;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Request;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Response;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleActorType;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleCreatorType;
|
||||
using WatchIt.DTO.Models.Generics.Rating;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Roles;
|
||||
|
||||
public interface IRolesBusinessLogic
|
||||
{
|
||||
Task<Result<IEnumerable<RoleActorResponse>>> GetRoleActors(RoleActorFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result<RoleActorResponse>> GetRoleActor(Guid roleId);
|
||||
Task<Result<IEnumerable<RoleCreatorResponse>>> GetRoleCreators(RoleCreatorFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result<RoleCreatorResponse>> GetRoleCreator(Guid roleId);
|
||||
Task<Result<RoleActorResponse>> PostRoleActor(RoleActorRequest body);
|
||||
Task<Result<RoleCreatorResponse>> PostRoleCreator(RoleCreatorRequest body);
|
||||
Task<Result<RoleActorResponse>> PutRoleActor(Guid roleId, RoleActorRequest body);
|
||||
Task<Result<RoleCreatorResponse>> PutRoleCreator(Guid roleId, RoleCreatorRequest body);
|
||||
Task<Result> DeleteRole(Guid roleId);
|
||||
|
||||
Task<Result<RatingOverallResponse>> GetRoleRating(Guid roleId);
|
||||
Task<Result<RatingUserResponse>> GetRoleUserRating(Guid roleId, long accountId);
|
||||
Task<Result> PutRoleRating(Guid roleId, RatingRequest body);
|
||||
Task<Result> DeleteRoleRating(Guid roleId);
|
||||
|
||||
Task<Result<IEnumerable<RoleActorTypeResponse>>> GetRoleActorTypes(RoleActorTypeFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result<RoleActorTypeResponse>> GetRoleActorType(short roleActorTypeId);
|
||||
Task<Result<RoleActorTypeResponse>> PostRoleActorType(RoleActorTypeRequest body);
|
||||
Task<Result> DeleteRoleActorType(short roleActorTypeId);
|
||||
Task<Result<IEnumerable<RoleCreatorTypeResponse>>> GetRoleCreatorTypes(RoleCreatorTypeFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery);
|
||||
Task<Result<RoleCreatorTypeResponse>> GetRoleCreatorType(short roleCreatorTypeId);
|
||||
Task<Result<RoleCreatorTypeResponse>> PostRoleCreatorType(RoleCreatorTypeRequest body);
|
||||
Task<Result> DeleteRoleCreatorType(short roleCreatorTypeId);
|
||||
}
|
||||
230
WatchIt.WebAPI/BusinessLogic/Roles/RolesBusinessLogic.cs
Normal file
230
WatchIt.WebAPI/BusinessLogic/Roles/RolesBusinessLogic.cs
Normal file
@@ -0,0 +1,230 @@
|
||||
using Ardalis.Result;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
using WatchIt.Database.Model.Roles;
|
||||
using WatchIt.DTO;
|
||||
using WatchIt.DTO.Models.Controllers.Roles;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Request;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Response;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleActorType;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleCreatorType;
|
||||
using WatchIt.DTO.Models.Generics.Rating;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.Repositories.Roles;
|
||||
using WatchIt.WebAPI.Services.User;
|
||||
|
||||
namespace WatchIt.WebAPI.BusinessLogic.Roles;
|
||||
|
||||
public class RolesBusinessLogic : IRolesBusinessLogic
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IUserService _userService;
|
||||
private readonly IRolesRepository _rolesRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RolesBusinessLogic(IUserService userService, IRolesRepository rolesRepository)
|
||||
{
|
||||
_userService = userService;
|
||||
_rolesRepository = rolesRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main - CRUD
|
||||
|
||||
public async Task<Result<IEnumerable<RoleActorResponse>>> GetRoleActors(RoleActorFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
IEnumerable<RoleActor> entities = await _rolesRepository.GetAllActorsAsync(filterQuery, orderQuery, pagingQuery);
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<RoleActorResponse>> GetRoleActor(Guid roleId)
|
||||
{
|
||||
RoleActor? entity = await _rolesRepository.GetAsync<RoleActor>(roleId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse()),
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<IEnumerable<RoleCreatorResponse>>> GetRoleCreators(RoleCreatorFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
IEnumerable<RoleCreator> entities = await _rolesRepository.GetAllCreatorsAsync(filterQuery, orderQuery, pagingQuery);
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<RoleCreatorResponse>> GetRoleCreator(Guid roleId)
|
||||
{
|
||||
RoleCreator? entity = await _rolesRepository.GetAsync<RoleCreator>(roleId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse()),
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<RoleActorResponse>> PostRoleActor(RoleActorRequest body)
|
||||
{
|
||||
RoleActor entity = body.ToEntity();
|
||||
await _rolesRepository.AddAsync(entity);
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result<RoleCreatorResponse>> PostRoleCreator(RoleCreatorRequest body)
|
||||
{
|
||||
RoleCreator entity = body.ToEntity();
|
||||
await _rolesRepository.AddAsync(entity);
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result<RoleActorResponse>> PutRoleActor(Guid roleId, RoleActorRequest body)
|
||||
{
|
||||
return await _rolesRepository.UpdateAsync<RoleActor>(roleId, x => x.UpdateWithRequest(body)) switch
|
||||
{
|
||||
false => Result.NotFound(),
|
||||
true => Result.Success()
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<RoleCreatorResponse>> PutRoleCreator(Guid roleId, RoleCreatorRequest body)
|
||||
{
|
||||
return await _rolesRepository.UpdateAsync<RoleCreator>(roleId, x => x.UpdateWithRequest(body)) switch
|
||||
{
|
||||
false => Result.NotFound(),
|
||||
true => Result.Success()
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteRole(Guid roleId)
|
||||
{
|
||||
await _rolesRepository.DeleteAsync(roleId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Main - Rating
|
||||
|
||||
public async Task<Result<RatingOverallResponse>> GetRoleRating(Guid roleId)
|
||||
{
|
||||
if (!await _rolesRepository.ExistsAsync(roleId))
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<RoleRating> ratings = await _rolesRepository.GetRoleRatingsAsync(roleId);
|
||||
return Result.Success(ratings.ToOverallResponse());
|
||||
}
|
||||
|
||||
public async Task<Result<RatingUserResponse>> GetRoleUserRating(Guid roleId, long accountId)
|
||||
{
|
||||
RoleRating? entity = await _rolesRepository.GetRoleRatingByUserAsync(roleId, accountId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToUserResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result> PutRoleRating(Guid roleId, RatingRequest body)
|
||||
{
|
||||
Account accountEntity = await _userService.GetAccountAsync();
|
||||
Role? roleEntity = await _rolesRepository.GetAsync(roleId);
|
||||
if (roleEntity is null)
|
||||
{
|
||||
return Result.NotFound();
|
||||
}
|
||||
await _rolesRepository.UpdateOrAddRoleRatingAsync(roleEntity.Id, accountEntity.Id, () => body.ToEntity(roleEntity.Id, accountEntity.Id), x => x.UpdateWithRequest(body));
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteRoleRating(Guid roleId)
|
||||
{
|
||||
Account accountEntity = await _userService.GetAccountAsync();
|
||||
await _rolesRepository.DeleteRoleUserRatingAsync(roleId, accountEntity.Id);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region ActorTypes
|
||||
|
||||
public async Task<Result<IEnumerable<RoleActorTypeResponse>>> GetRoleActorTypes(RoleActorTypeFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
IEnumerable<RoleActorType> entities = await _rolesRepository.GetAllActorTypesAsync(filterQuery, orderQuery, pagingQuery);
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<RoleActorTypeResponse>> GetRoleActorType(short roleActorTypeId)
|
||||
{
|
||||
RoleActorType? entity = await _rolesRepository.GetActorTypeAsync(roleActorTypeId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<RoleActorTypeResponse>> PostRoleActorType(RoleActorTypeRequest body)
|
||||
{
|
||||
RoleActorType entity = body.ToEntity();
|
||||
await _rolesRepository.AddActorTypeAsync(body.ToEntity());
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteRoleActorType(short roleActorTypeId)
|
||||
{
|
||||
await _rolesRepository.DeleteActorTypeAsync(roleActorTypeId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CreatorTypes
|
||||
|
||||
public async Task<Result<IEnumerable<RoleCreatorTypeResponse>>> GetRoleCreatorTypes(RoleCreatorTypeFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery)
|
||||
{
|
||||
IEnumerable<RoleCreatorType> entities = await _rolesRepository.GetAllCreatorTypesAsync(filterQuery, orderQuery, pagingQuery);
|
||||
return Result.Success(entities.Select(x => x.ToResponse()));
|
||||
}
|
||||
|
||||
public async Task<Result<RoleCreatorTypeResponse>> GetRoleCreatorType(short roleCreatorTypeId)
|
||||
{
|
||||
RoleCreatorType? entity = await _rolesRepository.GetCreatorTypeAsync(roleCreatorTypeId);
|
||||
return entity switch
|
||||
{
|
||||
null => Result.NotFound(),
|
||||
_ => Result.Success(entity.ToResponse())
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Result<RoleCreatorTypeResponse>> PostRoleCreatorType(RoleCreatorTypeRequest body)
|
||||
{
|
||||
RoleCreatorType entity = body.ToEntity();
|
||||
await _rolesRepository.AddCreatorTypeAsync(body.ToEntity());
|
||||
return Result.Created(entity.ToResponse());
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteRoleCreatorType(short roleCreatorTypeId)
|
||||
{
|
||||
await _rolesRepository.DeleteActorTypeAsync(roleCreatorTypeId);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
10
WatchIt.WebAPI/Constants/AdditionalClaimNames.cs
Normal file
10
WatchIt.WebAPI/Constants/AdditionalClaimNames.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace WatchIt.WebAPI.Constants;
|
||||
|
||||
public struct AdditionalClaimNames
|
||||
{
|
||||
#region CONSTANTS
|
||||
|
||||
public const string Admin = "admin";
|
||||
|
||||
#endregion
|
||||
}
|
||||
6
WatchIt.WebAPI/Constants/Policies.cs
Normal file
6
WatchIt.WebAPI/Constants/Policies.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace WatchIt.WebAPI.Constants;
|
||||
|
||||
public class Policies
|
||||
{
|
||||
public const string Admin = "Admin";
|
||||
}
|
||||
193
WatchIt.WebAPI/Controllers/AccountsController.cs
Normal file
193
WatchIt.WebAPI/Controllers/AccountsController.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
using Ardalis.Result;
|
||||
using Ardalis.Result.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.Account;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountBackgroundPicture;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountEmail;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountLogout;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountPassword;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountProfileInfo;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.AccountUsername;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Response;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.BusinessLogic.Accounts;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("accounts")]
|
||||
public class AccountsController(IAccountsBusinessLogic accountsBusinessLogic) : ControllerBase
|
||||
{
|
||||
#region Main
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<AccountResponse>>> GetAccounts([FromQuery]AccountFilterQuery filterQuery, [FromQuery]OrderQuery orderQuery, [FromQuery]PagingQuery pagingQuery, [FromQuery(Name = "include_profile_pictures")]bool includeProfilePictures = false) =>
|
||||
await accountsBusinessLogic.GetAccounts(filterQuery, orderQuery, pagingQuery, includeProfilePictures);
|
||||
|
||||
[HttpGet("{id:long}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<AccountResponse>> GetAccount([FromRoute(Name = "id")] long id, [FromQuery(Name = "include_profile_pictures")]bool includeProfilePictures = false) =>
|
||||
await accountsBusinessLogic.GetAccount(id, includeProfilePictures);
|
||||
|
||||
[HttpPost]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<AccountResponse>> PostAccount([FromBody]AccountRequest body) =>
|
||||
await accountsBusinessLogic.PostAccount(body);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile picture
|
||||
|
||||
[HttpGet("{id:long}/profile_picture")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<ImageResponse>> GetAccountProfilePicture([FromRoute(Name = "id")] long id) =>
|
||||
await accountsBusinessLogic.GetAccountProfilePicture(id);
|
||||
|
||||
[HttpPut("profile_picture")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<ImageResponse>> PutAccountProfilePicture([FromBody] ImageRequest body) =>
|
||||
await accountsBusinessLogic.PutAccountProfilePicture(body);
|
||||
|
||||
[HttpDelete("profile_picture")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteAccountProfilePicture() =>
|
||||
await accountsBusinessLogic.DeleteAccountProfilePicture();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Background picture
|
||||
|
||||
[HttpGet("{id:long}/background_picture")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PhotoResponse>> GetAccountBackgroundPicture([FromRoute(Name = "id")] long id) =>
|
||||
await accountsBusinessLogic.GetAccountBackgroundPicture(id);
|
||||
|
||||
[HttpPut("background_picture")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PhotoResponse>> PutAccountBackgroundPicture([FromBody] AccountBackgroundPictureRequest body) =>
|
||||
await accountsBusinessLogic.PutAccountBackgroundPicture(body);
|
||||
|
||||
[HttpDelete("background_picture")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteAccountBackgroundPicture() =>
|
||||
await accountsBusinessLogic.DeleteAccountBackgroundPicture();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile edit
|
||||
|
||||
[HttpPatch("profile_info")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PatchAccountProfileInfo([FromBody] AccountProfileInfoRequest body) =>
|
||||
await accountsBusinessLogic.PatchAccountProfileInfo(body);
|
||||
|
||||
[HttpPatch("username")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PatchAccountUsername([FromBody] AccountUsernameRequest body) =>
|
||||
await accountsBusinessLogic.PatchAccountUsername(body);
|
||||
|
||||
[HttpPatch("email")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PatchAccountEmail([FromBody] AccountEmailRequest body) =>
|
||||
await accountsBusinessLogic.PatchAccountEmail(body);
|
||||
|
||||
[HttpPatch("password")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PatchAccountPassword([FromBody] AccountPasswordRequest body) =>
|
||||
await accountsBusinessLogic.PatchAccountPassword(body);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Log out
|
||||
|
||||
[HttpDelete("logout")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> Logout([FromBody]AccountLogoutRequest body) =>
|
||||
await accountsBusinessLogic.Logout(body);
|
||||
|
||||
[HttpDelete("logout_all")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> LogoutAll() =>
|
||||
await accountsBusinessLogic.LogoutAll();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Follows
|
||||
|
||||
[HttpGet("{id:long}/follows")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<AccountResponse>>> GetAccountFollows([FromRoute(Name = "id")] long id, [FromQuery] AccountFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await accountsBusinessLogic.GetAccountFollows(id, filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpGet("{id:long}/followers")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<AccountResponse>>> GetAccountFollowers([FromRoute(Name = "id")] long id, [FromQuery] AccountFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await accountsBusinessLogic.GetAccountFollowers(id, filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpPost("follows/{followed_account_id:long}")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PostAccountFollow([FromRoute(Name = "followed_account_id")] long followedAccountId) =>
|
||||
await accountsBusinessLogic.PostAccountFollow(followedAccountId);
|
||||
|
||||
[HttpDelete("follows/{followed_account_id:long}")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteAccountFollow([FromRoute(Name = "followed_account_id")] long followedAccountId) =>
|
||||
await accountsBusinessLogic.DeleteAccountFollow(followedAccountId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ratings
|
||||
|
||||
[HttpGet("{id:long}/media")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<MediumUserRatedResponse>>> GetAccountRatedMedia([FromRoute(Name = "id")] long id, [FromQuery]MediumFilterQuery filterQuery, [FromQuery]MediumUserRatedFilterQuery<Medium> userRatedFilterQuery, [FromQuery]OrderQuery orderQuery, [FromQuery]PagingQuery pagingQuery, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await accountsBusinessLogic.GetAccountRatedMedia(id, filterQuery, userRatedFilterQuery, orderQuery, pagingQuery, includePictures);
|
||||
|
||||
[HttpGet("{id:long}/media/movies")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<MediumMovieUserRatedResponse>>> GetAccountRatedMediaMovies([FromRoute(Name = "id")] long id, [FromQuery]MediumMovieFilterQuery filterQuery, [FromQuery]MediumUserRatedFilterQuery<MediumMovie> userRatedFilterQuery, [FromQuery]OrderQuery orderQuery, [FromQuery]PagingQuery pagingQuery, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await accountsBusinessLogic.GetAccountRatedMediaMovies(id, filterQuery, userRatedFilterQuery, orderQuery, pagingQuery, includePictures);
|
||||
|
||||
[HttpGet("{id:long}/media/series")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<MediumSeriesUserRatedResponse>>> GetAccountRatedMediaSeries([FromRoute(Name = "id")] long id, [FromQuery]MediumSeriesFilterQuery filterQuery, [FromQuery]MediumUserRatedFilterQuery<MediumSeries> userRatedFilterQuery, [FromQuery]OrderQuery orderQuery, [FromQuery]PagingQuery pagingQuery, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await accountsBusinessLogic.GetAccountRatedMediaSeries(id, filterQuery, userRatedFilterQuery, orderQuery, pagingQuery, includePictures);
|
||||
|
||||
[HttpGet("{id:long}/people")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<PersonUserRatedResponse>>> GetAccountRatedPeople([FromRoute(Name = "id")] long id, [FromQuery]PersonFilterQuery filterQuery, [FromQuery]PersonUserRatedFilterQuery userRatedFilterQuery, [FromQuery]OrderQuery orderQuery, [FromQuery]PagingQuery pagingQuery, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await accountsBusinessLogic.GetAccountRatedPeople(id, filterQuery, userRatedFilterQuery, orderQuery, pagingQuery, includePictures);
|
||||
|
||||
#endregion
|
||||
}
|
||||
24
WatchIt.WebAPI/Controllers/AuthenticationController.cs
Normal file
24
WatchIt.WebAPI/Controllers/AuthenticationController.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Ardalis.Result;
|
||||
using Ardalis.Result.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.Account;
|
||||
using WatchIt.DTO.Models.Controllers.Authentication;
|
||||
using WatchIt.WebAPI.BusinessLogic.Authentication;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("authentication")]
|
||||
public class AuthenticationController(IAuthenticationBusinessLogic authenticationBusinessLogic) : ControllerBase
|
||||
{
|
||||
[HttpPost("authenticate")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<AuthenticationResponse>> Authenticate([FromBody]AuthenticationRequest body) => await authenticationBusinessLogic.Authenticate(body);
|
||||
|
||||
[HttpPost("authenticate_refresh")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<AuthenticationResponse>> AuthenticateRefresh([FromBody]AuthenticationRefreshRequest body) => await authenticationBusinessLogic.AuthenticateRefresh(body);
|
||||
}
|
||||
39
WatchIt.WebAPI/Controllers/GendersController.cs
Normal file
39
WatchIt.WebAPI/Controllers/GendersController.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Ardalis.Result;
|
||||
using Ardalis.Result.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.DTO.Models.Controllers.Genders.Gender;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.BusinessLogic.Genders;
|
||||
using WatchIt.WebAPI.Constants;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("genders")]
|
||||
public class GendersController(IGendersBusinessLogic gendersBusinessLogic) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<GenderResponse>>> GetGenders([FromQuery] GenderFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await gendersBusinessLogic.GetGenders(filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<GenderResponse>> GetGender([FromRoute(Name = "id")] short id) =>
|
||||
await gendersBusinessLogic.GetGender(id);
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<GenderResponse>> PostGender([FromBody] GenderRequest body) =>
|
||||
await gendersBusinessLogic.PostGender(body);
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteGender([FromRoute(Name = "id")] short id) =>
|
||||
await gendersBusinessLogic.DeleteGender(id);
|
||||
}
|
||||
39
WatchIt.WebAPI/Controllers/GenresController.cs
Normal file
39
WatchIt.WebAPI/Controllers/GenresController.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Ardalis.Result;
|
||||
using Ardalis.Result.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.BusinessLogic.Genres;
|
||||
using WatchIt.WebAPI.Constants;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("genres")]
|
||||
public class GenresController(IGenresBusinessLogic genresBusinessLogic) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<GenreResponse>>> GetGenres([FromQuery] GenreFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await genresBusinessLogic.GetGenres(filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<GenreResponse>> GetGenre([FromRoute(Name = "id")] short id) =>
|
||||
await genresBusinessLogic.GetGenre(id);
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<GenreResponse>> PostGenre([FromBody] GenreRequest body) =>
|
||||
await genresBusinessLogic.PostGenre(body);
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteGenre([FromRoute(Name = "id")] short id) =>
|
||||
await genresBusinessLogic.DeleteGenre(id);
|
||||
}
|
||||
184
WatchIt.WebAPI/Controllers/MediaController.cs
Normal file
184
WatchIt.WebAPI/Controllers/MediaController.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using Ardalis.Result;
|
||||
using Ardalis.Result.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Request;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Response;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Response;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Models.Generics.Rating;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.BusinessLogic.Media;
|
||||
using WatchIt.WebAPI.Constants;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("media")]
|
||||
public class MediaController(IMediaBusinessLogic mediaBusinessLogic) : ControllerBase
|
||||
{
|
||||
#region Main
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<MediumResponse>>> GetMedia([FromQuery] MediumFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await mediaBusinessLogic.GetMedia(filterQuery, orderQuery, pagingQuery, includePictures);
|
||||
|
||||
[HttpGet("{id:long}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<MediumResponse>> GetMedium([FromRoute(Name = "id")] long id, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await mediaBusinessLogic.GetMedium(id, includePictures);
|
||||
|
||||
[HttpGet("movies")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<MediumMovieResponse>>> GetMediumMovies([FromQuery] MediumMovieFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await mediaBusinessLogic.GetMediumMovies(filterQuery, orderQuery, pagingQuery, includePictures);
|
||||
|
||||
[HttpGet("movies/{id:long}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<MediumMovieResponse>> GetMediumMovie([FromRoute(Name = "id")] long id, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await mediaBusinessLogic.GetMediumMovie(id, includePictures);
|
||||
|
||||
[HttpGet("series")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<MediumSeriesResponse>>> GetMediumSeries([FromQuery] MediumSeriesFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await mediaBusinessLogic.GetMediumSeries(filterQuery, orderQuery, pagingQuery, includePictures);
|
||||
|
||||
[HttpGet("series/{id:long}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<MediumSeriesResponse>> GetMediumSeries([FromRoute(Name = "id")] long id, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await mediaBusinessLogic.GetMediumSeries(id, includePictures);
|
||||
|
||||
[HttpPost("movies")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<MediumMovieResponse>> PostMediumMovie([FromBody] MediumMovieRequest body) =>
|
||||
await mediaBusinessLogic.PostMediumMovie(body);
|
||||
|
||||
[HttpPost("series")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<MediumSeriesResponse>> PostMediumSeries([FromBody] MediumSeriesRequest body) =>
|
||||
await mediaBusinessLogic.PostMediumSeries(body);
|
||||
|
||||
[HttpPut("movies/{id:long}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<MediumMovieResponse>> PutMediumMovie([FromRoute(Name = "id")] long id, [FromBody] MediumMovieRequest body) =>
|
||||
await mediaBusinessLogic.PutMediumMovie(id, body);
|
||||
|
||||
[HttpPut("series/{id:long}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<MediumSeriesResponse>> PutMediumSeries([FromRoute(Name = "id")] long id, [FromBody] MediumSeriesRequest body) =>
|
||||
await mediaBusinessLogic.PutMediumSeries(id, body);
|
||||
|
||||
[HttpDelete("{id:long}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteMedium([FromRoute(Name = "id")] long id) =>
|
||||
await mediaBusinessLogic.DeleteMedium(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Genres
|
||||
|
||||
[HttpGet("{id:long}/genres")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<GenreResponse>>> GetMediumGenres([FromRoute(Name = "id")] long id, [FromQuery] GenreFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await mediaBusinessLogic.GetMediumGenres(id, filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpPost("{id:long}/genres/{genre_id}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PostMediumGenre([FromRoute(Name = "id")] long id, [FromRoute(Name = "genre_id")] short genreId) =>
|
||||
await mediaBusinessLogic.PostMediumGenre(id, genreId);
|
||||
|
||||
[HttpDelete("{id:long}/genres/{genre_id}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteMediumGenre([FromRoute(Name = "id")] long id, [FromRoute(Name = "genre_id")] short genreId) =>
|
||||
await mediaBusinessLogic.DeleteMediumGenre(id, genreId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
[HttpGet("{id:long}/rating")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RatingOverallResponse>> GetMediumRating([FromRoute(Name = "id")] long id) =>
|
||||
await mediaBusinessLogic.GetMediumRating(id);
|
||||
|
||||
[HttpGet("{id:long}/rating/{account_id:long}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RatingUserResponse>> GetMediumUserRating([FromRoute(Name = "id")] long id, [FromRoute(Name = "account_id")] long accountId) =>
|
||||
await mediaBusinessLogic.GetMediumUserRating(id, accountId);
|
||||
|
||||
[HttpPut("{id:long}/rating")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PutMediumRating([FromRoute(Name = "id")] long id, [FromBody] RatingRequest body) =>
|
||||
await mediaBusinessLogic.PutMediumRating(id, body);
|
||||
|
||||
[HttpDelete("{id:long}/rating")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteMediumRating([FromRoute(Name = "id")] long id) =>
|
||||
await mediaBusinessLogic.DeleteMediumRating(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region View Count
|
||||
|
||||
[HttpPut("{id:long}/view_count")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PutMediumViewCount([FromRoute(Name = "id")] long id) =>
|
||||
await mediaBusinessLogic.PutMediumViewCount(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Picture
|
||||
|
||||
[HttpGet("{id:long}/picture")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<ImageResponse>> GetMediumPicture([FromRoute(Name = "id")] long id) =>
|
||||
await mediaBusinessLogic.GetMediumPicture(id);
|
||||
|
||||
[HttpPut("{id:long}/picture")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<ImageResponse>> PutMediumPicture([FromRoute(Name = "id")] long id, [FromBody] ImageRequest body) =>
|
||||
await mediaBusinessLogic.PutMediumPicture(id, body);
|
||||
|
||||
[HttpDelete("{id:long}/picture")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteMediumPicture([FromRoute(Name = "id")] long id) =>
|
||||
await mediaBusinessLogic.DeleteMediumPicture(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Photos
|
||||
|
||||
[HttpGet("{id:long}/photos/background")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PhotoResponse?>> GetMediumBackgroundPhoto([FromRoute(Name = "id")] long id) =>
|
||||
await mediaBusinessLogic.GetMediumPhotoBackground(id);
|
||||
|
||||
#endregion
|
||||
}
|
||||
100
WatchIt.WebAPI/Controllers/PeopleController.cs
Normal file
100
WatchIt.WebAPI/Controllers/PeopleController.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using Ardalis.Result;
|
||||
using Ardalis.Result.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person.Query;
|
||||
using WatchIt.DTO.Models.Generics.Image;
|
||||
using WatchIt.DTO.Models.Generics.Rating;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.BusinessLogic.People;
|
||||
using WatchIt.WebAPI.Constants;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("people")]
|
||||
public class PeopleController(IPeopleBusinessLogic peopleBusinessLogic) : ControllerBase
|
||||
{
|
||||
#region Main
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<PersonResponse>>> GetPeople([FromQuery] PersonFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await peopleBusinessLogic.GetPeople(filterQuery, orderQuery, pagingQuery, includePictures);
|
||||
|
||||
[HttpGet("{id:long}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PersonResponse>> GetPerson([FromRoute(Name = "id")] long id, [FromQuery(Name = "include_pictures")]bool includePictures = false) =>
|
||||
await peopleBusinessLogic.GetPerson(id, includePictures);
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PersonResponse>> PostPerson([FromBody] PersonRequest body) =>
|
||||
await peopleBusinessLogic.PostPerson(body);
|
||||
|
||||
[HttpPut("{id:long}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PersonResponse>> PutPerson([FromRoute(Name = "id")] long id, [FromBody] PersonRequest body) =>
|
||||
await peopleBusinessLogic.PutPerson(id, body);
|
||||
|
||||
[HttpDelete("{id:long}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeletePerson([FromRoute(Name = "id")] long id) =>
|
||||
await peopleBusinessLogic.DeletePerson(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
[HttpGet("{id:long}/rating")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RatingOverallResponse>> GetPersonRating([FromRoute(Name = "id")] long id) =>
|
||||
await peopleBusinessLogic.GetPersonRating(id);
|
||||
|
||||
[HttpGet("{id:long}/rating/{account_id:long}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RatingUserOverallResponse>> GetPersonUserRating([FromRoute(Name = "id")] long id, [FromRoute(Name = "account_id")] long accountId) =>
|
||||
await peopleBusinessLogic.GetPersonUserRating(id, accountId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region View Count
|
||||
|
||||
[HttpPut("{id:long}/view_count")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PutPeopleViewCount([FromRoute(Name = "id")] long id) =>
|
||||
await peopleBusinessLogic.PutPeopleViewCount(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Picture
|
||||
|
||||
[HttpGet("{id:long}/picture")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<ImageResponse>> GetPersonPicture([FromRoute(Name = "id")] long id) =>
|
||||
await peopleBusinessLogic.GetPersonPicture(id);
|
||||
|
||||
[HttpPut("{id:long}/picture")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<ImageResponse>> PutPersonPicture([FromRoute(Name = "id")] long id, [FromBody] ImageRequest body) =>
|
||||
await peopleBusinessLogic.PutPersonPicture(id, body);
|
||||
|
||||
[HttpDelete("{id:long}/picture")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeletePersonPicture([FromRoute(Name = "id")] long id) =>
|
||||
await peopleBusinessLogic.DeletePersonPicture(id);
|
||||
|
||||
#endregion
|
||||
}
|
||||
72
WatchIt.WebAPI/Controllers/PhotosController.cs
Normal file
72
WatchIt.WebAPI/Controllers/PhotosController.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using Ardalis.Result;
|
||||
using Ardalis.Result.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.PhotoBackground;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.BusinessLogic.Photos;
|
||||
using WatchIt.WebAPI.Constants;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("photos")]
|
||||
public class PhotosController(IPhotosBusinessLogic photosBusinessLogic) : ControllerBase
|
||||
{
|
||||
#region Main
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PhotoResponse>> GetPhoto([FromRoute(Name = "id")] Guid id) =>
|
||||
await photosBusinessLogic.GetPhoto(id);
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<PhotoResponse>>> GetPhotos([FromQuery] PhotoFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await photosBusinessLogic.GetPhotos(filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PhotoResponse>> PostPhoto([FromBody] PhotoRequest body) =>
|
||||
await photosBusinessLogic.PostPhoto(body);
|
||||
|
||||
[HttpPut("{id:guid}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PhotoResponse>> PutPhoto([FromRoute(Name = "id")] Guid id, [FromBody] PhotoRequest body) =>
|
||||
await photosBusinessLogic.PutPhoto(id, body);
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeletePhoto([FromRoute(Name = "id")] Guid id) =>
|
||||
await photosBusinessLogic.DeletePhoto(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Background
|
||||
|
||||
[HttpGet("background")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PhotoResponse>> GetPhotoBackground() =>
|
||||
await photosBusinessLogic.GetPhotoBackground();
|
||||
|
||||
[HttpPut("{photo_id:guid}/background")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<PhotoBackgroundResponse>> PutPhotoBackground([FromRoute(Name = "photo_id")] Guid photoId, [FromBody] PhotoBackgroundRequest body) =>
|
||||
await photosBusinessLogic.PutPhotoBackground(photoId, body);
|
||||
|
||||
[HttpDelete("{photo_id:guid}/background")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeletePhotoBackground([FromRoute(Name = "photo_id")] Guid photoId) =>
|
||||
await photosBusinessLogic.DeletePhotoBackground(photoId);
|
||||
|
||||
#endregion
|
||||
}
|
||||
162
WatchIt.WebAPI/Controllers/RolesController.cs
Normal file
162
WatchIt.WebAPI/Controllers/RolesController.cs
Normal file
@@ -0,0 +1,162 @@
|
||||
using Ardalis.Result;
|
||||
using Ardalis.Result.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Request;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Response;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleActorType;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleCreatorType;
|
||||
using WatchIt.DTO.Models.Generics.Rating;
|
||||
using WatchIt.DTO.Query;
|
||||
using WatchIt.WebAPI.BusinessLogic.Roles;
|
||||
using WatchIt.WebAPI.Constants;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("roles")]
|
||||
public class RolesController(IRolesBusinessLogic rolesBusinessLogic) : ControllerBase
|
||||
{
|
||||
#region Main - CRUD
|
||||
|
||||
[HttpGet("actors")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<RoleActorResponse>>> GetRoleActors([FromQuery] RoleActorFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await rolesBusinessLogic.GetRoleActors(filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpGet("actors/{id:guid}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleActorResponse>> GetRoleActor([FromRoute] Guid id) =>
|
||||
await rolesBusinessLogic.GetRoleActor(id);
|
||||
|
||||
[HttpGet("creators")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<RoleCreatorResponse>>> GetRoleCreators([FromQuery] RoleCreatorFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await rolesBusinessLogic.GetRoleCreators(filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpGet("creators/{id:guid}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleCreatorResponse>> GetRoleCreator([FromRoute] Guid id) =>
|
||||
await rolesBusinessLogic.GetRoleCreator(id);
|
||||
|
||||
[HttpPost("actors")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleActorResponse>> PostRoleActor([FromBody] RoleActorRequest body) =>
|
||||
await rolesBusinessLogic.PostRoleActor(body);
|
||||
|
||||
[HttpPost("creators")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleCreatorResponse>> PostRoleCreator([FromBody] RoleCreatorRequest body) =>
|
||||
await rolesBusinessLogic.PostRoleCreator(body);
|
||||
|
||||
[HttpPut("actors/{id:guid}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleActorResponse>> PutRoleActor([FromRoute] Guid id, [FromBody] RoleActorRequest body) =>
|
||||
await rolesBusinessLogic.PutRoleActor(id, body);
|
||||
|
||||
[HttpPut("creators/{id:guid}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleCreatorResponse>> PutRoleCreator([FromRoute] Guid id, [FromBody] RoleCreatorRequest body) =>
|
||||
await rolesBusinessLogic.PutRoleCreator(id, body);
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteRole([FromRoute] Guid id) =>
|
||||
await rolesBusinessLogic.DeleteRole(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Main - Rating
|
||||
|
||||
[HttpGet("{id:guid}/rating")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RatingOverallResponse>> GetRoleRating([FromRoute] Guid id) =>
|
||||
await rolesBusinessLogic.GetRoleRating(id);
|
||||
|
||||
[HttpGet("{id:guid}/rating/{account_id:long}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RatingUserResponse>> GetRoleUserRating([FromRoute] Guid id, [FromRoute(Name = "account_id")] long accountId) =>
|
||||
await rolesBusinessLogic.GetRoleUserRating(id, accountId);
|
||||
|
||||
[HttpPut("{id:guid}/rating")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> PutRoleRating([FromRoute] Guid id, [FromBody] RatingRequest body) =>
|
||||
await rolesBusinessLogic.PutRoleRating(id, body);
|
||||
|
||||
[HttpDelete("{id:guid}/rating")]
|
||||
[Authorize]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteRoleRating([FromRoute] Guid id) =>
|
||||
await rolesBusinessLogic.DeleteRoleRating(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region ActorTypes
|
||||
|
||||
[HttpGet("actors/types")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<RoleActorTypeResponse>>> GetRoleActorTypes([FromQuery] RoleActorTypeFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await rolesBusinessLogic.GetRoleActorTypes(filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpGet("actors/types/{role_actor_type_id}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleActorTypeResponse>> GetRoleActorType([FromRoute(Name = "role_actor_type_id")] short roleActorTypeId) =>
|
||||
await rolesBusinessLogic.GetRoleActorType(roleActorTypeId);
|
||||
|
||||
[HttpPost("actors/types")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleActorTypeResponse>> PostRoleActorType([FromBody] RoleActorTypeRequest body) =>
|
||||
await rolesBusinessLogic.PostRoleActorType(body);
|
||||
|
||||
[HttpDelete("actors/types/{role_actor_type_id}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteRoleActorType([FromRoute(Name = "role_actor_type_id")] short roleActorTypeId) =>
|
||||
await rolesBusinessLogic.DeleteRoleActorType(roleActorTypeId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region CreatorTypes
|
||||
|
||||
[HttpGet("creators/types")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<IEnumerable<RoleCreatorTypeResponse>>> GetRoleCreatorTypes([FromQuery] RoleCreatorTypeFilterQuery filterQuery, [FromQuery] OrderQuery orderQuery, [FromQuery] PagingQuery pagingQuery) =>
|
||||
await rolesBusinessLogic.GetRoleCreatorTypes(filterQuery, orderQuery, pagingQuery);
|
||||
|
||||
[HttpGet("creators/types/{role_creator_type_id}")]
|
||||
[AllowAnonymous]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleCreatorTypeResponse>> GetRoleCreatorType([FromRoute(Name = "role_creator_type_id")] short roleCreatorTypeId) =>
|
||||
await rolesBusinessLogic.GetRoleCreatorType(roleCreatorTypeId);
|
||||
|
||||
[HttpPost("creators/types")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result<RoleCreatorTypeResponse>> PostRoleCreatorType([FromBody] RoleCreatorTypeRequest body) =>
|
||||
await rolesBusinessLogic.PostRoleCreatorType(body);
|
||||
|
||||
[HttpDelete("creators/types/{role_creator_type_id}")]
|
||||
[Authorize(Policy = Policies.Admin)]
|
||||
[TranslateResultToActionResult]
|
||||
public async Task<Result> DeleteRoleCreatorType([FromRoute(Name = "role_creator_type_id")] short roleCreatorTypeId) =>
|
||||
await rolesBusinessLogic.DeleteRoleCreatorType(roleCreatorTypeId);
|
||||
|
||||
#endregion
|
||||
}
|
||||
50
WatchIt.WebAPI/Helpers/PasswordHelpers.cs
Normal file
50
WatchIt.WebAPI/Helpers/PasswordHelpers.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using SimpleToolkit.Extensions;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts;
|
||||
|
||||
namespace WatchIt.WebAPI.Helpers;
|
||||
|
||||
public static class PasswordHelpers
|
||||
{
|
||||
#region CONSTANTS
|
||||
|
||||
private const string RandomPasswordCharacters = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890!@#$%^&*()-_=+[{]};:'\"\\|,<.>/?";
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public static PasswordData GeneratePasswordData(string password)
|
||||
{
|
||||
string leftSalt = StringExtensions.CreateRandom(20, RandomPasswordCharacters);
|
||||
string rightSalt = StringExtensions.CreateRandom(20, RandomPasswordCharacters);
|
||||
byte[] hash = ComputeHash(password, leftSalt, rightSalt);
|
||||
return new PasswordData
|
||||
{
|
||||
LeftSalt = leftSalt,
|
||||
RightSalt = rightSalt,
|
||||
PasswordHash = hash,
|
||||
};
|
||||
}
|
||||
|
||||
public static byte[] ComputeHash(string password, string leftSalt, string rightSalt)
|
||||
{
|
||||
string stringToHash = $"{leftSalt}{password}{rightSalt}";
|
||||
byte[] encodedString = Encoding.UTF8.GetBytes(stringToHash);
|
||||
byte[] hash = SHA512.HashData(encodedString);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public static bool ValidatePassword(string password, PasswordData passwordData)
|
||||
{
|
||||
byte[] checkedHash = ComputeHash(password, passwordData.LeftSalt, passwordData.RightSalt);
|
||||
byte[] actualHash = passwordData.PasswordHash;
|
||||
bool result = checkedHash.SequenceEqual(actualHash);
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
206
WatchIt.WebAPI/Program.cs
Normal file
206
WatchIt.WebAPI/Program.cs
Normal file
@@ -0,0 +1,206 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Delta;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.HttpLogging;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.DTO;
|
||||
using WatchIt.DTO.Converters;
|
||||
using WatchIt.WebAPI.BusinessLogic.Accounts;
|
||||
using WatchIt.WebAPI.BusinessLogic.Authentication;
|
||||
using WatchIt.WebAPI.BusinessLogic.Genders;
|
||||
using WatchIt.WebAPI.BusinessLogic.Genres;
|
||||
using WatchIt.WebAPI.BusinessLogic.Media;
|
||||
using WatchIt.WebAPI.BusinessLogic.People;
|
||||
using WatchIt.WebAPI.BusinessLogic.Photos;
|
||||
using WatchIt.WebAPI.BusinessLogic.Roles;
|
||||
using WatchIt.WebAPI.Constants;
|
||||
using WatchIt.WebAPI.Repositories.Accounts;
|
||||
using WatchIt.WebAPI.Repositories.Genders;
|
||||
using WatchIt.WebAPI.Repositories.Genres;
|
||||
using WatchIt.WebAPI.Repositories.Media;
|
||||
using WatchIt.WebAPI.Repositories.People;
|
||||
using WatchIt.WebAPI.Repositories.Photos;
|
||||
using WatchIt.WebAPI.Repositories.Roles;
|
||||
using WatchIt.WebAPI.Services.Tokens;
|
||||
using WatchIt.WebAPI.Services.User;
|
||||
|
||||
namespace WatchIt.WebAPI;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||
builder.SetupAuthentication();
|
||||
builder.SetupDatabases();
|
||||
builder.Services.AddRepositories();
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddServices();
|
||||
builder.Services.AddBusinessLogic();
|
||||
builder.Services.AddWorkers();
|
||||
builder.Services.AddFluentValidation();
|
||||
builder.Services.AddControllers()
|
||||
.AddJsonOptions(x =>
|
||||
{
|
||||
x.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
|
||||
x.JsonSerializerOptions.Converters.Add(new ColorJsonConverter());
|
||||
});
|
||||
builder.Services.AddOpenApi();
|
||||
builder.Services.AddHttpLogging(o => { o.LoggingFields |= HttpLoggingFields.RequestQuery; });
|
||||
|
||||
|
||||
WebApplication app = builder.Build();
|
||||
|
||||
app.UseHttpLogging();
|
||||
|
||||
app.UseDelta<DatabaseContext>();
|
||||
|
||||
app.InitializeDatabase();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private static void AddRepositories(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IGendersRepository, GendersRepository>();
|
||||
services.AddTransient<IGenresRepository, GenresRepository>();
|
||||
services.AddTransient<IAccountsRepository, AccountsRepository>();
|
||||
services.AddTransient<IMediaRepository, MediaRepository>();
|
||||
services.AddTransient<IPeopleRepository, PeopleRepository>();
|
||||
services.AddTransient<IPhotosRepository, PhotosRepository>();
|
||||
services.AddTransient<IRolesRepository, RolesRepository>();
|
||||
}
|
||||
|
||||
private static void AddServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<ITokensService, TokensService>();
|
||||
services.AddTransient<IUserService, UserService>();
|
||||
}
|
||||
|
||||
private static void AddBusinessLogic(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IGendersBusinessLogic, GendersBusinessLogic>();
|
||||
services.AddTransient<IGenresBusinessLogic, GenresBusinessLogic>();
|
||||
services.AddTransient<IAuthenticationBusinessLogic, AuthenticationBusinessLogic>();
|
||||
services.AddTransient<IAccountsBusinessLogic, AccountsBusinessLogic>();
|
||||
services.AddTransient<IMediaBusinessLogic, MediaBusinessLogic>();
|
||||
services.AddTransient<IPeopleBusinessLogic, PeopleBusinessLogic>();
|
||||
services.AddTransient<IPhotosBusinessLogic, PhotosBusinessLogic>();
|
||||
services.AddTransient<IRolesBusinessLogic, RolesBusinessLogic>();
|
||||
}
|
||||
|
||||
private static void AddWorkers(this IServiceCollection services)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private static void AddFluentValidation(this IServiceCollection services)
|
||||
{
|
||||
services.AddValidatorsFromAssembly(Assembly.GetAssembly(typeof(CustomValidators)));
|
||||
services.AddFluentValidationAutoValidation();
|
||||
}
|
||||
|
||||
private static WebApplicationBuilder SetupAuthentication(this WebApplicationBuilder builder)
|
||||
{
|
||||
string issuer = builder.Configuration.GetValue<string>("Authentication:JWT:Issuer")!;
|
||||
string audience = builder.Configuration.GetValue<string>("Authentication:JWT:Audience")!;
|
||||
string key = builder.Configuration.GetValue<string>("Authentication:JWT:Key")!;
|
||||
byte[] encodedKey = Encoding.UTF8.GetBytes(key);
|
||||
|
||||
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
||||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
||||
|
||||
builder.Services
|
||||
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(x =>
|
||||
{
|
||||
x.RequireHttpsMetadata = false;
|
||||
x.SaveToken = true;
|
||||
x.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidIssuer = issuer,
|
||||
ValidAudience = audience,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(encodedKey),
|
||||
ClockSkew = TimeSpan.FromMinutes(1),
|
||||
};
|
||||
x.Events = new JwtBearerEvents
|
||||
{
|
||||
OnAuthenticationFailed = context =>
|
||||
{
|
||||
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
|
||||
{
|
||||
context.Response.Headers.Append("Token-Expired", "true");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
});
|
||||
builder.Services
|
||||
.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy(Policies.Admin, policy => policy.RequireAuthenticatedUser()
|
||||
.RequireClaim(AdditionalClaimNames.Admin, bool.TrueString));
|
||||
});
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static WebApplicationBuilder SetupDatabases(this WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services
|
||||
.AddDbContext<DatabaseContext>(x => x.UseNpgsql(builder.Configuration
|
||||
.GetConnectionString("Database")),
|
||||
ServiceLifetime.Transient);
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static void InitializeDatabase(this WebApplication app)
|
||||
{
|
||||
using (IServiceScope scope = app.Services.CreateScope())
|
||||
{
|
||||
DatabaseContext database = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||
|
||||
while (!database.Database.CanConnect())
|
||||
{
|
||||
app.Logger.LogInformation("Waiting for database...");
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
database.Database.Migrate();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
23
WatchIt.WebAPI/Properties/launchSettings.json
Normal file
23
WatchIt.WebAPI/Properties/launchSettings.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5128",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7027;http://localhost:5128",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
130
WatchIt.WebAPI/Repositories/Accounts/AccountsRepository.cs
Normal file
130
WatchIt.WebAPI/Repositories/Accounts/AccountsRepository.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.Account;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Accounts;
|
||||
|
||||
public class AccountsRepository : Repository<Account>, IAccountsRepository
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public AccountsRepository(DatabaseContext database) : base(database) {}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<bool> ExistsAsync(long id) =>
|
||||
await DefaultSet.AnyAsync(x => x.Id == id);
|
||||
|
||||
public async Task<Account?> GetAsync(long id, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<Account?> GetByUsernameOrEmailAsync(string usernameOrEmail, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Username == usernameOrEmail || x.Email == usernameOrEmail);
|
||||
|
||||
public async Task<IEnumerable<Account>> GetAllAsync(AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null) =>
|
||||
await DefaultSet.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, AccountOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile picture
|
||||
|
||||
public async Task<AccountProfilePicture?> GetProfilePictureAsync(long id, Func<IQueryable<AccountProfilePicture>, IQueryable<AccountProfilePicture>>? additionalIncludes = null) =>
|
||||
await Database.AccountProfilePictures
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.AccountId == id);
|
||||
|
||||
public async Task<AccountProfilePicture> UpdateOrAddProfilePictureAsync(long id, Func<AccountProfilePicture> addFunc, Action<AccountProfilePicture> updateFunc) =>
|
||||
await UpdateOrAddAsync(await GetProfilePictureAsync(id), addFunc, updateFunc);
|
||||
|
||||
public async Task DeleteProfilePictureAsync(long id) =>
|
||||
await DeleteAsync<AccountProfilePicture>(x => x.AccountId == id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Background picture
|
||||
|
||||
public async Task<AccountBackgroundPicture?> GetBackgroundPictureAsync(long id, Func<IQueryable<AccountBackgroundPicture>, IQueryable<AccountBackgroundPicture>>? additionalIncludes = null) =>
|
||||
await Database.AccountBackgroundPictures
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.AccountId == id);
|
||||
|
||||
public async Task<AccountBackgroundPicture> UpdateOrAddBackgroundPictureAsync(long id, Func<AccountBackgroundPicture> addFunc, Action<AccountBackgroundPicture> updateFunc) =>
|
||||
await UpdateOrAddAsync(await GetBackgroundPictureAsync(id, x => x.Include(y => y.Background)
|
||||
.ThenInclude(y => y.Photo)), addFunc, updateFunc);
|
||||
|
||||
public async Task DeleteBackgroundPictureAsync(long id) =>
|
||||
await DeleteAsync<AccountBackgroundPicture>(x => x.AccountId == id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Follow
|
||||
|
||||
public async Task<bool> FollowExistsAsync(long followerId, long followedId) =>
|
||||
await Database.AccountFollows
|
||||
.AnyAsync(x => x.FollowerId == followerId && x.FollowedId == followedId);
|
||||
|
||||
public async Task<IEnumerable<Account>> GetFollowsAsync(long id, AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null) =>
|
||||
await Database.Accounts
|
||||
.Where(x => x.Followers
|
||||
.Any(y => y.Id == id))
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, AccountOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<IEnumerable<Account>> GetFollowersAsync(long id, AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null) =>
|
||||
await Database.Accounts
|
||||
.Where(x => x.Follows
|
||||
.Any(y => y.Id == id))
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, AccountOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task AddFollowAsync(AccountFollow entity) =>
|
||||
await AddAsync(entity);
|
||||
|
||||
public async Task DeleteFollowAsync(long followerId, long followedId) =>
|
||||
await DeleteAsync<AccountFollow>(x => x.FollowerId == followerId && x.FollowedId == followedId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Refresh token
|
||||
|
||||
public async Task UpdateRefreshTokenAsync(AccountRefreshToken refreshToken, Action<AccountRefreshToken> updateFunc) =>
|
||||
await UpdateAsync(refreshToken, updateFunc);
|
||||
|
||||
public async Task AddRefreshTokenAsync(AccountRefreshToken refreshToken) =>
|
||||
await AddAsync(refreshToken);
|
||||
|
||||
public async Task DeleteRefreshTokenAsync(Guid refreshTokenId) =>
|
||||
await DeleteAsync<AccountRefreshToken>(x => x.Token == refreshTokenId);
|
||||
|
||||
public async Task DeleteUserRefreshTokensAsync(long id)
|
||||
{
|
||||
IQueryable<AccountRefreshToken> tokens = Database.AccountRefreshTokens.Where(x => x.AccountId == id);
|
||||
Database.AccountRefreshTokens.AttachRange(tokens);
|
||||
Database.AccountRefreshTokens.RemoveRange(tokens);
|
||||
await Database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
33
WatchIt.WebAPI/Repositories/Accounts/IAccountsRepository.cs
Normal file
33
WatchIt.WebAPI/Repositories/Accounts/IAccountsRepository.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts.Account;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Accounts;
|
||||
|
||||
public interface IAccountsRepository : IRepository<Account>
|
||||
{
|
||||
Task<bool> ExistsAsync(long id);
|
||||
Task<Account?> GetAsync(long id, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null);
|
||||
Task<Account?> GetByUsernameOrEmailAsync(string usernameOrEmail, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null);
|
||||
Task<IEnumerable<Account>> GetAllAsync(AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null);
|
||||
|
||||
Task<AccountProfilePicture?> GetProfilePictureAsync(long id, Func<IQueryable<AccountProfilePicture>, IQueryable<AccountProfilePicture>>? additionalIncludes = null);
|
||||
Task<AccountProfilePicture> UpdateOrAddProfilePictureAsync(long id, Func<AccountProfilePicture> addFunc, Action<AccountProfilePicture> updateFunc);
|
||||
Task DeleteProfilePictureAsync(long id);
|
||||
|
||||
Task<bool> FollowExistsAsync(long followerId, long followedId);
|
||||
Task<IEnumerable<Account>> GetFollowsAsync(long id, AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null);
|
||||
Task<IEnumerable<Account>> GetFollowersAsync(long id, AccountFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Account>, IQueryable<Account>>? additionalIncludes = null);
|
||||
Task AddFollowAsync(AccountFollow entity);
|
||||
Task DeleteFollowAsync(long followerId, long followedId);
|
||||
|
||||
Task<AccountBackgroundPicture?> GetBackgroundPictureAsync(long id, Func<IQueryable<AccountBackgroundPicture>, IQueryable<AccountBackgroundPicture>>? additionalIncludes = null);
|
||||
Task<AccountBackgroundPicture> UpdateOrAddBackgroundPictureAsync(long id, Func<AccountBackgroundPicture> addFunc, Action<AccountBackgroundPicture> updateFunc);
|
||||
Task DeleteBackgroundPictureAsync(long id);
|
||||
|
||||
Task UpdateRefreshTokenAsync(AccountRefreshToken refreshToken, Action<AccountRefreshToken> updateFunc);
|
||||
Task AddRefreshTokenAsync(AccountRefreshToken refreshToken);
|
||||
Task DeleteRefreshTokenAsync(Guid refreshTokenId);
|
||||
Task DeleteUserRefreshTokensAsync(long id);
|
||||
}
|
||||
36
WatchIt.WebAPI/Repositories/Genders/GendersRepository.cs
Normal file
36
WatchIt.WebAPI/Repositories/Genders/GendersRepository.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Genders;
|
||||
using WatchIt.DTO.Models.Controllers.Genders.Gender;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Genders;
|
||||
|
||||
public class GendersRepository : Repository<Gender>, IGendersRepository
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public GendersRepository(DatabaseContext database) : base(database) {}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<bool> ExistsAsync(short id) =>
|
||||
await DefaultSet.AnyAsync(x => x.Id == id);
|
||||
|
||||
public async Task<Gender?> GetAsync(short id, Func<IQueryable<Gender>, IQueryable<Gender>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<IEnumerable<Gender>> GetAllAsync(GenderFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Gender>, IQueryable<Gender>>? additionalIncludes = null) =>
|
||||
await DefaultSet.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, GenderOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
#endregion
|
||||
}
|
||||
12
WatchIt.WebAPI/Repositories/Genders/IGendersRepository.cs
Normal file
12
WatchIt.WebAPI/Repositories/Genders/IGendersRepository.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using WatchIt.Database.Model.Genders;
|
||||
using WatchIt.DTO.Models.Controllers.Genders.Gender;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Genders;
|
||||
|
||||
public interface IGendersRepository : IRepository<Gender>
|
||||
{
|
||||
Task<bool> ExistsAsync(short id);
|
||||
Task<Gender?> GetAsync(short id, Func<IQueryable<Gender>, IQueryable<Gender>>? additionalIncludes = null);
|
||||
Task<IEnumerable<Gender>> GetAllAsync(GenderFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Gender>, IQueryable<Gender>>? additionalIncludes = null);
|
||||
}
|
||||
36
WatchIt.WebAPI/Repositories/Genres/GenresRepository.cs
Normal file
36
WatchIt.WebAPI/Repositories/Genres/GenresRepository.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Genres;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Genres;
|
||||
|
||||
public class GenresRepository : Repository<Genre>, IGenresRepository
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public GenresRepository(DatabaseContext database) : base(database) {}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<bool> ExistsAsync(short id) =>
|
||||
await DefaultSet.AnyAsync(x => x.Id == id);
|
||||
|
||||
public async Task<Genre?> GetAsync(short id, Func<IQueryable<Genre>, IQueryable<Genre>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<IEnumerable<Genre>> GetAllAsync(GenreFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Genre>, IQueryable<Genre>>? additionalIncludes = null) =>
|
||||
await DefaultSet.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, GenreOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
#endregion
|
||||
}
|
||||
12
WatchIt.WebAPI/Repositories/Genres/IGenresRepository.cs
Normal file
12
WatchIt.WebAPI/Repositories/Genres/IGenresRepository.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using WatchIt.Database.Model.Genres;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Genres;
|
||||
|
||||
public interface IGenresRepository : IRepository<Genre>
|
||||
{
|
||||
Task<bool> ExistsAsync(short id);
|
||||
Task<Genre?> GetAsync(short id, Func<IQueryable<Genre>, IQueryable<Genre>>? additionalIncludes = null);
|
||||
Task<IEnumerable<Genre>> GetAllAsync(GenreFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Genre>, IQueryable<Genre>>? additionalIncludes = null);
|
||||
}
|
||||
15
WatchIt.WebAPI/Repositories/IRepository.cs
Normal file
15
WatchIt.WebAPI/Repositories/IRepository.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories;
|
||||
|
||||
public interface IRepository<T> where T : class
|
||||
{
|
||||
public Task<IEnumerable<T>> GetAllAsync(Func<IQueryable<T>, IQueryable<T>>? additionalIncludes = null);
|
||||
public Task UpdateAsync(T entity, Action<T> updateFunc);
|
||||
public Task AddAsync(T entity);
|
||||
public Task<T> UpdateOrAddAsync(T? entity, Func<T> addFunc, Action<T> updateFunc);
|
||||
public Task DeleteAsync(T entity);
|
||||
public Task DeleteAsync(Expression<Func<T, bool>> predicate);
|
||||
}
|
||||
35
WatchIt.WebAPI/Repositories/Media/IMediaRepository.cs
Normal file
35
WatchIt.WebAPI/Repositories/Media/IMediaRepository.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using WatchIt.Database.Model.Genres;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Query;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Media;
|
||||
|
||||
public interface IMediaRepository : IRepository<Medium>
|
||||
{
|
||||
Task<bool> ExistsAsync(long id);
|
||||
Task<Medium?> GetAsync(long id, Func<IQueryable<Medium>, IQueryable<Medium>>? additionalIncludes = null);
|
||||
Task<T?> GetAsync<T>(long id, Func<IQueryable<T>, IQueryable<T>>? additionalIncludes = null) where T : Medium;
|
||||
Task<IEnumerable<Medium>> GetAllAsync(MediumFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Medium>, IQueryable<Medium>>? additionalIncludes = null);
|
||||
Task<IEnumerable<MediumMovie>> GetAllMoviesAsync(MediumMovieFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<MediumMovie>, IQueryable<MediumMovie>>? additionalIncludes = null);
|
||||
Task<IEnumerable<MediumSeries>> GetAllSeriesAsync(MediumSeriesFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<MediumSeries>, IQueryable<MediumSeries>>? additionalIncludes = null);
|
||||
Task<bool> UpdateAsync<T>(long id, Action<T> updateFunc) where T : Medium;
|
||||
|
||||
Task<IEnumerable<Genre>> GetMediumGenresAsync(long id, GenreFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Genre>, IQueryable<Genre>>? additionalIncludes = null);
|
||||
Task AddMediumGenreAsync(long mediumId, short genreId);
|
||||
Task DeleteMediumGenreAsync(long mediumId, short genreId);
|
||||
|
||||
Task<IEnumerable<Medium>> GetAllRatedByAccountAsync(long accountId, MediumFilterQuery filterQuery, MediumUserRatedFilterQuery<Medium> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Medium>, IQueryable<Medium>>? additionalIncludes = null);
|
||||
Task<IEnumerable<MediumMovie>> GetAllMoviesRatedByAccountAsync(long accountId, MediumMovieFilterQuery filterQuery, MediumUserRatedFilterQuery<MediumMovie> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<MediumMovie>, IQueryable<MediumMovie>>? additionalIncludes = null);
|
||||
Task<IEnumerable<MediumSeries>> GetAllSeriesRatedByAccountAsync(long accountId, MediumSeriesFilterQuery filterQuery, MediumUserRatedFilterQuery<MediumSeries> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<MediumSeries>, IQueryable<MediumSeries>>? additionalIncludes = null);
|
||||
Task<MediumRating?> GetMediumUserRatingAsync(long mediumId, long accountId, Func<IQueryable<MediumRating>, IQueryable<MediumRating>>? additionalIncludes = null);
|
||||
Task<MediumRating> UpdateOrAddMediumRatingAsync(long mediumId, long accountId, Func<MediumRating> addFunc, Action<MediumRating> updateFunc);
|
||||
Task DeleteMediumUserRatingAsync(long mediumId, long accountId);
|
||||
|
||||
Task<MediumViewCount> UpdateOrAddMediumViewCountAsync(long mediumId, DateOnly date, Func<MediumViewCount> addFunc, Action<MediumViewCount> updateFunc);
|
||||
|
||||
Task<MediumPicture?> GetMediumPictureAsync(long id, Func<IQueryable<MediumPicture>, IQueryable<MediumPicture>>? additionalIncludes = null);
|
||||
Task<MediumPicture> UpdateOrAddMediumPictureAsync(long id, Func<MediumPicture> addFunc, Action<MediumPicture> updateFunc);
|
||||
Task DeleteMediumPictureAsync(long id);
|
||||
}
|
||||
175
WatchIt.WebAPI/Repositories/Media/MediaRepository.cs
Normal file
175
WatchIt.WebAPI/Repositories/Media/MediaRepository.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Genres;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.Database.Model.Photos;
|
||||
using WatchIt.DTO.Models.Controllers.Genres.Genre;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium;
|
||||
using WatchIt.DTO.Models.Controllers.Media.Medium.Query;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Media;
|
||||
|
||||
public class MediaRepository : Repository<Medium>, IMediaRepository
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public MediaRepository(DatabaseContext database) : base(database) {}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<bool> ExistsAsync(long id) =>
|
||||
await DefaultSet.AnyAsync(x => x.Id == id);
|
||||
|
||||
public async Task<Medium?> GetAsync(long id, Func<IQueryable<Medium>, IQueryable<Medium>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<T?> GetAsync<T>(long id, Func<IQueryable<T>, IQueryable<T>>? additionalIncludes = null) where T : Medium =>
|
||||
await DefaultSet.OfType<T>()
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<IEnumerable<Medium>> GetAllAsync(MediumFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Medium>, IQueryable<Medium>>? additionalIncludes = null) =>
|
||||
await DefaultSet.ApplyFilter(filterQuery)
|
||||
.OrderByDescending(x => x.Id)
|
||||
.ApplyOrder(orderQuery, MediumOrderKeys.Base<Medium>(), MediumOrderKeys.Medium)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<IEnumerable<MediumMovie>> GetAllMoviesAsync(MediumMovieFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<MediumMovie>, IQueryable<MediumMovie>>? additionalIncludes = null) =>
|
||||
await DefaultSet.OfType<MediumMovie>()
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, MediumOrderKeys.Base<MediumMovie>(), MediumOrderKeys.MediumMovie)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<IEnumerable<MediumSeries>> GetAllSeriesAsync(MediumSeriesFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<MediumSeries>, IQueryable<MediumSeries>>? additionalIncludes = null) =>
|
||||
await DefaultSet.OfType<MediumSeries>()
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, MediumOrderKeys.Base<MediumSeries>(), MediumOrderKeys.MediumSeries)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<bool> UpdateAsync<T>(long id, Action<T> updateFunc) where T : Medium
|
||||
{
|
||||
T? entity = await GetAsync<T>(id);
|
||||
if (entity is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
updateFunc(entity);
|
||||
DefaultSet.Update(entity);
|
||||
await Database.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Genres
|
||||
|
||||
public async Task<IEnumerable<Genre>> GetMediumGenresAsync(long id, GenreFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Genre>, IQueryable<Genre>>? additionalIncludes = null) =>
|
||||
await Database.Set<MediumGenre>()
|
||||
.Where(x => x.MediumId == id)
|
||||
.Select(x => x.Genre)
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, GenreOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task AddMediumGenreAsync(long mediumId, short genreId)
|
||||
{
|
||||
if (!Database.Set<MediumGenre>().Any(x => x.MediumId == mediumId && x.GenreId == genreId))
|
||||
{
|
||||
await AddAsync(new MediumGenre { MediumId = mediumId, GenreId = genreId });
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteMediumGenreAsync(long mediumId, short genreId) =>
|
||||
await DeleteAsync(new MediumGenre { MediumId = mediumId, GenreId = genreId });
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
public async Task<IEnumerable<Medium>> GetAllRatedByAccountAsync(long accountId, MediumFilterQuery filterQuery, MediumUserRatedFilterQuery<Medium> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Medium>, IQueryable<Medium>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Where(x => x.Ratings
|
||||
.Any(y => y.AccountId == accountId))
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyFilter(userRatedFilterQuery)
|
||||
.ApplyOrder(orderQuery, MediumOrderKeys.Base<Medium>(), MediumOrderKeys.Medium, MediumOrderKeys.MediumUserRated<Medium>(accountId))
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<IEnumerable<MediumMovie>> GetAllMoviesRatedByAccountAsync(long accountId, MediumMovieFilterQuery filterQuery, MediumUserRatedFilterQuery<MediumMovie> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<MediumMovie>, IQueryable<MediumMovie>>? additionalIncludes = null) =>
|
||||
await DefaultSet.OfType<MediumMovie>()
|
||||
.Where(x => x.Ratings
|
||||
.Any(y => y.AccountId == accountId))
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyFilter(userRatedFilterQuery)
|
||||
.ApplyOrder(orderQuery, MediumOrderKeys.Base<MediumMovie>(), MediumOrderKeys.MediumMovie, MediumOrderKeys.MediumUserRated<MediumMovie>(accountId))
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<IEnumerable<MediumSeries>> GetAllSeriesRatedByAccountAsync(long accountId, MediumSeriesFilterQuery filterQuery, MediumUserRatedFilterQuery<MediumSeries> userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<MediumSeries>, IQueryable<MediumSeries>>? additionalIncludes = null) =>
|
||||
await DefaultSet.OfType<MediumSeries>()
|
||||
.Where(x => x.Ratings
|
||||
.Any(y => y.AccountId == accountId))
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyFilter(userRatedFilterQuery)
|
||||
.ApplyOrder(orderQuery, MediumOrderKeys.Base<MediumSeries>(), MediumOrderKeys.MediumSeries, MediumOrderKeys.MediumUserRated<MediumSeries>(accountId))
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<MediumRating?> GetMediumUserRatingAsync(long mediumId, long accountId, Func<IQueryable<MediumRating>, IQueryable<MediumRating>>? additionalIncludes = null) =>
|
||||
await Database.Set<MediumRating>()
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.MediumId == mediumId && x.AccountId == accountId);
|
||||
|
||||
public async Task<MediumRating> UpdateOrAddMediumRatingAsync(long mediumId, long accountId, Func<MediumRating> addFunc, Action<MediumRating> updateFunc) =>
|
||||
await UpdateOrAddAsync(await GetMediumUserRatingAsync(mediumId, accountId), addFunc, updateFunc);
|
||||
|
||||
public async Task DeleteMediumUserRatingAsync(long mediumId, long accountId) =>
|
||||
await DeleteAsync<MediumRating>(x => x.MediumId == mediumId && x.AccountId == accountId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
public async Task<MediumViewCount> UpdateOrAddMediumViewCountAsync(long mediumId, DateOnly date, Func<MediumViewCount> addFunc, Action<MediumViewCount> updateFunc) =>
|
||||
await UpdateOrAddAsync(await Database.Set<MediumViewCount>()
|
||||
.FirstOrDefaultAsync(x => x.MediumId == mediumId && x.Date == date), addFunc, updateFunc);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Picture
|
||||
|
||||
public async Task<MediumPicture?> GetMediumPictureAsync(long id, Func<IQueryable<MediumPicture>, IQueryable<MediumPicture>>? additionalIncludes = null) =>
|
||||
await Database.MediumPictures
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.MediumId == id);
|
||||
|
||||
public async Task<MediumPicture> UpdateOrAddMediumPictureAsync(long id, Func<MediumPicture> addFunc, Action<MediumPicture> updateFunc) =>
|
||||
await UpdateOrAddAsync(await GetMediumPictureAsync(id), addFunc, updateFunc);
|
||||
|
||||
public async Task DeleteMediumPictureAsync(long id) =>
|
||||
await DeleteAsync(new MediumPicture { MediumId = id });
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
22
WatchIt.WebAPI/Repositories/People/IPeopleRepository.cs
Normal file
22
WatchIt.WebAPI/Repositories/People/IPeopleRepository.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using WatchIt.Database.Model.People;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person.Query;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.People;
|
||||
|
||||
public interface IPeopleRepository : IRepository<Person>
|
||||
{
|
||||
Task<bool> ExistsAsync(long id);
|
||||
Task<Person?> GetAsync(long id, Func<IQueryable<Person>, IQueryable<Person>>? additionalIncludes = null);
|
||||
Task<IEnumerable<Person>> GetAllAsync(PersonFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Person>, IQueryable<Person>>? additionalIncludes = null);
|
||||
Task<bool> UpdateAsync(long id, Action<Person> updateFunc, Func<IQueryable<Person>, IQueryable<Person>>? additionalIncludes = null);
|
||||
Task DeleteAsync(long id);
|
||||
|
||||
Task<PersonViewCount> UpdateOrAddPersonViewCountAsync(long personId, DateOnly date, Func<PersonViewCount> addFunc, Action<PersonViewCount> updateFunc);
|
||||
|
||||
Task<PersonPicture?> GetPersonPictureAsync(long id, Func<IQueryable<PersonPicture>, IQueryable<PersonPicture>>? additionalIncludes = null);
|
||||
Task<PersonPicture> UpdateOrAddPersonPictureAsync(long id, Func<PersonPicture> addFunc, Action<PersonPicture> updateFunc);
|
||||
Task DeletePersonPictureAsync(long id);
|
||||
|
||||
Task<IEnumerable<Person>> GetAllRatedByAccountAsync(long accountId, PersonFilterQuery filterQuery, PersonUserRatedFilterQuery userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Person>, IQueryable<Person>>? additionalIncludes = null);
|
||||
}
|
||||
94
WatchIt.WebAPI/Repositories/People/PeopleRepository.cs
Normal file
94
WatchIt.WebAPI/Repositories/People/PeopleRepository.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.People;
|
||||
using WatchIt.Database.Model.Roles;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person;
|
||||
using WatchIt.DTO.Models.Controllers.People.Person.Query;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.People;
|
||||
|
||||
public class PeopleRepository : Repository<Person>, IPeopleRepository
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public PeopleRepository(DatabaseContext database) : base(database) {}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<bool> ExistsAsync(long id) =>
|
||||
await DefaultSet.AnyAsync(x => x.Id == id);
|
||||
|
||||
public async Task<Person?> GetAsync(long id, Func<IQueryable<Person>, IQueryable<Person>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<IEnumerable<Person>> GetAllAsync(PersonFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Person>, IQueryable<Person>>? additionalIncludes = null) =>
|
||||
await DefaultSet.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, PersonOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<bool> UpdateAsync(long id, Action<Person> updateFunc, Func<IQueryable<Person>, IQueryable<Person>>? additionalIncludes = null)
|
||||
{
|
||||
Person? person = await GetAsync(id, additionalIncludes);
|
||||
if (person is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
await UpdateAsync(person, updateFunc);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(long id) =>
|
||||
await DeleteAsync<Person>(new Person { Id = id });
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
public async Task<PersonViewCount> UpdateOrAddPersonViewCountAsync(long personId, DateOnly date, Func<PersonViewCount> addFunc, Action<PersonViewCount> updateFunc) =>
|
||||
await UpdateOrAddAsync(await Database.PersonViewCounts
|
||||
.FirstOrDefaultAsync(x => x.PersonId == personId && x.Date == date), addFunc, updateFunc);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Picture
|
||||
|
||||
public async Task<PersonPicture?> GetPersonPictureAsync(long id, Func<IQueryable<PersonPicture>, IQueryable<PersonPicture>>? additionalIncludes = null) =>
|
||||
await Database.PersonPictures
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.PersonId == id);
|
||||
|
||||
public async Task<PersonPicture> UpdateOrAddPersonPictureAsync(long id, Func<PersonPicture> addFunc, Action<PersonPicture> updateFunc) =>
|
||||
await UpdateOrAddAsync(await GetPersonPictureAsync(id), addFunc, updateFunc);
|
||||
|
||||
public async Task DeletePersonPictureAsync(long id) =>
|
||||
await DeleteAsync(new PersonPicture { PersonId = id });
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
public async Task<IEnumerable<Person>> GetAllRatedByAccountAsync(long accountId, PersonFilterQuery filterQuery, PersonUserRatedFilterQuery userRatedFilterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Person>, IQueryable<Person>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Where(x => x.Roles
|
||||
.SelectMany(y => y.Ratings)
|
||||
.Any(y => y.AccountId == accountId))
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyFilter(userRatedFilterQuery)
|
||||
.ApplyOrder(orderQuery, PersonOrderKeys.Base, PersonOrderKeys.UserRated(accountId))
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
21
WatchIt.WebAPI/Repositories/Photos/IPhotosRepository.cs
Normal file
21
WatchIt.WebAPI/Repositories/Photos/IPhotosRepository.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using WatchIt.Database.Model.Photos;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Photos;
|
||||
|
||||
public interface IPhotosRepository : IRepository<Photo>
|
||||
{
|
||||
Task<bool> ExistsAsync(Guid id);
|
||||
Task<Photo?> GetAsync(Guid id, Func<IQueryable<Photo>, IQueryable<Photo>>? additionalIncludes = null);
|
||||
Task<IEnumerable<Photo>> GetAllAsync(PhotoFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Photo>, IQueryable<Photo>>? additionalIncludes = null);
|
||||
Task<Photo?> GetPhotoRandomBackgroundAsync(Func<IQueryable<Photo>, IQueryable<Photo>>? additionalIncludes = null);
|
||||
Task<bool> UpdateAsync(Guid id, Action<Photo> updateFunc);
|
||||
Task DeleteAsync(Guid id);
|
||||
|
||||
Task AddPhotoBackgroundAsync(PhotoBackground photoBackground);
|
||||
Task<PhotoBackground> UpdateOrAddPhotoBackgroundAsync(Guid photoId, Func<PhotoBackground> addFunc, Action<PhotoBackground> updateFunc);
|
||||
Task DeletePhotoBackgroundAsync(Guid photoId);
|
||||
|
||||
Task<Photo> GetPhotoRandomBackgroundByMediumAsync(long mediumId, Func<IQueryable<Photo>, IQueryable<Photo>>? additionalIncludes = null);
|
||||
}
|
||||
112
WatchIt.WebAPI/Repositories/Photos/PhotosRepository.cs
Normal file
112
WatchIt.WebAPI/Repositories/Photos/PhotosRepository.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Photos;
|
||||
using WatchIt.DTO.Models.Controllers.Photos.Photo;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Photos;
|
||||
|
||||
public class PhotosRepository : Repository<Photo>, IPhotosRepository
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public PhotosRepository(DatabaseContext database) : base(database) {}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<bool> ExistsAsync(Guid id) =>
|
||||
await DefaultSet.AnyAsync(x => x.Id == id);
|
||||
|
||||
public async Task<Photo?> GetAsync(Guid id, Func<IQueryable<Photo>, IQueryable<Photo>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<IEnumerable<Photo>> GetAllAsync(PhotoFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<Photo>, IQueryable<Photo>>? additionalIncludes = null) =>
|
||||
await DefaultSet.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, PhotoOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<Photo?> GetPhotoRandomBackgroundAsync(Func<IQueryable<Photo>, IQueryable<Photo>>? additionalIncludes = null)
|
||||
{
|
||||
IQueryable<Photo> query = DefaultSet.Where(x => x.Background != null && x.Background.IsUniversal);
|
||||
int count = await query.CountAsync();
|
||||
int randomIndex = Random.Shared.Next(count);
|
||||
return await query.Include(additionalIncludes)
|
||||
.ElementAtAsync(randomIndex);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(Guid id, Action<Photo> updateFunc)
|
||||
{
|
||||
Photo? entity = await GetAsync(id);
|
||||
if (entity is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
updateFunc(entity);
|
||||
DefaultSet.Update(entity);
|
||||
await Database.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id)
|
||||
{
|
||||
await DeleteAsync<PhotoBackground>(x => x.PhotoId == id);
|
||||
await DeleteAsync(x => x.Id == id);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Background
|
||||
|
||||
public async Task AddPhotoBackgroundAsync(PhotoBackground photoBackground)
|
||||
{
|
||||
await Database.PhotoBackgrounds.AddAsync(photoBackground);
|
||||
await Database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<PhotoBackground> UpdateOrAddPhotoBackgroundAsync(Guid photoId, Func<PhotoBackground> addFunc, Action<PhotoBackground> updateFunc)
|
||||
{
|
||||
PhotoBackground? entity = await Database.PhotoBackgrounds.FirstOrDefaultAsync(x => x.PhotoId == photoId);
|
||||
if (entity is null)
|
||||
{
|
||||
entity = addFunc();
|
||||
await Database.PhotoBackgrounds.AddAsync(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
updateFunc(entity);
|
||||
Database.PhotoBackgrounds.Update(entity);
|
||||
}
|
||||
await Database.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public async Task DeletePhotoBackgroundAsync(Guid photoId) =>
|
||||
await DeleteAsync<PhotoBackground>(x => x.PhotoId == photoId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Medium
|
||||
|
||||
public async Task<Photo> GetPhotoRandomBackgroundByMediumAsync(long mediumId, Func<IQueryable<Photo>, IQueryable<Photo>>? additionalIncludes = null)
|
||||
{
|
||||
IQueryable<Photo> query = DefaultSet.Where(x => x.MediumId == mediumId && x.Background != null);
|
||||
int count = await query.CountAsync();
|
||||
int randomIndex = Random.Shared.Next(count);
|
||||
return await query.Include(additionalIncludes)
|
||||
.ElementAtAsync(randomIndex);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
16
WatchIt.WebAPI/Repositories/QueryableExtensions.cs
Normal file
16
WatchIt.WebAPI/Repositories/QueryableExtensions.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories;
|
||||
|
||||
public static class QueryableExtensions
|
||||
{
|
||||
internal static IQueryable<T> Include<T>(this IQueryable<T> queryable, Func<IQueryable<T>, IQueryable<T>>? additionalIncludes = null)
|
||||
{
|
||||
if (additionalIncludes is not null)
|
||||
{
|
||||
queryable = additionalIncludes(queryable);
|
||||
}
|
||||
return queryable;
|
||||
}
|
||||
}
|
||||
118
WatchIt.WebAPI/Repositories/Repository.cs
Normal file
118
WatchIt.WebAPI/Repositories/Repository.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories;
|
||||
|
||||
|
||||
public abstract class Repository<T> : IRepository<T> where T : class
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
protected readonly DatabaseContext Database;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region FIELDS
|
||||
|
||||
protected readonly DbSet<T> DefaultSet;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
protected Repository(DatabaseContext database)
|
||||
{
|
||||
Database = database;
|
||||
DefaultSet = database.Set<T>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<IEnumerable<T>> GetAllAsync(Func<IQueryable<T>, IQueryable<T>>? additionalIncludes = null) =>
|
||||
await GetAllAsync<T>(additionalIncludes);
|
||||
|
||||
public async Task UpdateAsync(T entity, Action<T> updateFunc) =>
|
||||
await UpdateAsync<T>(entity, updateFunc);
|
||||
|
||||
public async Task AddAsync(T entity) =>
|
||||
await AddAsync<T>(entity);
|
||||
|
||||
public async Task<T> UpdateOrAddAsync(T? entity, Func<T> addFunc, Action<T> updateFunc) =>
|
||||
await UpdateOrAddAsync<T>(entity, addFunc, updateFunc);
|
||||
|
||||
public async Task DeleteAsync(T entity) =>
|
||||
await DeleteAsync<T>(entity);
|
||||
|
||||
public async Task DeleteAsync(Expression<Func<T, bool>> predicate) =>
|
||||
await DeleteAsync<T>(predicate);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
protected async Task<IEnumerable<TEntity>> GetAllAsync<TEntity>(Func<IQueryable<TEntity>, IQueryable<TEntity>>? additionalIncludes = null) where TEntity : class =>
|
||||
await Database.Set<TEntity>()
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
protected async Task UpdateAsync<TEntity>(TEntity entity, Action<TEntity> updateFunc) where TEntity : class
|
||||
{
|
||||
updateFunc(entity);
|
||||
Database.Set<TEntity>().Update(entity);
|
||||
await Database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
protected async Task AddAsync<TEntity>(TEntity entity) where TEntity : class
|
||||
{
|
||||
await Database.Set<TEntity>().AddAsync(entity);
|
||||
await Database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
protected async Task<TEntity> UpdateOrAddAsync<TEntity>(TEntity? entity, Func<TEntity> addFunc, Action<TEntity> updateFunc) where TEntity : class
|
||||
{
|
||||
if (entity is null)
|
||||
{
|
||||
entity = addFunc();
|
||||
await Database.Set<TEntity>().AddAsync(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
updateFunc(entity);
|
||||
Database.Set<TEntity>().Update(entity);
|
||||
}
|
||||
await Database.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
protected async Task DeleteAsync<TEntity>(TEntity entity) where TEntity : class
|
||||
{
|
||||
DbSet<TEntity> dbSet = Database.Set<TEntity>();
|
||||
dbSet.Attach(entity);
|
||||
dbSet.Remove(entity);
|
||||
await Database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
protected async Task DeleteAsync<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
|
||||
{
|
||||
DbSet<TEntity> dbSet = Database.Set<TEntity>();
|
||||
IQueryable<TEntity> entities = dbSet.Where(predicate);
|
||||
dbSet.AttachRange(entities);
|
||||
dbSet.RemoveRange(entities);
|
||||
await Database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
38
WatchIt.WebAPI/Repositories/Roles/IRolesRepository.cs
Normal file
38
WatchIt.WebAPI/Repositories/Roles/IRolesRepository.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Linq.Expressions;
|
||||
using WatchIt.Database.Model.Roles;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleActorType;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleCreatorType;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Roles;
|
||||
|
||||
public interface IRolesRepository : IRepository<Role>
|
||||
{
|
||||
Task<bool> ExistsAsync(Guid id);
|
||||
Task<Role?> GetAsync(Guid id, Func<IQueryable<Role>, IQueryable<Role>>? additionalIncludes = null);
|
||||
Task<T?> GetAsync<T>(Guid id, Func<IQueryable<T>, IQueryable<T>>? additionalIncludes = null) where T : Role;
|
||||
Task<IEnumerable<RoleActor>> GetAllActorsAsync(RoleActorFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<RoleActor>, IQueryable<RoleActor>>? additionalIncludes = null);
|
||||
Task<IEnumerable<RoleCreator>> GetAllCreatorsAsync(RoleCreatorFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<RoleCreator>, IQueryable<RoleCreator>>? additionalIncludes = null);
|
||||
Task<bool> UpdateAsync<T>(Guid id, Action<T> updateFunc) where T : Role;
|
||||
Task DeleteAsync(Guid id);
|
||||
|
||||
Task<IEnumerable<RoleRating>> GetRoleRatingsAsync(Guid roleId, Func<IQueryable<RoleRating>, IQueryable<RoleRating>>? additionalIncludes = null);
|
||||
Task<RoleRating?> GetRoleRatingByUserAsync(Guid roleId, long accountId, Func<IQueryable<RoleRating>, IQueryable<RoleRating>>? additionalIncludes = null);
|
||||
Task<IEnumerable<RoleRating>> GetPersonRoleRatingsAsync(long personId, Func<IQueryable<RoleRating>, IQueryable<RoleRating>>? additionalIncludes = null);
|
||||
Task<IEnumerable<RoleRating>> GetPersonRoleRatingsByAccountIdAsync(long personId, long accountId, Func<IQueryable<RoleRating>, IQueryable<RoleRating>>? additionalIncludes = null);
|
||||
Task<RoleRating> UpdateOrAddRoleRatingAsync(Guid roleId, long accountId, Func<RoleRating> addFunc, Action<RoleRating> updateFunc);
|
||||
Task DeleteRoleUserRatingAsync(Guid roleId, long accountId);
|
||||
|
||||
Task<bool> ExistsActorTypeAsync(short id);
|
||||
Task<RoleActorType?> GetActorTypeAsync(short id, Func<IQueryable<RoleActorType>, IQueryable<RoleActorType>>? additionalIncludes = null);
|
||||
Task<IEnumerable<RoleActorType>> GetAllActorTypesAsync(RoleActorTypeFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<RoleActorType>, IQueryable<RoleActorType>>? additionalIncludes = null);
|
||||
Task AddActorTypeAsync(RoleActorType actorType);
|
||||
Task DeleteActorTypeAsync(short id);
|
||||
|
||||
Task<bool> ExistsCreatorTypeAsync(short id);
|
||||
Task<RoleCreatorType?> GetCreatorTypeAsync(short id, Func<IQueryable<RoleCreatorType>, IQueryable<RoleCreatorType>>? additionalIncludes = null);
|
||||
Task<IEnumerable<RoleCreatorType>> GetAllCreatorTypesAsync(RoleCreatorTypeFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<RoleCreatorType>, IQueryable<RoleCreatorType>>? additionalIncludes = null);
|
||||
Task AddCreatorTypeAsync(RoleCreatorType creatorType);
|
||||
Task DeleteCreatorTypeAsync(short id);
|
||||
}
|
||||
161
WatchIt.WebAPI/Repositories/Roles/RolesRepository.cs
Normal file
161
WatchIt.WebAPI/Repositories/Roles/RolesRepository.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Roles;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.Role.Query;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleActorType;
|
||||
using WatchIt.DTO.Models.Controllers.Roles.RoleCreatorType;
|
||||
using WatchIt.DTO.Query;
|
||||
|
||||
namespace WatchIt.WebAPI.Repositories.Roles;
|
||||
|
||||
public class RolesRepository : Repository<Role>, IRolesRepository
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RolesRepository(DatabaseContext database) : base(database) {}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<bool> ExistsAsync(Guid id) =>
|
||||
await DefaultSet.AnyAsync(x => x.Id == id);
|
||||
|
||||
public async Task<Role?> GetAsync(Guid id, Func<IQueryable<Role>, IQueryable<Role>>? additionalIncludes = null) =>
|
||||
await DefaultSet.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<T?> GetAsync<T>(Guid id, Func<IQueryable<T>, IQueryable<T>>? additionalIncludes = null) where T : Role =>
|
||||
await DefaultSet.OfType<T>()
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<IEnumerable<RoleActor>> GetAllActorsAsync(RoleActorFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<RoleActor>, IQueryable<RoleActor>>? additionalIncludes = null) =>
|
||||
await DefaultSet.OfType<RoleActor>()
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, RoleOrderKeys.Base<RoleActor>(), RoleOrderKeys.RoleActor)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<IEnumerable<RoleCreator>> GetAllCreatorsAsync(RoleCreatorFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<RoleCreator>, IQueryable<RoleCreator>>? additionalIncludes = null) =>
|
||||
await DefaultSet.OfType<RoleCreator>()
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, RoleOrderKeys.Base<RoleCreator>(), RoleOrderKeys.RoleCreator)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<bool> UpdateAsync<T>(Guid id, Action<T> updateFunc) where T : Role
|
||||
{
|
||||
T? entity = await GetAsync<T>(id);
|
||||
if (entity is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
updateFunc(entity);
|
||||
DefaultSet.Update(entity);
|
||||
await Database.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id) =>
|
||||
await DeleteAsync(x => x.Id == id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
public async Task<IEnumerable<RoleRating>> GetRoleRatingsAsync(Guid roleId, Func<IQueryable<RoleRating>, IQueryable<RoleRating>>? additionalIncludes = null) =>
|
||||
await Database.RoleRatings
|
||||
.Where(x => x.RoleId == roleId)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<RoleRating?> GetRoleRatingByUserAsync(Guid roleId, long accountId, Func<IQueryable<RoleRating>, IQueryable<RoleRating>>? additionalIncludes = null) =>
|
||||
await Database.RoleRatings
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.RoleId == roleId && x.AccountId == accountId);
|
||||
|
||||
public async Task<IEnumerable<RoleRating>> GetPersonRoleRatingsAsync(long personId, Func<IQueryable<RoleRating>, IQueryable<RoleRating>>? additionalIncludes = null) =>
|
||||
await Database.RoleRatings
|
||||
.Where(x => x.Role.PersonId == personId)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<IEnumerable<RoleRating>> GetPersonRoleRatingsByAccountIdAsync(long personId, long accountId, Func<IQueryable<RoleRating>, IQueryable<RoleRating>>? additionalIncludes = null) =>
|
||||
await Database.RoleRatings
|
||||
.Where(x => x.Role.PersonId == personId && x.AccountId == accountId)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<RoleRating> UpdateOrAddRoleRatingAsync(Guid roleId, long accountId, Func<RoleRating> addFunc, Action<RoleRating> updateFunc) =>
|
||||
await UpdateOrAddAsync(await GetRoleRatingByUserAsync(roleId, accountId), addFunc, updateFunc);
|
||||
|
||||
public async Task DeleteRoleUserRatingAsync(Guid roleId, long accountId) =>
|
||||
await DeleteAsync<RoleRating>(x => x.RoleId == roleId && x.AccountId == accountId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region ActorTypes
|
||||
|
||||
public async Task<bool> ExistsActorTypeAsync(short id) =>
|
||||
await Database.RoleActorTypes
|
||||
.AnyAsync(x => x.Id == id);
|
||||
|
||||
public async Task<RoleActorType?> GetActorTypeAsync(short id, Func<IQueryable<RoleActorType>, IQueryable<RoleActorType>>? additionalIncludes = null) =>
|
||||
await Database.RoleActorTypes
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<IEnumerable<RoleActorType>> GetAllActorTypesAsync(RoleActorTypeFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<RoleActorType>, IQueryable<RoleActorType>>? additionalIncludes = null) =>
|
||||
await Database.RoleActorTypes
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, RoleActorTypeOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task AddActorTypeAsync(RoleActorType actorType) =>
|
||||
await AddAsync(actorType);
|
||||
|
||||
public async Task DeleteActorTypeAsync(short id) =>
|
||||
await DeleteAsync(new RoleActorType { Id = id });
|
||||
|
||||
#endregion
|
||||
|
||||
#region CreatorTypes
|
||||
|
||||
public async Task<bool> ExistsCreatorTypeAsync(short id) =>
|
||||
await Database.RoleCreatorTypes
|
||||
.AnyAsync(x => x.Id == id);
|
||||
|
||||
public async Task<RoleCreatorType?> GetCreatorTypeAsync(short id, Func<IQueryable<RoleCreatorType>, IQueryable<RoleCreatorType>>? additionalIncludes = null) =>
|
||||
await Database.RoleCreatorTypes
|
||||
.Include(additionalIncludes)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
public async Task<IEnumerable<RoleCreatorType>> GetAllCreatorTypesAsync(RoleCreatorTypeFilterQuery filterQuery, OrderQuery orderQuery, PagingQuery pagingQuery, Func<IQueryable<RoleCreatorType>, IQueryable<RoleCreatorType>>? additionalIncludes = null) =>
|
||||
await Database.RoleCreatorTypes
|
||||
.ApplyFilter(filterQuery)
|
||||
.ApplyOrder(orderQuery, RoleCreatorTypeOrderKeys.Base)
|
||||
.ApplyPaging(pagingQuery)
|
||||
.Include(additionalIncludes)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task AddCreatorTypeAsync(RoleCreatorType creatorType) =>
|
||||
await AddAsync(creatorType);
|
||||
|
||||
public async Task DeleteCreatorTypeAsync(short id) =>
|
||||
await DeleteAsync(new RoleCreatorType { Id = id });
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
14
WatchIt.WebAPI/Services/Tokens/Configuration/JWT.cs
Normal file
14
WatchIt.WebAPI/Services/Tokens/Configuration/JWT.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace WatchIt.WebAPI.Services.Tokens.Configuration;
|
||||
|
||||
public class JWT
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public string Key { get; set; } = null!;
|
||||
public string Issuer { get; set; } = null!;
|
||||
public string Audience { get; set; } = null!;
|
||||
public string Algorithm { get; set; } = null!;
|
||||
public TokensLifetime Lifetime { get; set; } = null!;
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace WatchIt.WebAPI.Services.Tokens.Configuration;
|
||||
|
||||
public class TokenLifetime
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public int Normal { get; set; }
|
||||
public int? Extended { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace WatchIt.WebAPI.Services.Tokens.Configuration;
|
||||
|
||||
public class TokensLifetime
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public TokenLifetime AccessToken { get; set; } = null!;
|
||||
public TokenLifetime RefreshToken { get; set; } = null!;
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Tokens.Exceptions;
|
||||
namespace WatchIt.WebAPI.Services.Tokens.Exceptions;
|
||||
|
||||
public class TokenNotExtendableException : Exception
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Tokens.Exceptions;
|
||||
namespace WatchIt.WebAPI.Services.Tokens.Exceptions;
|
||||
|
||||
public class TokenNotFoundException : Exception
|
||||
{
|
||||
13
WatchIt.WebAPI/Services/Tokens/ITokensService.cs
Normal file
13
WatchIt.WebAPI/Services/Tokens/ITokensService.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Tokens;
|
||||
|
||||
public interface ITokensService
|
||||
{
|
||||
string CreateAccessToken(Account account);
|
||||
Task<string> CreateRefreshTokenAsync(Account account, bool isExtendable);
|
||||
Task<Account> ExtendRefreshTokenAsync(string refreshToken, string accessToken);
|
||||
Task RevokeRefreshTokenAsync(string stringToken);
|
||||
Task RevokeRefreshTokenAsync(Guid token);
|
||||
Task RevokeAccountRefreshTokensAsync(Account account);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Tokens;
|
||||
|
||||
public static class SecurityTokenDescriptorExtensions
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public static string ToJwtString(this SecurityTokenDescriptor tokenDescriptor)
|
||||
{
|
||||
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
|
||||
handler.InboundClaimTypeMap.Clear();
|
||||
|
||||
SecurityToken token = handler.CreateToken(tokenDescriptor);
|
||||
|
||||
return handler.WriteToken(token);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
160
WatchIt.WebAPI/Services/Tokens/TokensService.cs
Normal file
160
WatchIt.WebAPI/Services/Tokens/TokensService.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
using WatchIt.DTO.Models.Controllers.Accounts;
|
||||
using WatchIt.WebAPI.Constants;
|
||||
using WatchIt.WebAPI.Repositories.Accounts;
|
||||
using WatchIt.WebAPI.Services.Tokens.Configuration;
|
||||
using WatchIt.WebAPI.Services.Tokens.Exceptions;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Tokens;
|
||||
|
||||
public class TokensService : ITokensService
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly JWT _configuration;
|
||||
private readonly IAccountsRepository _accountsRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public TokensService(IConfiguration configuration, IAccountsRepository accountsRepository)
|
||||
{
|
||||
_configuration = configuration.GetSection("Authentication").GetSection("JWT").Get<JWT>()!;
|
||||
_accountsRepository = accountsRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public string CreateAccessToken(Account account)
|
||||
{
|
||||
int lifetime = _configuration.Lifetime.AccessToken.Normal;
|
||||
DateTimeOffset expirationDate = new DateTimeOffset(DateTime.UtcNow.AddMinutes(lifetime));
|
||||
|
||||
SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(
|
||||
[
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Sub, account.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Exp, expirationDate.Ticks.ToString()),
|
||||
new Claim(AdditionalClaimNames.Admin, account.IsAdmin.ToString())
|
||||
]),
|
||||
Issuer = _configuration.Issuer,
|
||||
Audience = _configuration.Audience,
|
||||
Expires = expirationDate.UtcDateTime,
|
||||
SigningCredentials = new SigningCredentials(CreateSecurityKey(), _configuration.Algorithm),
|
||||
};
|
||||
|
||||
return descriptor.ToJwtString();
|
||||
}
|
||||
|
||||
public async Task<string> CreateRefreshTokenAsync(Account account, bool isExtendable)
|
||||
{
|
||||
Guid newToken = Guid.NewGuid();
|
||||
DateTimeOffset expirationDate = GetExpirationDate(_configuration.Lifetime.RefreshToken, isExtendable);
|
||||
|
||||
AccountRefreshToken tokenEntity = AccountsMappers.CreateAccountRefreshTokenEntity(newToken, account.Id, expirationDate, isExtendable);
|
||||
await _accountsRepository.AddRefreshTokenAsync(tokenEntity);
|
||||
|
||||
return Convert.ToBase64String(newToken.ToByteArray());
|
||||
}
|
||||
|
||||
public async Task<Account> ExtendRefreshTokenAsync(string refreshToken, string accessToken)
|
||||
{
|
||||
long accountId = ValidateExpiredAccessTokenAndGetAccountId(accessToken);
|
||||
Account? account = await _accountsRepository.GetAsync(accountId, x => x.Include(y => y.RefreshTokens));
|
||||
if (account is null)
|
||||
{
|
||||
throw new SecurityTokenException("Invalid token");
|
||||
}
|
||||
|
||||
Guid token = new Guid(Convert.FromBase64String(refreshToken));
|
||||
AccountRefreshToken? tokenEntity = account.RefreshTokens.FirstOrDefault(x => x.Token == token);
|
||||
if (tokenEntity is null)
|
||||
{
|
||||
throw new SecurityTokenException("Invalid token");
|
||||
}
|
||||
if (tokenEntity.ExpirationDate < DateTimeOffset.Now)
|
||||
{
|
||||
throw new SecurityTokenExpiredException();
|
||||
}
|
||||
|
||||
DateTimeOffset expirationDate = GetExpirationDate(_configuration.Lifetime.RefreshToken, tokenEntity.IsExtendable);
|
||||
await _accountsRepository.UpdateRefreshTokenAsync(tokenEntity, x => x.UpdateExpirationDate(expirationDate));
|
||||
return account;
|
||||
}
|
||||
|
||||
public async Task RevokeRefreshTokenAsync(string stringToken) =>
|
||||
await RevokeRefreshTokenAsync(new Guid(Convert.FromBase64String(stringToken)));
|
||||
|
||||
public async Task RevokeRefreshTokenAsync(Guid token) =>
|
||||
await _accountsRepository.DeleteRefreshTokenAsync(token);
|
||||
|
||||
public async Task RevokeAccountRefreshTokensAsync(Account account) =>
|
||||
await _accountsRepository.DeleteUserRefreshTokensAsync(account.Id);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private long ValidateExpiredAccessTokenAndGetAccountId(string accessToken)
|
||||
{
|
||||
TokenValidationParameters tokenValidation = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidateAudience = true,
|
||||
ValidateIssuer = true,
|
||||
ValidateLifetime = false,
|
||||
ValidIssuer = _configuration.Issuer,
|
||||
ValidAudience = _configuration.Audience,
|
||||
IssuerSigningKey = CreateSecurityKey(),
|
||||
ClockSkew = TimeSpan.FromMinutes(1),
|
||||
};
|
||||
JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
|
||||
tokenHandler.ValidateToken(accessToken, tokenValidation, out SecurityToken validatedToken);
|
||||
JwtSecurityToken? jwtSecurityToken = validatedToken as JwtSecurityToken;
|
||||
if (jwtSecurityToken is null || !jwtSecurityToken.Header.Alg.Equals(_configuration.Algorithm, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
throw new SecurityTokenException("Invalid token");
|
||||
}
|
||||
|
||||
Claim? sub = jwtSecurityToken.Claims.FirstOrDefault(x => x.Type == JwtRegisteredClaimNames.Sub);
|
||||
if (sub is null || !long.TryParse(sub.Value, out long accountId))
|
||||
{
|
||||
throw new SecurityTokenException("Invalid token");
|
||||
}
|
||||
|
||||
return accountId;
|
||||
}
|
||||
|
||||
private SymmetricSecurityKey CreateSecurityKey()
|
||||
{
|
||||
string stringKey = _configuration.Key;
|
||||
byte[] encodedKey = Encoding.UTF8.GetBytes(stringKey);
|
||||
SymmetricSecurityKey securityKey = new SymmetricSecurityKey(encodedKey);
|
||||
return securityKey;
|
||||
}
|
||||
|
||||
private DateTimeOffset GetExpirationDate(TokenLifetime tokenConfiguration, bool isExtendable = false)
|
||||
{
|
||||
int lifetime = isExtendable ? tokenConfiguration.Extended ?? tokenConfiguration.Normal : tokenConfiguration.Normal;
|
||||
DateTimeOffset expirationDate = DateTimeOffset.UtcNow.AddMinutes(lifetime);
|
||||
return expirationDate;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
8
WatchIt.WebAPI/Services/User/IUserService.cs
Normal file
8
WatchIt.WebAPI/Services/User/IUserService.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.User;
|
||||
|
||||
public interface IUserService
|
||||
{
|
||||
Task<Account> GetAccountAsync();
|
||||
}
|
||||
80
WatchIt.WebAPI/Services/User/UserService.cs
Normal file
80
WatchIt.WebAPI/Services/User/UserService.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using WatchIt.Database.Model.Accounts;
|
||||
using WatchIt.WebAPI.Constants;
|
||||
using WatchIt.WebAPI.Repositories.Accounts;
|
||||
using WatchIt.WebAPI.Services.Tokens;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.User;
|
||||
|
||||
public class UserService : IUserService
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IHttpContextAccessor _accessor;
|
||||
private readonly IAccountsRepository _accountsRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public UserService(IHttpContextAccessor accessor, IAccountsRepository accountsRepository)
|
||||
{
|
||||
_accessor = accessor;
|
||||
_accountsRepository = accountsRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<Account> GetAccountAsync()
|
||||
{
|
||||
long? id = GetAccountId();
|
||||
if (!id.HasValue)
|
||||
{
|
||||
throw new SecurityTokenException("Incorrect sub claim");
|
||||
}
|
||||
|
||||
Account? account = await _accountsRepository.GetAsync(id.Value);
|
||||
if (account is null)
|
||||
{
|
||||
throw new SecurityTokenException("Account with sub claim id not found");
|
||||
}
|
||||
return account;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private ClaimsPrincipal? GetClaims()
|
||||
{
|
||||
if (_accessor.HttpContext is null)
|
||||
{
|
||||
throw new NullReferenceException();
|
||||
}
|
||||
return _accessor.HttpContext.User;
|
||||
}
|
||||
|
||||
private long? GetAccountId()
|
||||
{
|
||||
ClaimsPrincipal? user = GetClaims();
|
||||
Claim? subClaim = user?.FindFirst(JwtRegisteredClaimNames.Sub);
|
||||
if (subClaim is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
long.TryParse(subClaim.Value, out long accountId);
|
||||
return accountId;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,188 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
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;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("accounts")]
|
||||
public class AccountsController(IAccountsControllerService accountsControllerService) : ControllerBase
|
||||
{
|
||||
#region Basic
|
||||
|
||||
[HttpPost("register")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(RegisterResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult> Register([FromBody]RegisterRequest body) => await accountsControllerService.Register(body);
|
||||
|
||||
[HttpPost("authenticate")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(AuthenticateResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> Authenticate([FromBody]AuthenticateRequest body) => await accountsControllerService.Authenticate(body);
|
||||
|
||||
[HttpPost("authenticate_refresh")]
|
||||
[Authorize(AuthenticationSchemes = "refresh")]
|
||||
[ProducesResponseType(typeof(AuthenticateResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> AuthenticateRefresh() => await accountsControllerService.AuthenticateRefresh();
|
||||
|
||||
[HttpDelete("logout")]
|
||||
[Authorize(AuthenticationSchemes = "refresh")]
|
||||
[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)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetAccountProfilePicture([FromRoute(Name = "id")]long id) => await accountsControllerService.GetAccountProfilePicture(id);
|
||||
|
||||
[HttpPut("profile_picture")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(AccountProfilePictureResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> PutAccountProfilePicture([FromBody]AccountProfilePictureRequest body) => await accountsControllerService.PutAccountProfilePicture(body);
|
||||
|
||||
[HttpDelete("profile_picture")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
|
||||
[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
|
||||
|
||||
#region 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> GetAccount([FromRoute]long id) => await accountsControllerService.GetAccount(id);
|
||||
|
||||
[HttpPut("profile_info")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[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);
|
||||
|
||||
[HttpPatch("email")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> PatchAccountEmail([FromBody]AccountEmailRequest data) => await accountsControllerService.PatchAccountEmail(data);
|
||||
|
||||
[HttpPatch("password")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> PatchAccountPassword([FromBody]AccountPasswordRequest data) => await accountsControllerService.PatchAccountPassword(data);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Media
|
||||
|
||||
[HttpGet("{id}/movies")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<MovieRatedResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetAccountRatedMovies([FromRoute]long id, MovieRatedQueryParameters query) => await accountsControllerService.GetAccountRatedMovies(id, query);
|
||||
|
||||
[HttpGet("{id}/series")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<SeriesRatedResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetAccountRatedSeries([FromRoute]long id, SeriesRatedQueryParameters query) => await accountsControllerService.GetAccountRatedSeries(id, query);
|
||||
|
||||
[HttpGet("{id}/persons")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<PersonRatedResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetAccountRatedPersons([FromRoute]long id, PersonRatedQueryParameters query) => await accountsControllerService.GetAccountRatedPersons(id, query);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Follows
|
||||
|
||||
[HttpGet("{id}/follows")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<AccountResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetAccountFollows([FromRoute]long id, AccountQueryParameters query) => await accountsControllerService.GetAccountFollows(id, query);
|
||||
|
||||
[HttpGet("{id}/followers")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<AccountResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetAccountFollowers([FromRoute]long id, AccountQueryParameters query) => await accountsControllerService.GetAccountFollowers(id, query);
|
||||
|
||||
[HttpPost("follows/{user_id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> PostAccountFollow([FromRoute(Name = "user_id")]long userId) => await accountsControllerService.PostAccountFollow(userId);
|
||||
|
||||
[HttpDelete("follows/{user_id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> DeleteAccountFollow([FromRoute(Name = "user_id")]long userId) => await accountsControllerService.DeleteAccountFollow(userId);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Common.Model.Genders;
|
||||
using WatchIt.WebAPI.Services.Controllers.Genders;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("genders")]
|
||||
public class GendersController : ControllerBase
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IGendersControllerService _gendersControllerService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public GendersController(IGendersControllerService gendersControllerService)
|
||||
{
|
||||
_gendersControllerService = gendersControllerService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<GenderResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult> GetAllGenders(GenderQueryParameters query) => await _gendersControllerService.GetAllGenders(query);
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(GenderResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetGender([FromRoute]short id) => await _gendersControllerService.GetGender(id);
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(GenderResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PostGender([FromBody]GenderRequest body) => await _gendersControllerService.PostGender(body);
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeleteGender([FromRoute]short id) => await _gendersControllerService.DeleteGender(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Common.Model.Genres;
|
||||
using WatchIt.Common.Model.Media;
|
||||
using WatchIt.WebAPI.Services.Controllers.Genres;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("genres")]
|
||||
public class GenresController(IGenresControllerService genresControllerService) : ControllerBase
|
||||
{
|
||||
#region METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<GenreResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult> GetGenres(GenreQueryParameters query) => await genresControllerService.GetGenres(query);
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(GenreResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetGenre([FromRoute]short id) => await genresControllerService.GetGenre(id);
|
||||
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
[ProducesResponseType(typeof(GenreResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PostGenre([FromBody]GenreRequest body) => await genresControllerService.PostGenre(body);
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize]
|
||||
[ProducesResponseType(typeof(GenreResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeleteGenre([FromRoute]short id) => await genresControllerService.DeleteGenre(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Media
|
||||
|
||||
[HttpGet("{id}/media")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<MediaResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetGenreMedia([FromRoute]short id, MediaQueryParameters query) => await genresControllerService.GetGenreMedia(id, query);
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,206 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Common.Model.Genres;
|
||||
using WatchIt.Common.Model.Media;
|
||||
using WatchIt.Common.Model.Photos;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Model.Roles;
|
||||
using WatchIt.WebAPI.Services.Controllers.Media;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("media")]
|
||||
public class MediaController : ControllerBase
|
||||
{
|
||||
#region FIELDS
|
||||
|
||||
private readonly IMediaControllerService _mediaControllerService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public MediaController(IMediaControllerService mediaControllerService)
|
||||
{
|
||||
_mediaControllerService = mediaControllerService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<MediaResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult> GetAllMedia(MediaQueryParameters query) => await _mediaControllerService.GetAllMedia(query);
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(MediaResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMedia([FromRoute] long id) => await _mediaControllerService.GetMedia(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Genres
|
||||
|
||||
[HttpGet("{id}/genres")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<GenreResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaGenres([FromRoute]long id) => await _mediaControllerService.GetMediaGenres(id);
|
||||
|
||||
[HttpPost("{id}/genres/{genre_id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PostMediaGenre([FromRoute]long id, [FromRoute(Name = "genre_id")]short genreId) => await _mediaControllerService.PostMediaGenre(id, genreId);
|
||||
|
||||
[HttpDelete("{id}/genres/{genre_id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeleteMediaGenre([FromRoute]long id, [FromRoute(Name = "genre_id")]short genreId) => await _mediaControllerService.DeleteMediaGenre(id, genreId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
[HttpGet("{id}/rating")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(RatingResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaRating([FromRoute] long id) => await _mediaControllerService.GetMediaRating(id);
|
||||
|
||||
[HttpGet("{id}/rating/{user_id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(short), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaRatingByUser([FromRoute] long id, [FromRoute(Name = "user_id")]long userId) => await _mediaControllerService.GetMediaRatingByUser(id, userId);
|
||||
|
||||
[HttpPut("{id}/rating")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PutMediaRating([FromRoute] long id, [FromBody] RatingRequest data) => await _mediaControllerService.PutMediaRating(id, data);
|
||||
|
||||
[HttpDelete("{id}/rating")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> DeleteMediaRating([FromRoute] long id) => await _mediaControllerService.DeleteMediaRating(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
[HttpPost("{id}/view")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PostMediaView([FromRoute] long id) => await _mediaControllerService.PostMediaView(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Poster
|
||||
|
||||
[HttpGet("{id}/poster")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(MediaPosterResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaPoster([FromRoute] long id) => await _mediaControllerService.GetMediaPoster(id);
|
||||
|
||||
[HttpPut("{id}/poster")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(MediaPosterResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PutMediaPoster([FromRoute]long id, [FromBody]MediaPosterRequest body) => await _mediaControllerService.PutMediaPoster(id, body);
|
||||
|
||||
[HttpDelete("{id}/poster")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeleteMediaPoster([FromRoute]long id) => await _mediaControllerService.DeleteMediaPoster(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Photos
|
||||
|
||||
[HttpGet("{id}/photos")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<PhotoResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaPhotos([FromRoute]long id, PhotoQueryParameters query) => await _mediaControllerService.GetMediaPhotos(id, query);
|
||||
|
||||
[HttpGet("{id}/photos/random_background")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(PhotoResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaPhotoRandomBackground([FromRoute]long id) => await _mediaControllerService.GetMediaPhotoRandomBackground(id);
|
||||
|
||||
[HttpPost("{id}/photos")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(PhotoResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PostPhoto([FromRoute]long id, [FromBody]MediaPhotoRequest body) => await _mediaControllerService.PostMediaPhoto(id, body);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Roles
|
||||
|
||||
[HttpGet("{id}/roles/actor")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<ActorRoleResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaAllActorRoles([FromRoute]long id, ActorRoleMediaQueryParameters query) => await _mediaControllerService.GetMediaAllActorRoles(id, query);
|
||||
|
||||
[HttpPost("{id}/roles/actor")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(ActorRoleResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PostMediaActorRole([FromRoute]long id, [FromBody]ActorRoleMediaRequest body) => await _mediaControllerService.PostMediaActorRole(id, body);
|
||||
|
||||
[HttpGet("{id}/roles/creator")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<CreatorRoleResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaAllCreatorRoles([FromRoute]long id, CreatorRoleMediaQueryParameters query) => await _mediaControllerService.GetMediaAllCreatorRoles(id, query);
|
||||
|
||||
[HttpPost("{id}/roles/creator")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(CreatorRoleResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PostMediaCreatorRole([FromRoute]long id, [FromBody]CreatorRoleMediaRequest body) => await _mediaControllerService.PostMediaCreatorRole(id, body);
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Common.Model.Genres;
|
||||
using WatchIt.Common.Model.Movies;
|
||||
using WatchIt.WebAPI.Services.Controllers.Movies;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("movies")]
|
||||
public class MoviesController : ControllerBase
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IMoviesControllerService _moviesControllerService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public MoviesController(IMoviesControllerService moviesControllerService)
|
||||
{
|
||||
_moviesControllerService = moviesControllerService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<MovieResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult> GetAllMovies(MovieQueryParameters query) => await _moviesControllerService.GetAllMovies(query);
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(MovieResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMovie([FromRoute] long id) => await _moviesControllerService.GetMovie(id);
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(MovieResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PostMovie([FromBody] MovieRequest body) => await _moviesControllerService.PostMovie(body);
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PutMovie([FromRoute] long id, [FromBody]MovieRequest body) => await _moviesControllerService.PutMovie(id, body);
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeleteMovie([FromRoute] long id) => await _moviesControllerService.DeleteMovie(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
[HttpGet("view")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<MovieResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult> GetMoviesViewRank([FromQuery] int first = 5, [FromQuery] int days = 7) => await _moviesControllerService.GetMoviesViewRank(first, days);
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,168 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Common.Model.Persons;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Model.Roles;
|
||||
using WatchIt.WebAPI.Services.Controllers.Persons;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("persons")]
|
||||
public class PersonsController : ControllerBase
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IPersonsControllerService _personsControllerService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public PersonsController(IPersonsControllerService personsControllerService)
|
||||
{
|
||||
_personsControllerService = personsControllerService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<PersonResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult> GetAllMovies(PersonQueryParameters query) => await _personsControllerService.GetAllPersons(query);
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(PersonResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMovie([FromRoute] long id) => await _personsControllerService.GetPerson(id);
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(PersonResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PostMovie([FromBody] PersonRequest body) => await _personsControllerService.PostPerson(body);
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PutMovie([FromRoute] long id, [FromBody]PersonRequest body) => await _personsControllerService.PutPerson(id, body);
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeleteMovie([FromRoute] long id) => await _personsControllerService.DeletePerson(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
[HttpGet("view")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<PersonResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult> GetPersonsViewRank([FromQuery] int first = 5, [FromQuery] int days = 7) => await _personsControllerService.GetPersonsViewRank(first, days);
|
||||
|
||||
[HttpPost("{id}/view")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PostPersonsView([FromRoute] long id) => await _personsControllerService.PostPersonsView(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Photo
|
||||
|
||||
[HttpGet("{id}/photo")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(PersonPhotoResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetPersonPhoto([FromRoute] long id) => await _personsControllerService.GetPersonPhoto(id);
|
||||
|
||||
[HttpPut("{id}/photo")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(PersonPhotoResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PutPersonPhoto([FromRoute]long id, [FromBody]PersonPhotoRequest body) => await _personsControllerService.PutPersonPhoto(id, body);
|
||||
|
||||
[HttpDelete("{id}/photo")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeletePersonPhoto([FromRoute]long id) => await _personsControllerService.DeletePersonPhoto(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Roles
|
||||
|
||||
[HttpGet("{id}/roles/actor")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<ActorRoleResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetPersonAllActorRoles([FromRoute]long id, ActorRolePersonQueryParameters query) => await _personsControllerService.GetPersonAllActorRoles(id, query);
|
||||
|
||||
[HttpPost("{id}/roles/actor")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(ActorRoleResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PostPersonActorRole([FromRoute]long id, [FromBody]ActorRolePersonRequest body) => await _personsControllerService.PostPersonActorRole(id, body);
|
||||
|
||||
[HttpGet("{id}/roles/creator")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<CreatorRoleResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetPersonAllCreatorRoles([FromRoute]long id, CreatorRolePersonQueryParameters query) => await _personsControllerService.GetPersonAllCreatorRoles(id, query);
|
||||
|
||||
[HttpPost("{id}/roles/creator")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(CreatorRoleResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PostPersonCreatorRole([FromRoute]long id, [FromBody]CreatorRolePersonRequest body) => await _personsControllerService.PostPersonCreatorRole(id, body);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
[HttpGet("{id}/rating")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(RatingResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetPersonGlobalRating([FromRoute]long id) => await _personsControllerService.GetPersonGlobalRating(id);
|
||||
|
||||
[HttpGet("{id}/rating/{user_id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(RatingResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetPersonUserRating([FromRoute]long id, [FromRoute(Name = "user_id")]long userId) => await _personsControllerService.GetPersonUserRating(id, userId);
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Common.Model.Photos;
|
||||
using WatchIt.WebAPI.Services.Controllers.Photos;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("photos")]
|
||||
public class PhotosController : ControllerBase
|
||||
{
|
||||
#region FIELDS
|
||||
|
||||
private readonly IPhotosControllerService _photosControllerService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public PhotosController(IPhotosControllerService photosControllerService)
|
||||
{
|
||||
_photosControllerService = photosControllerService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
[HttpGet("random_background")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(PhotoResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetPhotoRandomBackground() => await _photosControllerService.GetPhotoRandomBackground();
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeletePhoto([FromRoute] Guid id) => await _photosControllerService.DeletePhoto(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Background data
|
||||
|
||||
[HttpPut("{id}/background_data")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PutPhotoBackgroundData([FromRoute]Guid id, [FromBody] PhotoBackgroundDataRequest body) => await _photosControllerService.PutPhotoBackgroundData(id, body);
|
||||
|
||||
[HttpDelete("{id}/background_data")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeletePhotoBackgroundData([FromRoute]Guid id) => await _photosControllerService.DeletePhotoBackgroundData(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Model.Roles;
|
||||
using WatchIt.WebAPI.Services.Controllers.Roles;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("roles")]
|
||||
public class RolesController : ControllerBase
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IRolesControllerService _rolesControllerService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RolesController(IRolesControllerService rolesControllerService)
|
||||
{
|
||||
_rolesControllerService = rolesControllerService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region METHODS
|
||||
|
||||
#region Actor
|
||||
|
||||
[HttpGet("actor/{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<ActorRoleResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetActorRole([FromRoute]Guid id) => await _rolesControllerService.GetActorRole(id);
|
||||
|
||||
[HttpPut("actor/{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PutActorRole([FromRoute]Guid id, [FromBody]ActorRoleUniversalRequest body) => await _rolesControllerService.PutActorRole(id, body);
|
||||
|
||||
[HttpDelete("actor/{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeleteActorRole([FromRoute]Guid id) => await _rolesControllerService.DeleteActorRole(id);
|
||||
|
||||
[HttpGet("actor/{id}/rating")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(RatingResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetActorRoleRating([FromRoute] Guid id) => await _rolesControllerService.GetActorRoleRating(id);
|
||||
|
||||
[HttpGet("actor/{id}/rating/{user_id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(short), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetActorRoleRatingByUser([FromRoute] Guid id, [FromRoute(Name = "user_id")]long userId) => await _rolesControllerService.GetActorRoleRatingByUser(id, userId);
|
||||
|
||||
[HttpPut("actor/{id}/rating")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PutActorRoleRating([FromRoute] Guid id, [FromBody] RatingRequest data) => await _rolesControllerService.PutActorRoleRating(id, data);
|
||||
|
||||
[HttpDelete("actor/{id}/rating")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> DeleteActorRoleRating([FromRoute] Guid id) => await _rolesControllerService.DeleteActorRoleRating(id);
|
||||
|
||||
[HttpGet("actor/type")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<RoleTypeResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult> GetAllActorRoleTypes(RoleTypeQueryParameters query) => await _rolesControllerService.GetAllActorRoleTypes(query);
|
||||
|
||||
[HttpGet("actor/type/{type_id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(RoleTypeResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetActorRoleType([FromRoute(Name = "type_id")]short typeId) => await _rolesControllerService.GetActorRoleType(typeId);
|
||||
|
||||
[HttpPost("actor/type")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(RoleTypeResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PostActorRoleType([FromBody]RoleTypeRequest body) => await _rolesControllerService.PostActorRoleType(body);
|
||||
|
||||
[HttpDelete("actor/type/{type_id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeleteActorRoleType([FromRoute(Name = "type_id")]short typeId) => await _rolesControllerService.DeleteActorRoleType(typeId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Creator
|
||||
|
||||
[HttpGet("creator/{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<CreatorRoleResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult> GetCreatorRole([FromRoute]Guid id) => await _rolesControllerService.GetCreatorRole(id);
|
||||
|
||||
[HttpPut("creator/{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PutCreatorRole([FromRoute]Guid id, [FromBody]CreatorRoleUniversalRequest body) => await _rolesControllerService.PutCreatorRole(id, body);
|
||||
|
||||
[HttpDelete("creator/{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeleteCreatorRole([FromRoute]Guid id) => await _rolesControllerService.DeleteCreatorRole(id);
|
||||
|
||||
[HttpGet("creator/{id}/rating")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(RatingResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetCreatorRoleRating([FromRoute] Guid id) => await _rolesControllerService.GetCreatorRoleRating(id);
|
||||
|
||||
[HttpGet("creator/{id}/rating/{user_id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(short), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetCreatorRoleRatingByUser([FromRoute] Guid id, [FromRoute(Name = "user_id")] long userId) => await _rolesControllerService.GetCreatorRoleRatingByUser(id, userId);
|
||||
|
||||
[HttpPut("creator/{id}/rating")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PutCreatorRoleRating([FromRoute] Guid id, [FromBody] RatingRequest data) => await _rolesControllerService.PutCreatorRoleRating(id, data);
|
||||
|
||||
[HttpDelete("creator/{id}/rating")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> DeleteCreatorRoleRating([FromRoute] Guid id) => await _rolesControllerService.DeleteCreatorRoleRating(id);
|
||||
|
||||
[HttpGet("creator/type")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<RoleTypeResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult> GetAllCreatorRoleTypes(RoleTypeQueryParameters query) => await _rolesControllerService.GetAllCreatorRoleTypes(query);
|
||||
|
||||
[HttpGet("creator/type/{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(RoleTypeResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetCreatorRoleType([FromRoute]short id) => await _rolesControllerService.GetCreatorRoleType(id);
|
||||
|
||||
[HttpPost("creator/type")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(RoleTypeResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PostCreatorRoleType([FromBody]RoleTypeRequest body) => await _rolesControllerService.PostCreatorRoleType(body);
|
||||
|
||||
[HttpDelete("creator/type/{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeleteCreatorRoleType([FromRoute]short id) => await _rolesControllerService.DeleteCreatorRoleType(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Common.Model.Series;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.WebAPI.Services.Controllers.Series;
|
||||
|
||||
namespace WatchIt.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("series")]
|
||||
public class SeriesController : ControllerBase
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly ISeriesControllerService _seriesControllerService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public SeriesController(ISeriesControllerService seriesControllerService)
|
||||
{
|
||||
_seriesControllerService = seriesControllerService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<SeriesResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult> GetAllSeries(SeriesQueryParameters query) => await _seriesControllerService.GetAllSeries(query);
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(SeriesResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetSeries([FromRoute] long id) => await _seriesControllerService.GetSeries(id);
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(SeriesResponse), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PostSeries([FromBody] SeriesRequest body) => await _seriesControllerService.PostSeries(body);
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PutSeries([FromRoute] long id, [FromBody]SeriesRequest body) => await _seriesControllerService.PutSeries(id, body);
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> DeleteSeries([FromRoute] long id) => await _seriesControllerService.DeleteSeries(id);
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
[HttpGet("view")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<SeriesResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult> GetSeriesViewRank([FromQuery] int first = 5, [FromQuery] int days = 7) => await _seriesControllerService.GetSeriesViewRank(first, days);
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Accounts\WatchIt.WebAPI.Services.Controllers.Accounts.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Genders\WatchIt.WebAPI.Services.Controllers.Genders.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Genres\WatchIt.WebAPI.Services.Controllers.Genres.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Media\WatchIt.WebAPI.Services.Controllers.Media.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Movies\WatchIt.WebAPI.Services.Controllers.Movies.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Persons\WatchIt.WebAPI.Services.Controllers.Persons.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Photos\WatchIt.WebAPI.Services.Controllers.Photos.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Roles\WatchIt.WebAPI.Services.Controllers.Roles.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Series\WatchIt.WebAPI.Services.Controllers.Series.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,461 +0,0 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SimpleToolkit.Extensions;
|
||||
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;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.Database.Model.Rating;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.Tokens;
|
||||
using WatchIt.WebAPI.Services.Utility.Tokens.Exceptions;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
using Account = WatchIt.Database.Model.Account.Account;
|
||||
using AccountProfilePicture = WatchIt.Common.Model.Accounts.AccountProfilePicture;
|
||||
using Person = WatchIt.Database.Model.Person.Person;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Accounts;
|
||||
|
||||
public class AccountsControllerService(
|
||||
ILogger<AccountsControllerService> logger,
|
||||
DatabaseContext database,
|
||||
ITokensService tokensService,
|
||||
IUserService userService
|
||||
) : IAccountsControllerService
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Basic
|
||||
|
||||
public async Task<RequestResult> Register(RegisterRequest data)
|
||||
{
|
||||
Account account = new Account
|
||||
{
|
||||
Username = data.Username,
|
||||
Email = data.Email,
|
||||
};
|
||||
|
||||
SetPassword(account, data.Password);
|
||||
await database.Accounts.AddAsync(account);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
logger.LogInformation($"New account with ID {account.Id} was created (username: {account.Username}; email: {account.Email})");
|
||||
return RequestResult.Created($"accounts/{account.Id}", new RegisterResponse(account));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> Authenticate(AuthenticateRequest data)
|
||||
{
|
||||
Account? account = await database.Accounts.FirstOrDefaultAsync(x => string.Equals(x.Email, data.UsernameOrEmail) || string.Equals(x.Username, data.UsernameOrEmail));
|
||||
if (account is null || !ComputeHash(data.Password, account.LeftSalt, account.RightSalt).SequenceEqual(account.Password))
|
||||
{
|
||||
return RequestResult.Unauthorized();
|
||||
}
|
||||
|
||||
Task<string> refreshTokenTask = tokensService.CreateRefreshTokenAsync(account, true);
|
||||
Task<string> accessTokenTask = tokensService.CreateAccessTokenAsync(account);
|
||||
AuthenticateResponse response = new AuthenticateResponse
|
||||
{
|
||||
AccessToken = await accessTokenTask,
|
||||
RefreshToken = await refreshTokenTask,
|
||||
};
|
||||
|
||||
account.LastActive = DateTime.UtcNow;
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
logger.LogInformation($"Account with ID {account.Id} was authenticated");
|
||||
return RequestResult.Ok(response);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> AuthenticateRefresh()
|
||||
{
|
||||
Guid jti = userService.GetJti();
|
||||
AccountRefreshToken? token = await database.AccountRefreshTokens.FirstOrDefaultAsync(x => x.Id == jti);
|
||||
if (token is null || token.ExpirationDate < DateTime.UtcNow)
|
||||
{
|
||||
return RequestResult.Unauthorized();
|
||||
}
|
||||
|
||||
string refreshToken;
|
||||
try
|
||||
{
|
||||
refreshToken = await tokensService.ExtendRefreshTokenAsync(token.Account, token.Id);
|
||||
}
|
||||
catch (TokenNotFoundException)
|
||||
{
|
||||
return RequestResult.Unauthorized();
|
||||
}
|
||||
catch (TokenNotExtendableException)
|
||||
{
|
||||
refreshToken = userService.GetRawToken().Replace("Bearer ", string.Empty);
|
||||
}
|
||||
|
||||
string accessToken = await tokensService.CreateAccessTokenAsync(token.Account);
|
||||
|
||||
token.Account.LastActive = DateTime.UtcNow;
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
logger.LogInformation($"Account with ID {token.AccountId} was authenticated by token refreshing");
|
||||
return RequestResult.Ok(new AuthenticateResponse
|
||||
{
|
||||
AccessToken = accessToken,
|
||||
RefreshToken = refreshToken,
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<RequestResult> Logout()
|
||||
{
|
||||
Guid jti = userService.GetJti();
|
||||
AccountRefreshToken? token = await database.AccountRefreshTokens.FirstOrDefaultAsync(x => x.Id == jti);
|
||||
if (token is not null)
|
||||
{
|
||||
database.AccountRefreshTokens.Attach(token);
|
||||
database.AccountRefreshTokens.Remove(token);
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile picture
|
||||
|
||||
public async Task<RequestResult> GetAccountProfilePicture(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.ProfilePicture is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
AccountProfilePictureResponse picture = new AccountProfilePictureResponse(account.ProfilePicture);
|
||||
return RequestResult.Ok(picture);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutAccountProfilePicture(AccountProfilePictureRequest data)
|
||||
{
|
||||
Account account = await database.Accounts.FirstAsync(x => x.Id == userService.GetUserId());
|
||||
Database.Model.Account.AccountProfilePicture? picture = account.ProfilePicture;
|
||||
|
||||
if (picture is null)
|
||||
{
|
||||
picture = data.CreateMediaPosterImage();
|
||||
await database.AccountProfilePictures.AddAsync(picture);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
account.ProfilePictureId = picture.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
data.UpdateMediaPosterImage(picture);
|
||||
}
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
AccountProfilePictureResponse returnData = new AccountProfilePictureResponse(picture);
|
||||
return RequestResult.Ok(returnData);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteAccountProfilePicture()
|
||||
{
|
||||
Account account = await database.Accounts.FirstAsync(x => x.Id == userService.GetUserId());
|
||||
Database.Model.Account.AccountProfilePicture? picture = account.ProfilePicture;
|
||||
|
||||
if (picture is not null)
|
||||
{
|
||||
account.ProfilePictureId = null;
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
database.AccountProfilePictures.Attach(picture);
|
||||
database.AccountProfilePictures.Remove(picture);
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
#region Info
|
||||
|
||||
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)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
AccountResponse profileInfoResponse = new AccountResponse(account);
|
||||
return RequestResult.Ok(profileInfoResponse);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutAccountProfileInfo(AccountProfileInfoRequest data)
|
||||
{
|
||||
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == userService.GetUserId());
|
||||
if (account is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
data.UpdateAccount(account);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PatchAccountEmail(AccountEmailRequest 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();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PatchAccountPassword(AccountPasswordRequest data)
|
||||
{
|
||||
Account account = await database.Accounts.FirstAsync(x => x.Id == userService.GetUserId());
|
||||
|
||||
if (!ComputeHash(data.OldPassword, account.LeftSalt, account.RightSalt).SequenceEqual(account.Password))
|
||||
{
|
||||
return RequestResult.Unauthorized();
|
||||
}
|
||||
|
||||
SetPassword(account, data.NewPassword);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Media
|
||||
|
||||
public async Task<RequestResult> GetAccountRatedMovies(long id, MovieRatedQueryParameters query)
|
||||
{
|
||||
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (account is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<MovieRatedResponse> response = account.RatingMedia.Join(database.MediaMovies, x => x.MediaId, x => x.Id, (x, y) => new MovieRatedResponse(y, x));
|
||||
response = query.PrepareData(response);
|
||||
return RequestResult.Ok(response);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetAccountRatedSeries(long id, SeriesRatedQueryParameters query)
|
||||
{
|
||||
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (account is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<SeriesRatedResponse> response = account.RatingMedia.Join(database.MediaSeries, x => x.MediaId, x => x.Id, (x, y) => new SeriesRatedResponse(y, x));
|
||||
response = query.PrepareData(response);
|
||||
return RequestResult.Ok(response);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetAccountRatedPersons(long id, PersonRatedQueryParameters query)
|
||||
{
|
||||
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (account is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<RatingPersonActorRole> actorRolesRatings = account.RatingPersonActorRole;
|
||||
IEnumerable<RatingPersonCreatorRole> creatorRolesRatings = account.RatingPersonCreatorRole;
|
||||
IEnumerable<Person> persons = actorRolesRatings.Select(x => x.PersonActorRole.Person)
|
||||
.Union(creatorRolesRatings.Select(x => x.PersonCreatorRole.Person));
|
||||
|
||||
IEnumerable<PersonRatedResponse> response = persons.Select(x => new PersonRatedResponse(x, actorRolesRatings.Where(y => y.PersonActorRole.Person.Id == x.Id), creatorRolesRatings.Where(y => y.PersonCreatorRole.Person.Id == x.Id)));
|
||||
response = query.PrepareData(response);
|
||||
return RequestResult.Ok(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Follows
|
||||
|
||||
public async Task<RequestResult> GetAccountFollows(long id, AccountQueryParameters query)
|
||||
{
|
||||
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (account is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<AccountResponse> data = account.AccountFollows.Select(x => new AccountResponse(x.AccountFollowed));
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetAccountFollowers(long id, AccountQueryParameters query)
|
||||
{
|
||||
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (account is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<AccountResponse> data = account.AccountFollowers.Select(x => new AccountResponse(x.AccountFollower));
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostAccountFollow(long userId)
|
||||
{
|
||||
long followerId = userService.GetUserId();
|
||||
if (userId == followerId)
|
||||
{
|
||||
return RequestResult.BadRequest().AddValidationError("user_id", "You cannot follow yourself");
|
||||
}
|
||||
if (!database.Accounts.Any(x => x.Id == userId))
|
||||
{
|
||||
return RequestResult.BadRequest().AddValidationError("user_id", "User with this id doesn't exist");
|
||||
}
|
||||
|
||||
Account account = await database.Accounts.FirstAsync(x => x.Id == followerId);
|
||||
if (account.AccountFollows.All(x => x.AccountFollowedId != userId))
|
||||
{
|
||||
AccountFollow follow = new AccountFollow()
|
||||
{
|
||||
AccountFollowerId = followerId,
|
||||
AccountFollowedId = userId,
|
||||
};
|
||||
await database.AccountFollows.AddAsync(follow);
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteAccountFollow(long userId)
|
||||
{
|
||||
AccountFollow? accountFollow = await database.AccountFollows.FirstOrDefaultAsync(x => x.AccountFollowerId == userService.GetUserId() && x.AccountFollowedId == userId);
|
||||
|
||||
if (accountFollow is not null)
|
||||
{
|
||||
database.AccountFollows.Attach(accountFollow);
|
||||
database.AccountFollows.Remove(accountFollow);
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
protected byte[] ComputeHash(string password, string leftSalt, string rightSalt) => SHA512.HashData(Encoding.UTF8.GetBytes($"{leftSalt}{password}{rightSalt}"));
|
||||
|
||||
private void SetPassword(Account account, string password)
|
||||
{
|
||||
string leftSalt = StringExtensions.CreateRandom(20);
|
||||
string rightSalt = StringExtensions.CreateRandom(20);
|
||||
byte[] hash = ComputeHash(password, leftSalt, rightSalt);
|
||||
|
||||
account.Password = hash;
|
||||
account.LeftSalt = leftSalt;
|
||||
account.RightSalt = rightSalt;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
using WatchIt.Common.Model.Accounts;
|
||||
using WatchIt.Common.Model.Media;
|
||||
using WatchIt.Common.Model.Movies;
|
||||
using WatchIt.Common.Model.Persons;
|
||||
using WatchIt.Common.Model.Series;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Accounts;
|
||||
|
||||
public interface IAccountsControllerService
|
||||
{
|
||||
Task<RequestResult> Register(RegisterRequest data);
|
||||
Task<RequestResult> Authenticate(AuthenticateRequest data);
|
||||
Task<RequestResult> AuthenticateRefresh();
|
||||
Task<RequestResult> Logout();
|
||||
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> GetAccounts(AccountQueryParameters query);
|
||||
Task<RequestResult> GetAccount(long id);
|
||||
Task<RequestResult> PutAccountProfileInfo(AccountProfileInfoRequest data);
|
||||
Task<RequestResult> PatchAccountUsername(AccountUsernameRequest data);
|
||||
Task<RequestResult> PatchAccountEmail(AccountEmailRequest data);
|
||||
Task<RequestResult> PatchAccountPassword(AccountPasswordRequest data);
|
||||
Task<RequestResult> GetAccountRatedMovies(long id, MovieRatedQueryParameters query);
|
||||
Task<RequestResult> GetAccountRatedSeries(long id, SeriesRatedQueryParameters query);
|
||||
Task<RequestResult> GetAccountRatedPersons(long id, PersonRatedQueryParameters query);
|
||||
Task<RequestResult> GetAccountFollows(long id, AccountQueryParameters query);
|
||||
Task<RequestResult> GetAccountFollowers(long id, AccountQueryParameters query);
|
||||
Task<RequestResult> PostAccountFollow(long userId);
|
||||
Task<RequestResult> DeleteAccountFollow(long userId);
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database.Model\WatchIt.Database.Model\WatchIt.Database.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\..\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.Tokens\WatchIt.WebAPI.Services.Utility.Tokens.csproj" />
|
||||
<ProjectReference Include="..\..\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.User\WatchIt.WebAPI.Services.Utility.User.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services.Controllers.Common\WatchIt.WebAPI.Services.Controllers.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SimpleToolkit.Extensions" Version="1.7.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,44 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public class RequestBadRequestResult : RequestResult
|
||||
{
|
||||
#region FIELDS
|
||||
|
||||
private readonly ModelStateDictionary _modelState;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RequestBadRequestResult() : base(RequestResultStatus.BadRequest)
|
||||
{
|
||||
_modelState = new ModelStateDictionary();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public RequestBadRequestResult AddValidationError(string propertyName, string message)
|
||||
{
|
||||
_modelState.AddModelError(propertyName, message);
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERTION
|
||||
|
||||
protected override ActionResult ConvertToActionResult() => new BadRequestObjectResult(_modelState);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public class RequestConflictResult : RequestResult
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RequestConflictResult() : base(RequestResultStatus.Conflict)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERTION
|
||||
|
||||
protected override ActionResult ConvertToActionResult() => new ConflictResult();
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public class RequestCreatedResult<T> : RequestResult
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public string Location { get; }
|
||||
public T Data { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
internal RequestCreatedResult(string location, T data) : base(RequestResultStatus.Created)
|
||||
{
|
||||
Location = location;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERTION
|
||||
|
||||
protected override ActionResult ConvertToActionResult() => new CreatedResult(Location, Data);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public class RequestForbiddenResult : RequestResult
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RequestForbiddenResult() : base(RequestResultStatus.Forbidden)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERTION
|
||||
|
||||
protected override ActionResult ConvertToActionResult() => new ForbidResult();
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public class RequestNoContentResult : RequestResult
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
internal RequestNoContentResult() : base(RequestResultStatus.NoContent)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERTION
|
||||
|
||||
protected override ActionResult ConvertToActionResult() => new NoContentResult();
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public class RequestNotFoundResult : RequestResult
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RequestNotFoundResult() : base(RequestResultStatus.NotFound)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERTION
|
||||
|
||||
protected override ActionResult ConvertToActionResult() => new NotFoundResult();
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public class RequestOkResult : RequestResult
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
internal RequestOkResult() : base(RequestResultStatus.Ok)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERTION
|
||||
|
||||
protected override ActionResult ConvertToActionResult() => new OkResult();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public class RequestOkResult<T> : RequestOkResult
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public T Data { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
internal RequestOkResult(T data) : base() => Data = data;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERTION
|
||||
|
||||
protected override ActionResult ConvertToActionResult() => new OkObjectResult(Data);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public abstract class RequestResult
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public RequestResultStatus Status { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
protected RequestResult(RequestResultStatus status) => Status = status;
|
||||
|
||||
public static RequestOkResult Ok() => new RequestOkResult();
|
||||
public static RequestOkResult<T> Ok<T>(T data) => new RequestOkResult<T>(data);
|
||||
public static RequestCreatedResult<T> Created<T>(string location, T data) => new RequestCreatedResult<T>(location, data);
|
||||
public static RequestNoContentResult NoContent() => new RequestNoContentResult();
|
||||
public static RequestBadRequestResult BadRequest() => new RequestBadRequestResult();
|
||||
public static RequestUnauthorizedResult Unauthorized() => new RequestUnauthorizedResult();
|
||||
public static RequestForbiddenResult Forbidden() => new RequestForbiddenResult();
|
||||
public static RequestNotFoundResult NotFound() => new RequestNotFoundResult();
|
||||
public static RequestConflictResult Conflict() => new RequestConflictResult();
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERSION
|
||||
|
||||
public static implicit operator ActionResult(RequestResult result) => result.ConvertToActionResult();
|
||||
|
||||
protected abstract ActionResult ConvertToActionResult();
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public enum RequestResultStatus
|
||||
{
|
||||
Ok = 200,
|
||||
Created = 201,
|
||||
NoContent = 204,
|
||||
BadRequest = 400,
|
||||
Unauthorized = 401,
|
||||
Forbidden = 403,
|
||||
NotFound = 404,
|
||||
Conflict = 409,
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
public class RequestUnauthorizedResult : RequestResult
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RequestUnauthorizedResult() : base(RequestResultStatus.Unauthorized)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONVERTION
|
||||
|
||||
protected override ActionResult ConvertToActionResult() => new UnauthorizedResult();
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.AspNetCore.Mvc.Core">
|
||||
<HintPath>..\..\..\..\..\..\..\..\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.2\Microsoft.AspNetCore.Mvc.Core.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,97 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Common.Model.Genders;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Common;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
using Gender = WatchIt.Database.Model.Common.Gender;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Genders;
|
||||
|
||||
public class GendersControllerService : IGendersControllerService
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public GendersControllerService(DatabaseContext database, IUserService userService)
|
||||
{
|
||||
_database = database;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<RequestResult> GetAllGenders(GenderQueryParameters query)
|
||||
{
|
||||
IEnumerable<Gender> rawData = await _database.Genders.ToListAsync();
|
||||
IEnumerable<GenderResponse> data = rawData.Select(x => new GenderResponse(x));
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetGender(short id)
|
||||
{
|
||||
Gender? item = await _database.Genders.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
GenderResponse data = new GenderResponse(item);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostGender(GenderRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Gender item = data.CreateGender();
|
||||
await _database.Genders.AddAsync(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"genres/{item.Id}", new GenderResponse(item));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteGender(short id)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Gender? item = await _database.Genders.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
_database.Genders.Attach(item);
|
||||
_database.Genders.Remove(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using WatchIt.Common.Model.Genders;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Genders;
|
||||
|
||||
public interface IGendersControllerService
|
||||
{
|
||||
Task<RequestResult> GetAllGenders(GenderQueryParameters query);
|
||||
Task<RequestResult> GetGender(short id);
|
||||
Task<RequestResult> PostGender(GenderRequest data);
|
||||
Task<RequestResult> DeleteGender(short id);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\..\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.User\WatchIt.WebAPI.Services.Utility.User.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services.Controllers.Common\WatchIt.WebAPI.Services.Controllers.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,95 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Common.Model.Genres;
|
||||
using WatchIt.Common.Model.Media;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
using Genre = WatchIt.Database.Model.Common.Genre;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Genres;
|
||||
|
||||
public class GenresControllerService(DatabaseContext database, IUserService userService) : IGenresControllerService
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<RequestResult> GetGenres(GenreQueryParameters query)
|
||||
{
|
||||
IEnumerable<GenreResponse> data = await database.Genres.Select(x => new GenreResponse(x)).ToListAsync();
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetGenre(short id)
|
||||
{
|
||||
Genre? item = await database.Genres.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
GenreResponse data = new GenreResponse(item);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostGenre(GenreRequest data)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Genre item = data.CreateGenre();
|
||||
await database.Genres.AddAsync(item);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"genres/{item.Id}", new GenreResponse(item));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteGenre(short id)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Genre? item = await database.Genres.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
database.MediaGenres.AttachRange(item.MediaGenres);
|
||||
database.MediaGenres.RemoveRange(item.MediaGenres);
|
||||
database.Genres.Attach(item);
|
||||
database.Genres.Remove(item);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Media
|
||||
|
||||
public async Task<RequestResult> GetGenreMedia(short id, MediaQueryParameters query)
|
||||
{
|
||||
Genre? genre = await database.Genres.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (genre is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<MediaResponse> data = genre.Media.Select(x => new MediaResponse(x, database.MediaMovies.Any(y => y.Id == x.Id) ? MediaType.Movie : MediaType.Series));
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using WatchIt.Common.Model.Genres;
|
||||
using WatchIt.Common.Model.Media;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Genres;
|
||||
|
||||
public interface IGenresControllerService
|
||||
{
|
||||
Task<RequestResult> GetGenres(GenreQueryParameters query);
|
||||
Task<RequestResult> GetGenre(short id);
|
||||
Task<RequestResult> PostGenre(GenreRequest data);
|
||||
Task<RequestResult> DeleteGenre(short id);
|
||||
Task<RequestResult> GetGenreMedia(short id, MediaQueryParameters query);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\..\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.User\WatchIt.WebAPI.Services.Utility.User.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services.Controllers.Common\WatchIt.WebAPI.Services.Controllers.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,37 +0,0 @@
|
||||
using WatchIt.Common.Model.Media;
|
||||
using WatchIt.Common.Model.Photos;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Model.Roles;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Media;
|
||||
|
||||
public interface IMediaControllerService
|
||||
{
|
||||
Task<RequestResult> GetAllMedia(MediaQueryParameters query);
|
||||
Task<RequestResult> GetMedia(long mediaId);
|
||||
|
||||
Task<RequestResult> GetMediaGenres(long mediaId);
|
||||
Task<RequestResult> PostMediaGenre(long mediaId, short genreId);
|
||||
Task<RequestResult> DeleteMediaGenre(long mediaId, short genreId);
|
||||
|
||||
Task<RequestResult> GetMediaRating(long mediaId);
|
||||
Task<RequestResult> GetMediaRatingByUser(long mediaId, long userId);
|
||||
Task<RequestResult> PutMediaRating(long mediaId, RatingRequest data);
|
||||
Task<RequestResult> DeleteMediaRating(long mediaId);
|
||||
|
||||
Task<RequestResult> PostMediaView(long mediaId);
|
||||
|
||||
Task<RequestResult> GetMediaPoster(long mediaId);
|
||||
Task<RequestResult> PutMediaPoster(long mediaId, MediaPosterRequest data);
|
||||
Task<RequestResult> DeleteMediaPoster(long mediaId);
|
||||
|
||||
Task<RequestResult> GetMediaPhotos(long mediaId, PhotoQueryParameters queryParameters);
|
||||
Task<RequestResult> GetMediaPhotoRandomBackground(long mediaId);
|
||||
Task<RequestResult> PostMediaPhoto(long mediaId, MediaPhotoRequest data);
|
||||
|
||||
Task<RequestResult> GetMediaAllActorRoles(long mediaId, ActorRoleMediaQueryParameters queryParameters);
|
||||
Task<RequestResult> PostMediaActorRole(long mediaId, ActorRoleMediaRequest data);
|
||||
Task<RequestResult> GetMediaAllCreatorRoles(long mediaId, CreatorRoleMediaQueryParameters queryParameters);
|
||||
Task<RequestResult> PostMediaCreatorRole(long mediaId, CreatorRoleMediaRequest data);
|
||||
}
|
||||
@@ -1,429 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SimpleToolkit.Extensions;
|
||||
using WatchIt.Common.Model.Genres;
|
||||
using WatchIt.Common.Model.Media;
|
||||
using WatchIt.Common.Model.Photos;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Model.Roles;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.Database.Model.Person;
|
||||
using WatchIt.Database.Model.Rating;
|
||||
using WatchIt.Database.Model.ViewCount;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Media;
|
||||
|
||||
public class MediaControllerService(DatabaseContext database, IUserService userService) : IMediaControllerService
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<RequestResult> GetAllMedia(MediaQueryParameters query)
|
||||
{
|
||||
IEnumerable<Database.Model.Media.Media> rawData = await database.Media.ToListAsync();
|
||||
IEnumerable<MediaResponse> data = rawData.Select(x => new MediaResponse(x, database.MediaMovies.Any(y => y.Id == x.Id) ? MediaType.Movie : MediaType.Series));
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetMedia(long mediaId)
|
||||
{
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
MediaMovie? movie = await database.MediaMovies.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
|
||||
MediaResponse mediaResponse = new MediaResponse(item, movie is not null ? MediaType.Movie : MediaType.Series);
|
||||
|
||||
return RequestResult.Ok(mediaResponse);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Genres
|
||||
|
||||
public async Task<RequestResult> GetMediaGenres(long mediaId)
|
||||
{
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<GenreResponse> genres = item.MediaGenres.Select(x => new GenreResponse(x.Genre));
|
||||
return RequestResult.Ok(genres);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostMediaGenre(long mediaId, short genreId)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Database.Model.Media.Media? mediaItem = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
Database.Model.Common.Genre? genreItem = await database.Genres.FirstOrDefaultAsync(x => x.Id == genreId);
|
||||
if (mediaItem is null || genreItem is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
await database.MediaGenres.AddAsync(new MediaGenre
|
||||
{
|
||||
GenreId = genreId,
|
||||
MediaId = mediaId,
|
||||
});
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteMediaGenre(long mediaId, short genreId)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
MediaGenre? item = await database.MediaGenres.FirstOrDefaultAsync(x => x.MediaId == mediaId && x.GenreId == genreId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
database.MediaGenres.Attach(item);
|
||||
database.MediaGenres.Remove(item);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
public async Task<RequestResult> GetMediaRating(long mediaId)
|
||||
{
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
RatingResponse ratingResponse = RatingResponseBuilder.Initialize()
|
||||
.Add(item.RatingMedia, x => x.Rating)
|
||||
.Build();
|
||||
|
||||
return RequestResult.Ok(ratingResponse);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetMediaRatingByUser(long mediaId, long userId)
|
||||
{
|
||||
RatingMedia? rating = await database.RatingsMedia.FirstOrDefaultAsync(x => x.MediaId == mediaId && x.AccountId == userId);
|
||||
if (rating is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
return RequestResult.Ok(rating.Rating);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutMediaRating(long mediaId, RatingRequest data)
|
||||
{
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
long userId = userService.GetUserId();
|
||||
|
||||
RatingMedia? rating = item.RatingMedia.FirstOrDefault(x => x.AccountId == userId);
|
||||
if (rating is not null)
|
||||
{
|
||||
rating.Rating = data.Rating;
|
||||
}
|
||||
else
|
||||
{
|
||||
rating = new RatingMedia
|
||||
{
|
||||
AccountId = userId,
|
||||
MediaId = mediaId,
|
||||
Rating = data.Rating
|
||||
};
|
||||
await database.RatingsMedia.AddAsync(rating);
|
||||
}
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteMediaRating(long mediaId)
|
||||
{
|
||||
long userId = userService.GetUserId();
|
||||
|
||||
RatingMedia? item = await database.RatingsMedia.FirstOrDefaultAsync(x => x.MediaId == mediaId && x.AccountId == userId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
database.RatingsMedia.Attach(item);
|
||||
database.RatingsMedia.Remove(item);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
public async Task<RequestResult> PostMediaView(long mediaId)
|
||||
{
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
DateOnly dateNow = DateOnly.FromDateTime(DateTime.Now);
|
||||
ViewCountMedia? viewCount = await database.ViewCountsMedia.FirstOrDefaultAsync(x => x.MediaId == mediaId && x.Date == dateNow);
|
||||
if (viewCount is null)
|
||||
{
|
||||
viewCount = new ViewCountMedia
|
||||
{
|
||||
MediaId = mediaId,
|
||||
Date = dateNow,
|
||||
ViewCount = 1
|
||||
};
|
||||
await database.ViewCountsMedia.AddAsync(viewCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
viewCount.ViewCount++;
|
||||
}
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Poster
|
||||
|
||||
public async Task<RequestResult> GetMediaPoster(long mediaId)
|
||||
{
|
||||
Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.BadRequest();
|
||||
}
|
||||
|
||||
MediaPosterImage? poster = media.MediaPosterImage;
|
||||
if (poster is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
MediaPosterResponse data = new MediaPosterResponse(poster);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutMediaPoster(long mediaId, MediaPosterRequest data)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.BadRequest();
|
||||
}
|
||||
|
||||
if (media.MediaPosterImage is null)
|
||||
{
|
||||
MediaPosterImage image = data.CreateMediaPosterImage();
|
||||
await database.MediaPosterImages.AddAsync(image);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
media.MediaPosterImageId = image.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
data.UpdateMediaPosterImage(media.MediaPosterImage);
|
||||
}
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
MediaPosterResponse returnData = new MediaPosterResponse(media.MediaPosterImage!);
|
||||
return RequestResult.Ok(returnData);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteMediaPoster(long mediaId)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
|
||||
if (media?.MediaPosterImage != null)
|
||||
{
|
||||
database.MediaPosterImages.Attach(media.MediaPosterImage);
|
||||
database.MediaPosterImages.Remove(media.MediaPosterImage);
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Photos
|
||||
|
||||
public async Task<RequestResult> GetMediaPhotos(long mediaId, PhotoQueryParameters queryParameters)
|
||||
{
|
||||
Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<MediaPhotoImage> imagesRaw = await database.MediaPhotoImages.Where(x => x.MediaId == mediaId).ToListAsync();
|
||||
IEnumerable<PhotoResponse> images = imagesRaw.Select(x => new PhotoResponse(x));
|
||||
images = queryParameters.PrepareData(images);
|
||||
return RequestResult.Ok(images);
|
||||
}
|
||||
|
||||
public Task<RequestResult> GetMediaPhotoRandomBackground(long mediaId)
|
||||
{
|
||||
MediaPhotoImage? image = database.MediaPhotoImages.Where(x => x.MediaId == mediaId && x.MediaPhotoImageBackground != null).Random();
|
||||
if (image is null)
|
||||
{
|
||||
return Task.FromResult<RequestResult>(RequestResult.NotFound());
|
||||
}
|
||||
|
||||
PhotoResponse data = new PhotoResponse(image);
|
||||
return Task.FromResult<RequestResult>(RequestResult.Ok(data));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostMediaPhoto(long mediaId, MediaPhotoRequest data)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
MediaPhotoImage item = data.CreateMediaPhotoImage(mediaId);
|
||||
await database.MediaPhotoImages.AddAsync(item);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
MediaPhotoImageBackground? background = data.CreateMediaPhotoImageBackground(item.Id);
|
||||
if (background is not null)
|
||||
{
|
||||
await database.MediaPhotoImageBackgrounds.AddAsync(background);
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RequestResult.Created($"photos/{item.Id}", new PhotoResponse(item));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Roles
|
||||
|
||||
public async Task<RequestResult> GetMediaAllActorRoles(long mediaId, ActorRoleMediaQueryParameters queryParameters)
|
||||
{
|
||||
Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<PersonActorRole> dataRaw = await database.PersonActorRoles.Where(x => x.MediaId == mediaId).ToListAsync();
|
||||
IEnumerable<ActorRoleResponse> data = dataRaw.Select(x => new ActorRoleResponse(x));
|
||||
data = queryParameters.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostMediaActorRole(long mediaId, ActorRoleMediaRequest data)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
PersonActorRole item = data.CreateActorRole(mediaId);
|
||||
await database.PersonActorRoles.AddAsync(item);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"roles/actor/{item.Id}", new ActorRoleResponse(item));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetMediaAllCreatorRoles(long mediaId, CreatorRoleMediaQueryParameters queryParameters)
|
||||
{
|
||||
Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<PersonCreatorRole> dataRaw = await database.PersonCreatorRoles.Where(x => x.MediaId == mediaId).ToListAsync();
|
||||
IEnumerable<CreatorRoleResponse> data = dataRaw.Select(x => new CreatorRoleResponse(x));
|
||||
data = queryParameters.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostMediaCreatorRole(long mediaId, CreatorRoleMediaRequest data)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
PersonCreatorRole item = data.CreateCreatorRole(mediaId);
|
||||
await database.PersonCreatorRoles.AddAsync(item);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"roles/creator/{item.Id}", new CreatorRoleResponse(item));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database.Model\WatchIt.Database.Model\WatchIt.Database.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\..\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.User\WatchIt.WebAPI.Services.Utility.User.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services.Controllers.Common\WatchIt.WebAPI.Services.Controllers.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SimpleToolkit.Extensions" Version="1.7.7" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,15 +0,0 @@
|
||||
using WatchIt.Common.Model.Movies;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Movies;
|
||||
|
||||
public interface IMoviesControllerService
|
||||
{
|
||||
Task<RequestResult> GetAllMovies(MovieQueryParameters query);
|
||||
Task<RequestResult> GetMovie(long id);
|
||||
Task<RequestResult> PostMovie(MovieRequest data);
|
||||
Task<RequestResult> PutMovie(long id, MovieRequest data);
|
||||
Task<RequestResult> DeleteMovie(long id);
|
||||
|
||||
Task<RequestResult> GetMoviesViewRank(int first, int days);
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Common.Model.Movies;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Movies;
|
||||
|
||||
public class MoviesControllerService : IMoviesControllerService
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly DatabaseContext _database;
|
||||
|
||||
private readonly IUserService _userService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public MoviesControllerService(DatabaseContext database, IUserService userService)
|
||||
{
|
||||
_database = database;
|
||||
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<RequestResult> GetAllMovies(MovieQueryParameters query)
|
||||
{
|
||||
IEnumerable<MediaMovie> rawData = await _database.MediaMovies.ToListAsync();
|
||||
IEnumerable<MovieResponse> data = rawData.Select(x => new MovieResponse(x));
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetMovie(long id)
|
||||
{
|
||||
MediaMovie? item = await _database.MediaMovies.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
MovieResponse data = new MovieResponse(item);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostMovie(MovieRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Media mediaItem = data.CreateMedia();
|
||||
await _database.Media.AddAsync(mediaItem);
|
||||
await _database.SaveChangesAsync();
|
||||
MediaMovie mediaMovieItem = data.CreateMediaMovie(mediaItem.Id);
|
||||
await _database.MediaMovies.AddAsync(mediaMovieItem);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"movies/{mediaItem.Id}", new MovieResponse(mediaMovieItem));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutMovie(long id, MovieRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
MediaMovie? item = await _database.MediaMovies.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
data.UpdateMediaMovie(item);
|
||||
data.UpdateMedia(item.Media);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteMovie(long id)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
MediaMovie? item = await _database.MediaMovies.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
_database.MediaMovies.Attach(item);
|
||||
_database.MediaMovies.Remove(item);
|
||||
_database.MediaPosterImages.Attach(item.Media.MediaPosterImage!);
|
||||
_database.MediaPosterImages.Remove(item.Media.MediaPosterImage!);
|
||||
_database.MediaPhotoImages.AttachRange(item.Media.MediaPhotoImages);
|
||||
_database.MediaPhotoImages.RemoveRange(item.Media.MediaPhotoImages);
|
||||
_database.MediaGenres.AttachRange(item.Media.MediaGenres);
|
||||
_database.MediaGenres.RemoveRange(item.Media.MediaGenres);
|
||||
_database.MediaProductionCountries.AttachRange(item.Media.MediaProductionCountries);
|
||||
_database.MediaProductionCountries.RemoveRange(item.Media.MediaProductionCountries);
|
||||
_database.PersonActorRoles.AttachRange(item.Media.PersonActorRoles);
|
||||
_database.PersonActorRoles.RemoveRange(item.Media.PersonActorRoles);
|
||||
_database.PersonCreatorRoles.AttachRange(item.Media.PersonCreatorRoles);
|
||||
_database.PersonCreatorRoles.RemoveRange(item.Media.PersonCreatorRoles);
|
||||
_database.RatingsMedia.AttachRange(item.Media.RatingMedia);
|
||||
_database.RatingsMedia.RemoveRange(item.Media.RatingMedia);
|
||||
_database.ViewCountsMedia.AttachRange(item.Media.ViewCountsMedia);
|
||||
_database.ViewCountsMedia.RemoveRange(item.Media.ViewCountsMedia);
|
||||
_database.Media.Attach(item.Media);
|
||||
_database.Media.Remove(item.Media);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
public async Task<RequestResult> GetMoviesViewRank(int first, int days)
|
||||
{
|
||||
if (first < 1 || days < 1)
|
||||
{
|
||||
return RequestResult.BadRequest();
|
||||
}
|
||||
|
||||
DateOnly startDate = DateOnly.FromDateTime(DateTime.Now).AddDays(-days);
|
||||
IEnumerable<MediaMovie> rawData = await _database.MediaMovies.OrderByDescending(x => x.Media.ViewCountsMedia.Where(y => y.Date >= startDate)
|
||||
.Sum(y => y.ViewCount))
|
||||
.ThenBy(x => x.Id)
|
||||
.Take(first)
|
||||
.ToListAsync();
|
||||
|
||||
IEnumerable<MovieResponse> data = rawData.Select(x => new MovieResponse(x));
|
||||
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\..\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.User\WatchIt.WebAPI.Services.Utility.User.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services.Controllers.Common\WatchIt.WebAPI.Services.Controllers.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,29 +0,0 @@
|
||||
using WatchIt.Common.Model.Persons;
|
||||
using WatchIt.Common.Model.Roles;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Persons;
|
||||
|
||||
public interface IPersonsControllerService
|
||||
{
|
||||
Task<RequestResult> GetAllPersons(PersonQueryParameters query);
|
||||
Task<RequestResult> GetPerson(long id);
|
||||
Task<RequestResult> PostPerson(PersonRequest data);
|
||||
Task<RequestResult> PutPerson(long id, PersonRequest data);
|
||||
Task<RequestResult> DeletePerson(long id);
|
||||
|
||||
Task<RequestResult> GetPersonsViewRank(int first, int days);
|
||||
Task<RequestResult> PostPersonsView(long personId);
|
||||
|
||||
Task<RequestResult> GetPersonPhoto(long id);
|
||||
Task<RequestResult> PutPersonPhoto(long id, PersonPhotoRequest data);
|
||||
Task<RequestResult> DeletePersonPhoto(long id);
|
||||
|
||||
Task<RequestResult> GetPersonAllActorRoles(long personId, ActorRolePersonQueryParameters queryParameters);
|
||||
Task<RequestResult> PostPersonActorRole(long personId, ActorRolePersonRequest data);
|
||||
Task<RequestResult> GetPersonAllCreatorRoles(long personId, CreatorRolePersonQueryParameters queryParameters);
|
||||
Task<RequestResult> PostPersonCreatorRole(long personId, CreatorRolePersonRequest data);
|
||||
|
||||
Task<RequestResult> GetPersonGlobalRating(long id);
|
||||
Task<RequestResult> GetPersonUserRating(long id, long userId);
|
||||
}
|
||||
@@ -1,365 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Common.Model.Persons;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Model.Roles;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Person;
|
||||
using WatchIt.Database.Model.Rating;
|
||||
using WatchIt.Database.Model.ViewCount;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
using Person = WatchIt.Database.Model.Person.Person;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Persons;
|
||||
|
||||
public class PersonsControllerService : IPersonsControllerService
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public PersonsControllerService(DatabaseContext database, IUserService userService)
|
||||
{
|
||||
_database = database;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<RequestResult> GetAllPersons(PersonQueryParameters query)
|
||||
{
|
||||
IEnumerable<Person> rawData = await _database.Persons.ToListAsync();
|
||||
IEnumerable<PersonResponse> data = rawData.Select(x => new PersonResponse(x));
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetPerson(long id)
|
||||
{
|
||||
Person? item = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
PersonResponse data = new PersonResponse(item);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostPerson(PersonRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Person personItem = data.CreatePerson();
|
||||
await _database.Persons.AddAsync(personItem);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"persons/{personItem.Id}", new PersonResponse(personItem));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutPerson(long id, PersonRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Person? item = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
data.UpdatePerson(item);
|
||||
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeletePerson(long id)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Person? item = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
_database.PersonCreatorRoles.AttachRange(item.PersonCreatorRoles);
|
||||
_database.PersonCreatorRoles.RemoveRange(item.PersonCreatorRoles);
|
||||
_database.PersonActorRoles.AttachRange(item.PersonActorRoles);
|
||||
_database.PersonActorRoles.RemoveRange(item.PersonActorRoles);
|
||||
_database.ViewCountsPerson.AttachRange(item.ViewCountsPerson);
|
||||
_database.ViewCountsPerson.RemoveRange(item.ViewCountsPerson);
|
||||
_database.Persons.Attach(item);
|
||||
_database.Persons.Remove(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region View count
|
||||
|
||||
public async Task<RequestResult> GetPersonsViewRank(int first, int days)
|
||||
{
|
||||
if (first < 1 || days < 1)
|
||||
{
|
||||
return RequestResult.BadRequest();
|
||||
}
|
||||
|
||||
DateOnly startDate = DateOnly.FromDateTime(DateTime.Now).AddDays(-days);
|
||||
IEnumerable<Person> rawData = await _database.Persons.OrderByDescending(x => x.ViewCountsPerson.Where(y => y.Date >= startDate)
|
||||
.Sum(y => y.ViewCount))
|
||||
.ThenBy(x => x.Id)
|
||||
.Take(first)
|
||||
.ToListAsync();
|
||||
|
||||
IEnumerable<PersonResponse> data = rawData.Select(x => new PersonResponse(x));
|
||||
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostPersonsView(long personId)
|
||||
{
|
||||
Database.Model.Media.Media? item = await _database.Media.FirstOrDefaultAsync(x => x.Id == personId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
DateOnly dateNow = DateOnly.FromDateTime(DateTime.Now);
|
||||
ViewCountPerson? viewCount = await _database.ViewCountsPerson.FirstOrDefaultAsync(x => x.PersonId == personId && x.Date == dateNow);
|
||||
if (viewCount is null)
|
||||
{
|
||||
viewCount = new ViewCountPerson
|
||||
{
|
||||
PersonId = personId,
|
||||
Date = dateNow,
|
||||
ViewCount = 1
|
||||
};
|
||||
await _database.ViewCountsPerson.AddAsync(viewCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
viewCount.ViewCount++;
|
||||
}
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Photo
|
||||
|
||||
public async Task<RequestResult> GetPersonPhoto(long id)
|
||||
{
|
||||
Person? person = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (person is null)
|
||||
{
|
||||
return RequestResult.BadRequest();
|
||||
}
|
||||
|
||||
PersonPhotoImage? photo = person.PersonPhoto;
|
||||
if (photo is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
PersonPhotoResponse data = new PersonPhotoResponse(photo);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutPersonPhoto(long id, PersonPhotoRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Person? person = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (person is null)
|
||||
{
|
||||
return RequestResult.BadRequest();
|
||||
}
|
||||
|
||||
if (person.PersonPhoto is null)
|
||||
{
|
||||
PersonPhotoImage image = data.CreatePersonPhotoImage();
|
||||
await _database.PersonPhotoImages.AddAsync(image);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
person.PersonPhotoId = image.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
data.UpdatePersonPhotoImage(person.PersonPhoto);
|
||||
}
|
||||
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
PersonPhotoResponse returnData = new PersonPhotoResponse(person.PersonPhoto);
|
||||
return RequestResult.Ok(returnData);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeletePersonPhoto(long id)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Person? person = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (person?.PersonPhoto != null)
|
||||
{
|
||||
_database.PersonPhotoImages.Attach(person.PersonPhoto);
|
||||
_database.PersonPhotoImages.Remove(person.PersonPhoto);
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Roles
|
||||
|
||||
public async Task<RequestResult> GetPersonAllActorRoles(long personId, ActorRolePersonQueryParameters queryParameters)
|
||||
{
|
||||
Database.Model.Person.Person? person = await _database.Persons.FirstOrDefaultAsync(x => x.Id == personId);
|
||||
if (person is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<PersonActorRole> dataRaw = await _database.PersonActorRoles.Where(x => x.PersonId == personId).ToListAsync();
|
||||
IEnumerable<ActorRoleResponse> data = dataRaw.Select(x => new ActorRoleResponse(x));
|
||||
data = queryParameters.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostPersonActorRole(long personId, ActorRolePersonRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Person? person = await _database.Persons.FirstOrDefaultAsync(x => x.Id == personId);
|
||||
if (person is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
PersonActorRole item = data.CreateActorRole(personId);
|
||||
await _database.PersonActorRoles.AddAsync(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"roles/actor/{item.Id}", new ActorRoleResponse(item));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetPersonAllCreatorRoles(long personId, CreatorRolePersonQueryParameters queryParameters)
|
||||
{
|
||||
Person? media = await _database.Persons.FirstOrDefaultAsync(x => x.Id == personId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
IEnumerable<PersonCreatorRole> dataRaw = await _database.PersonCreatorRoles.Where(x => x.PersonId == personId).ToListAsync();
|
||||
IEnumerable<CreatorRoleResponse> data = dataRaw.Select(x => new CreatorRoleResponse(x));
|
||||
data = queryParameters.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostPersonCreatorRole(long personId, CreatorRolePersonRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
Person? media = await _database.Persons.FirstOrDefaultAsync(x => x.Id == personId);
|
||||
if (media is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
PersonCreatorRole item = data.CreateCreatorRole(personId);
|
||||
await _database.PersonCreatorRoles.AddAsync(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"roles/creator/{item.Id}", new CreatorRoleResponse(item));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
public async Task<RequestResult> GetPersonGlobalRating(long id)
|
||||
{
|
||||
Person? item = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
RatingResponse ratingResponse = RatingResponseBuilder.Initialize()
|
||||
.Add(item.PersonActorRoles.SelectMany(x => x.RatingPersonActorRole), x => x.Rating)
|
||||
.Add(item.PersonCreatorRoles.SelectMany(x => x.RatingPersonCreatorRole), x => x.Rating)
|
||||
.Build();
|
||||
|
||||
return RequestResult.Ok(ratingResponse);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetPersonUserRating(long id, long userId)
|
||||
{
|
||||
Person? item = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
RatingResponse ratingResponse = RatingResponseBuilder.Initialize()
|
||||
.Add(item.PersonActorRoles.SelectMany(x => x.RatingPersonActorRole).Where(x => x.AccountId == userId), x => x.Rating)
|
||||
.Add(item.PersonCreatorRoles.SelectMany(x => x.RatingPersonCreatorRole).Where(x => x.AccountId == userId), x => x.Rating)
|
||||
.Build();
|
||||
|
||||
return RequestResult.Ok(ratingResponse);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\..\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.User\WatchIt.WebAPI.Services.Utility.User.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services.Controllers.Common\WatchIt.WebAPI.Services.Controllers.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,13 +0,0 @@
|
||||
using WatchIt.Common.Model.Photos;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Photos;
|
||||
|
||||
public interface IPhotosControllerService
|
||||
{
|
||||
Task<RequestResult> GetPhotoRandomBackground();
|
||||
Task<RequestResult> DeletePhoto(Guid photoId);
|
||||
|
||||
Task<RequestResult> PutPhotoBackgroundData(Guid id, PhotoBackgroundDataRequest data);
|
||||
Task<RequestResult> DeletePhotoBackgroundData(Guid id);
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SimpleToolkit.Extensions;
|
||||
using WatchIt.Common.Model.Photos;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Photos;
|
||||
|
||||
public class PhotosControllerService : IPhotosControllerService
|
||||
{
|
||||
#region FIELDS
|
||||
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONTRUCTORS
|
||||
|
||||
public PhotosControllerService(DatabaseContext database, IUserService userService)
|
||||
{
|
||||
_database = database;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public Task<RequestResult> GetPhotoRandomBackground()
|
||||
{
|
||||
MediaPhotoImage? image = _database.MediaPhotoImages.Where(x => x.MediaPhotoImageBackground != null && x.MediaPhotoImageBackground.IsUniversalBackground).Random();
|
||||
if (image is null)
|
||||
{
|
||||
return Task.FromResult<RequestResult>(RequestResult.NotFound());
|
||||
}
|
||||
|
||||
PhotoResponse data = new PhotoResponse(image);
|
||||
return Task.FromResult<RequestResult>(RequestResult.Ok(data));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeletePhoto(Guid photoId)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
MediaPhotoImage? item = await _database.MediaPhotoImages.FirstOrDefaultAsync(x => x.Id == photoId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
if (item.MediaPhotoImageBackground is not null)
|
||||
{
|
||||
_database.MediaPhotoImageBackgrounds.Attach(item.MediaPhotoImageBackground);
|
||||
_database.MediaPhotoImageBackgrounds.Remove(item.MediaPhotoImageBackground);
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
_database.MediaPhotoImages.Attach(item);
|
||||
_database.MediaPhotoImages.Remove(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Background data
|
||||
|
||||
public async Task<RequestResult> PutPhotoBackgroundData(Guid id, PhotoBackgroundDataRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
MediaPhotoImage? image = await _database.MediaPhotoImages.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (image is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
MediaPhotoImageBackground? imageBackground = image.MediaPhotoImageBackground;
|
||||
if (imageBackground is null)
|
||||
{
|
||||
imageBackground = data.CreateMediaPhotoImageBackground(id);
|
||||
await _database.MediaPhotoImageBackgrounds.AddAsync(imageBackground);
|
||||
}
|
||||
else
|
||||
{
|
||||
data.UpdateMediaPhotoImageBackground(imageBackground);
|
||||
}
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeletePhotoBackgroundData(Guid id)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
MediaPhotoImage? image = await _database.MediaPhotoImages.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (image is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
MediaPhotoImageBackground? imageBackground = image.MediaPhotoImageBackground;
|
||||
if (imageBackground is not null)
|
||||
{
|
||||
_database.MediaPhotoImageBackgrounds.Attach(imageBackground);
|
||||
_database.MediaPhotoImageBackgrounds.Remove(imageBackground);
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\..\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.User\WatchIt.WebAPI.Services.Utility.User.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services.Controllers.Common\WatchIt.WebAPI.Services.Controllers.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SimpleToolkit.Extensions" Version="1.7.7" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,32 +0,0 @@
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Model.Roles;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Roles;
|
||||
|
||||
public interface IRolesControllerService
|
||||
{
|
||||
Task<RequestResult> GetActorRole(Guid id);
|
||||
Task<RequestResult> PutActorRole(Guid id, ActorRoleUniversalRequest data);
|
||||
Task<RequestResult> DeleteActorRole(Guid id);
|
||||
Task<RequestResult> GetActorRoleRating(Guid id);
|
||||
Task<RequestResult> GetActorRoleRatingByUser(Guid id, long userId);
|
||||
Task<RequestResult> PutActorRoleRating(Guid id, RatingRequest data);
|
||||
Task<RequestResult> DeleteActorRoleRating(Guid id);
|
||||
Task<RequestResult> GetAllActorRoleTypes(RoleTypeQueryParameters query);
|
||||
Task<RequestResult> GetActorRoleType(short typeId);
|
||||
Task<RequestResult> PostActorRoleType(RoleTypeRequest data);
|
||||
Task<RequestResult> DeleteActorRoleType(short typeId);
|
||||
|
||||
Task<RequestResult> GetCreatorRole(Guid id);
|
||||
Task<RequestResult> PutCreatorRole(Guid id, CreatorRoleUniversalRequest data);
|
||||
Task<RequestResult> DeleteCreatorRole(Guid id);
|
||||
Task<RequestResult> GetCreatorRoleRating(Guid id);
|
||||
Task<RequestResult> GetCreatorRoleRatingByUser(Guid id, long userId);
|
||||
Task<RequestResult> PutCreatorRoleRating(Guid id, RatingRequest data);
|
||||
Task<RequestResult> DeleteCreatorRoleRating(Guid id);
|
||||
Task<RequestResult> GetAllCreatorRoleTypes(RoleTypeQueryParameters query);
|
||||
Task<RequestResult> GetCreatorRoleType(short typeId);
|
||||
Task<RequestResult> PostCreatorRoleType(RoleTypeRequest data);
|
||||
Task<RequestResult> DeleteCreatorRoleType(short typeId);
|
||||
}
|
||||
@@ -1,410 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Model.Roles;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Person;
|
||||
using WatchIt.Database.Model.Rating;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Roles;
|
||||
|
||||
public class RolesControllerService : IRolesControllerService
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RolesControllerService(DatabaseContext database, IUserService userService)
|
||||
{
|
||||
_database = database;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Actor
|
||||
|
||||
public async Task<RequestResult> GetActorRole(Guid id)
|
||||
{
|
||||
PersonActorRole? item = await _database.PersonActorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
ActorRoleResponse data = new ActorRoleResponse(item);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutActorRole(Guid id, ActorRoleUniversalRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
PersonActorRole? item = await _database.PersonActorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
data.UpdateActorRole(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteActorRole(Guid id)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
PersonActorRole? item = await _database.PersonActorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
_database.PersonActorRoles.Attach(item);
|
||||
_database.PersonActorRoles.Remove(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetActorRoleRating(Guid id)
|
||||
{
|
||||
PersonActorRole? item = await _database.PersonActorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
RatingResponse ratingResponse = RatingResponseBuilder.Initialize()
|
||||
.Add(item.RatingPersonActorRole, x => x.Rating)
|
||||
.Build();
|
||||
|
||||
return RequestResult.Ok(ratingResponse);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetActorRoleRatingByUser(Guid id, long userId)
|
||||
{
|
||||
RatingPersonActorRole? rating = await _database.RatingsPersonActorRole.FirstOrDefaultAsync(x => x.PersonActorRoleId == id && x.AccountId == userId);
|
||||
if (rating is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
return RequestResult.Ok(rating.Rating);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutActorRoleRating(Guid id, RatingRequest data)
|
||||
{
|
||||
PersonActorRole? item = await _database.PersonActorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
long userId = _userService.GetUserId();
|
||||
|
||||
RatingPersonActorRole? rating = item.RatingPersonActorRole.FirstOrDefault(x => x.AccountId == userId);
|
||||
if (rating is not null)
|
||||
{
|
||||
rating.Rating = data.Rating;
|
||||
}
|
||||
else
|
||||
{
|
||||
rating = new RatingPersonActorRole
|
||||
{
|
||||
AccountId = userId,
|
||||
PersonActorRoleId = id,
|
||||
Rating = data.Rating
|
||||
};
|
||||
await _database.RatingsPersonActorRole.AddAsync(rating);
|
||||
}
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteActorRoleRating(Guid id)
|
||||
{
|
||||
long userId = _userService.GetUserId();
|
||||
|
||||
RatingPersonActorRole? item = await _database.RatingsPersonActorRole.FirstOrDefaultAsync(x => x.PersonActorRoleId == id && x.AccountId == userId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
_database.RatingsPersonActorRole.Attach(item);
|
||||
_database.RatingsPersonActorRole.Remove(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetAllActorRoleTypes(RoleTypeQueryParameters query)
|
||||
{
|
||||
IEnumerable<PersonActorRoleType> rawData = await _database.PersonActorRoleTypes.ToListAsync();
|
||||
IEnumerable<RoleTypeResponse> data = rawData.Select(x => new RoleTypeResponse(x));
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetActorRoleType(short id)
|
||||
{
|
||||
PersonActorRoleType? item = await _database.PersonActorRoleTypes.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
RoleTypeResponse data = new RoleTypeResponse(item);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostActorRoleType(RoleTypeRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
PersonActorRoleType item = data.CreateActorRoleType();
|
||||
await _database.PersonActorRoleTypes.AddAsync(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"roles/actor/{item.Id}", new RoleTypeResponse(item));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteActorRoleType(short id)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
PersonActorRoleType? item = await _database.PersonActorRoleTypes.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
_database.PersonActorRoleTypes.Attach(item);
|
||||
_database.PersonActorRoleTypes.Remove(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Creator
|
||||
|
||||
public async Task<RequestResult> GetCreatorRole(Guid id)
|
||||
{
|
||||
PersonCreatorRole? item = await _database.PersonCreatorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
CreatorRoleResponse data = new CreatorRoleResponse(item);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutCreatorRole(Guid id, CreatorRoleUniversalRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
PersonCreatorRole? item = await _database.PersonCreatorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
data.UpdateCreatorRole(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteCreatorRole(Guid id)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
PersonCreatorRole? item = await _database.PersonCreatorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
_database.PersonCreatorRoles.Attach(item);
|
||||
_database.PersonCreatorRoles.Remove(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetCreatorRoleRating(Guid id)
|
||||
{
|
||||
PersonCreatorRole? item = await _database.PersonCreatorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
RatingResponse ratingResponse = RatingResponseBuilder.Initialize()
|
||||
.Add(item.RatingPersonCreatorRole, x => x.Rating)
|
||||
.Build();
|
||||
|
||||
return RequestResult.Ok(ratingResponse);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetCreatorRoleRatingByUser(Guid id, long userId)
|
||||
{
|
||||
RatingPersonCreatorRole? rating = await _database.RatingsPersonCreatorRole.FirstOrDefaultAsync(x => x.PersonCreatorRoleId == id && x.AccountId == userId);
|
||||
if (rating is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
return RequestResult.Ok(rating.Rating);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutCreatorRoleRating(Guid id, RatingRequest data)
|
||||
{
|
||||
PersonCreatorRole? item = await _database.PersonCreatorRoles.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
long userId = _userService.GetUserId();
|
||||
|
||||
RatingPersonCreatorRole? rating = item.RatingPersonCreatorRole.FirstOrDefault(x => x.AccountId == userId);
|
||||
if (rating is not null)
|
||||
{
|
||||
rating.Rating = data.Rating;
|
||||
}
|
||||
else
|
||||
{
|
||||
rating = new RatingPersonCreatorRole
|
||||
{
|
||||
AccountId = userId,
|
||||
PersonCreatorRoleId = id,
|
||||
Rating = data.Rating
|
||||
};
|
||||
await _database.RatingsPersonCreatorRole.AddAsync(rating);
|
||||
}
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteCreatorRoleRating(Guid id)
|
||||
{
|
||||
long userId = _userService.GetUserId();
|
||||
|
||||
RatingPersonCreatorRole? item = await _database.RatingsPersonCreatorRole.FirstOrDefaultAsync(x => x.PersonCreatorRoleId == id && x.AccountId == userId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
_database.RatingsPersonCreatorRole.Attach(item);
|
||||
_database.RatingsPersonCreatorRole.Remove(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetAllCreatorRoleTypes(RoleTypeQueryParameters query)
|
||||
{
|
||||
IEnumerable<PersonCreatorRoleType> rawData = await _database.PersonCreatorRoleTypes.ToListAsync();
|
||||
IEnumerable<RoleTypeResponse> data = rawData.Select(x => new RoleTypeResponse(x));
|
||||
data = query.PrepareData(data);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetCreatorRoleType(short id)
|
||||
{
|
||||
PersonCreatorRoleType? item = await _database.PersonCreatorRoleTypes.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
RoleTypeResponse data = new RoleTypeResponse(item);
|
||||
return RequestResult.Ok(data);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostCreatorRoleType(RoleTypeRequest data)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
PersonCreatorRoleType item = data.CreateCreatorRoleType();
|
||||
await _database.PersonCreatorRoleTypes.AddAsync(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Created($"roles/creator/{item.Id}", new RoleTypeResponse(item));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteCreatorRoleType(short id)
|
||||
{
|
||||
UserValidator validator = _userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
}
|
||||
|
||||
PersonCreatorRoleType? item = await _database.PersonCreatorRoleTypes.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
_database.PersonCreatorRoleTypes.Attach(item);
|
||||
_database.PersonCreatorRoleTypes.Remove(item);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user