diff --git a/src/Application/Contracts/ContractService.cs b/src/Application/Contracts/ContractService.cs index d361a03e..3eba6da9 100644 --- a/src/Application/Contracts/ContractService.cs +++ b/src/Application/Contracts/ContractService.cs @@ -74,12 +74,6 @@ public IEnumerable SearchUnauthorized(string query) return ConvertToPreviews(results); } - /// - public IEnumerable FetchFavorites() - { - return _repo.Favorites; - } - /// public void UpdateContract(Contract contract) { diff --git a/src/Application/Contracts/IContractRepository.cs b/src/Application/Contracts/IContractRepository.cs index 62c9b654..3dad6dbb 100644 --- a/src/Application/Contracts/IContractRepository.cs +++ b/src/Application/Contracts/IContractRepository.cs @@ -18,12 +18,6 @@ public interface IContractRepository /// IEnumerable Recent { get; } - /// - /// Gets all contracts marked as favorites. - /// - /// The contract with a favorite mark. - IEnumerable Favorites { get; } - /// /// Adds a new contract to store. /// diff --git a/src/Application/Contracts/IContractService.cs b/src/Application/Contracts/IContractService.cs index cd1b2cbc..061282d4 100644 --- a/src/Application/Contracts/IContractService.cs +++ b/src/Application/Contracts/IContractService.cs @@ -38,12 +38,6 @@ public interface IContractService /// Whether the removal was successful. bool Remove(Guid id); - /// - /// Gets contracts marked as favorites. - /// - /// All favorite marked contracts. - IEnumerable FetchFavorites(); - /// /// Updates the contract in the repository. /// diff --git a/src/Application/Users/FavoriteContractDto.cs b/src/Application/Users/FavoriteContractDto.cs new file mode 100644 index 00000000..04d955e9 --- /dev/null +++ b/src/Application/Users/FavoriteContractDto.cs @@ -0,0 +1,22 @@ +namespace Application.Users; + +/// +/// Used to send information about making a Contract a users favorite. +/// +public class FavoriteContractDto +{ + /// + /// Gets the id of the user. + /// + public string UserName { get; init; } = string.Empty; + + /// + /// Gets the id of the contract. + /// + public Guid ContractId { get; init; } + + /// + /// Gets a value indicating whether gets if the Contract should be a favorite or not. + /// + public bool IsFavorite { get; init; } +} diff --git a/src/Application/Users/IUserRepository.cs b/src/Application/Users/IUserRepository.cs index e157f42b..1b0eae49 100644 --- a/src/Application/Users/IUserRepository.cs +++ b/src/Application/Users/IUserRepository.cs @@ -44,4 +44,19 @@ public interface IUserRepository /// Ensures that an admin user is created. /// void EnsureAdminCreated(); + + /// + /// Adds a favorite contract to store, for a certain user. + /// + /// The name of the user. + /// The id of the contract. + void AddFavorite(string userName, Guid contractId); + + /// + /// Removes a stored favorite contract, for a certain user. + /// + /// The name of the user. + /// The id of the contract. + /// If the removal was successful. + bool RemoveFavorite(string userName, Guid contractId); } diff --git a/src/Application/Users/IUserService.cs b/src/Application/Users/IUserService.cs index b442c14f..57528a64 100644 --- a/src/Application/Users/IUserService.cs +++ b/src/Application/Users/IUserService.cs @@ -1,3 +1,4 @@ +using Domain.Contracts; using Domain.Users; namespace Application.Users; @@ -40,4 +41,34 @@ public interface IUserService /// The password of the to validate. /// A response containing the generated token. AuthenticateResponse Authenticate(string username, string password); + + /// + /// Gets all contracts marked as favorite by a certain user. + /// + /// The name of the user. + /// All contracts marked as favorite by the user. + IEnumerable FetchAllFavorites(string userName); + + /// + /// Checks if the contract is marked as favorite by the user. + /// + /// The name of the user. + /// The id of the contract. + /// Whether the contract was marked as favorite. + bool IsFavorite(string userName, Guid contractId); + + /// + /// Adds a favorite contract for a certain user. + /// + /// The name of the user. + /// The id of the contract. + void AddFavorite(string userName, Guid contractId); + + /// + /// Removes a favorite contract for a certain user. + /// + /// The name of the user. + /// The id of the contract. + /// Whether the removal was successful. + bool RemoveFavorite(string userName, Guid contractId); } diff --git a/src/Application/Users/UserService.cs b/src/Application/Users/UserService.cs index 0b3d6598..3655d093 100644 --- a/src/Application/Users/UserService.cs +++ b/src/Application/Users/UserService.cs @@ -4,7 +4,7 @@ using Application.Configuration; using Application.Exceptions; - +using Domain.Contracts; using Domain.Users; using Microsoft.Extensions.Configuration; @@ -71,6 +71,30 @@ public IEnumerable FetchAllUsers() return _repo.All; } + /// + public IEnumerable FetchAllFavorites(string userName) + { + return FetchUser(userName).Favorites; + } + + /// + public bool IsFavorite(string userName, Guid contractId) + { + return FetchUser(userName).Favorites.Any(c => c.Id == contractId); + } + + /// + public void AddFavorite(string userName, Guid contractId) + { + _repo.AddFavorite(userName, contractId); + } + + /// + public bool RemoveFavorite(string userName, Guid contractId) + { + return _repo.RemoveFavorite(userName, contractId); + } + private static IEnumerable CreateClaims(User user) { var claims = new List { new("id", user.Id.ToString()), }; @@ -102,4 +126,14 @@ private string GenerateJwtToken(User user) SecurityToken? token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } + + private User FetchUser(string username) + { + User? user = _repo.Fetch(username); + + if (user == null) + throw new UserDoesNotExistException(username); + + return user; + } } diff --git a/src/Client/Pages/Contracts/ContractCard.razor b/src/Client/Pages/Contracts/ContractCard.razor index 22faca95..d0c99e39 100644 --- a/src/Client/Pages/Contracts/ContractCard.razor +++ b/src/Client/Pages/Contracts/ContractCard.razor @@ -18,7 +18,7 @@

@Contract.Name

- +
@@ -50,7 +50,7 @@ /// Called when a contract has its favorite status changed. /// [Parameter] - public EventCallback OnFavoriteChange { get; set; } = EventCallback.Empty; + public EventCallback<(Guid, bool)> OnFavoriteChange { get; set; } = EventCallback<(Guid, bool)>.Empty; private async Task AddToRecentlyViewed() { @@ -61,10 +61,9 @@ } } - private async Task FavoriteChange(Contract contract) + private async Task ChangeFavorite((Guid Id, bool IsFavorite)args) { - Contract = contract; - await OnFavoriteChange.InvokeAsync(contract); + await OnFavoriteChange.InvokeAsync(args); } } diff --git a/src/Client/Pages/Contracts/FavoriteButton.razor b/src/Client/Pages/Contracts/FavoriteButton.razor index e2504bb6..fd37e6ff 100644 --- a/src/Client/Pages/Contracts/FavoriteButton.razor +++ b/src/Client/Pages/Contracts/FavoriteButton.razor @@ -2,10 +2,12 @@ @using Newtonsoft.Json @using Domain.Contracts @using System.Text +@using Application.Users @inject HttpClient _http +@inject ISessionService _session