-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Used `IRepository.GetAggregateAsync` method with a newly created `MemberPermissionsSpecification` to reduce a number of db queries.
- Loading branch information
1 parent
51b2a61
commit be2cce7
Showing
4 changed files
with
120 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using AdvancedTodoList.Core.Models.TodoLists.Members; | ||
|
||
namespace AdvancedTodoList.Core.Dtos; | ||
|
||
/// <summary> | ||
/// Represents an aggregate of a to-do list member with a role. | ||
/// </summary> | ||
public record PermissionsAggregate(RoleEssentials? Role); | ||
|
||
/// <summary> | ||
/// Represents role's permissions and priority. | ||
/// </summary> | ||
public record RoleEssentials(int Priority, RolePermissions Permissions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
AdvancedTodoList.Infrastructure/Specifications/MemberPermissionsSpecification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using AdvancedTodoList.Core.Models.TodoLists.Members; | ||
using AdvancedTodoList.Core.Specifications; | ||
using AdvancedTodoList.Infrastructure.Services.Auth; | ||
using System.Linq.Expressions; | ||
|
||
namespace AdvancedTodoList.Infrastructure.Specifications; | ||
|
||
/// <summary> | ||
/// Represents a specification for obtaining an aggregate containg the to-do list member | ||
/// and his/her/their role, used in <see cref="PermissionsChecker" />. | ||
/// </summary> | ||
/// <param name="todoListId">ID of the to-do list.</param> | ||
/// <param name="userId">ID of the user.</param> | ||
public class MemberPermissionsSpecification(string todoListId, string userId) : ISpecification<TodoListMember> | ||
{ | ||
/// <summary> | ||
/// Gets the to-do list ID. | ||
/// </summary> | ||
public string TodoListId { get; } = todoListId; | ||
/// <summary> | ||
/// Gets the user ID. | ||
/// </summary> | ||
public string UserId { get; } = userId; | ||
|
||
/// <summary> | ||
/// Gets the criteria expression that defines the filtering conditions. | ||
/// </summary> | ||
public Expression<Func<TodoListMember, bool>> Criteria => | ||
x => x.TodoListId == TodoListId && x.UserId == UserId; | ||
|
||
/// <summary> | ||
/// Gets the list of include expressions specifying a to-do list role to be included in the query results. | ||
/// </summary> | ||
public List<Expression<Func<TodoListMember, object?>>> Includes => | ||
[ | ||
x => x.Role | ||
]; | ||
|
||
/// <summary> | ||
/// Gets the list of include strings specifying related entities to be included in the query results. | ||
/// </summary> | ||
public List<string> IncludeStrings { get; } = []; | ||
} |
50 changes: 50 additions & 0 deletions
50
AdvancedTodoList.UnitTests/Specifications/MemberPermissionsSpecificationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using AdvancedTodoList.Core.Models.TodoLists.Members; | ||
using AdvancedTodoList.Infrastructure.Specifications; | ||
|
||
namespace AdvancedTodoList.UnitTests.Specifications; | ||
|
||
public class MemberPermissionsSpecificationTests | ||
{ | ||
[Test] | ||
public void Criteria_ValidTodoListIdAndUserId_ReturnsTrue() | ||
{ | ||
// Arrange | ||
const string todoListId = "TodoId"; | ||
const string userId = "UserId"; | ||
TodoListMember member = new() | ||
{ | ||
TodoListId = todoListId, | ||
UserId = userId | ||
}; | ||
MemberPermissionsSpecification specification = new(todoListId, userId); | ||
var criteria = specification.Criteria.Compile(); | ||
|
||
// Act | ||
bool result = criteria(member); | ||
|
||
// Arrange | ||
Assert.That(result, Is.True); | ||
} | ||
|
||
[Test] | ||
[TestCase("TodoListId", "invalid")] | ||
[TestCase("invalid", "UserId")] | ||
[TestCase("invalid", "invalid")] | ||
public void Criteria_InvalidTodoListIdAndUserIdPairs_ReturnsFalse(string todoListId, string userId) | ||
{ | ||
// Arrange | ||
TodoListMember member = new() | ||
{ | ||
TodoListId = "TodoListId", | ||
UserId = "UserId" | ||
}; | ||
MemberPermissionsSpecification specification = new(todoListId, userId); | ||
var criteria = specification.Criteria.Compile(); | ||
|
||
// Act | ||
bool result = criteria(member); | ||
|
||
// Arrange | ||
Assert.That(result, Is.False); | ||
} | ||
} |