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
|
||||
}
|
||||
Reference in New Issue
Block a user