Skip to content

Commit

Permalink
refactor: implement ResultPattern (#204) (#290)
Browse files Browse the repository at this point in the history
* refactor: implement ResultPattern for Expenses (#204)

* fix: add base controller (#204)

* refactor: implement ResultPatter for ExpenseItems (#204)

* refactor: implement ResultPattern for Categories (#204)

* refactor: Implement ResultPatter for Incomes (#204)

* refactor: implement ResultPattern in Projects (#204)

* refactor: change error codes to not found in case of empty object (#204)

* fix: resolve add-project e2e test fail (#204)

* Update EasyFinance.Server/EasyFinance.Server.csproj

---------

Co-authored-by: Felipe Soares <[email protected]>
  • Loading branch information
AmirAbdollahi and FelipePSoares authored Dec 21, 2024
1 parent 47807b6 commit 68413ee
Show file tree
Hide file tree
Showing 102 changed files with 1,144 additions and 537 deletions.
4 changes: 2 additions & 2 deletions EasyFinance.Application.Tests/AccessControlServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using EasyFinance.Application.Contracts.Persistence;
using EasyFinance.Application.Features.AccessControlService;
using EasyFinance.Domain.Models.AccessControl;
using EasyFinance.Domain.Models.FinancialProject;
using EasyFinance.Domain.AccessControl;
using EasyFinance.Domain.FinancialProject;
using FluentAssertions;
using Moq;

Expand Down
6 changes: 3 additions & 3 deletions EasyFinance.Application.Tests/ExpenseItemServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using EasyFinance.Common.Tests.AccessControl;
using EasyFinance.Common.Tests.Financial;
using EasyFinance.Common.Tests.FinancialProject;
using EasyFinance.Domain.Models.AccessControl;
using EasyFinance.Domain.Models.Financial;
using EasyFinance.Domain.Models.FinancialProject;
using EasyFinance.Domain.AccessControl;
using EasyFinance.Domain.Financial;
using EasyFinance.Domain.FinancialProject;
using EasyFinance.Persistence.DatabaseContext;
using EasyFinance.Persistence.Repositories;
using FluentAssertions;
Expand Down
6 changes: 3 additions & 3 deletions EasyFinance.Application.Tests/ExpenseServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using EasyFinance.Common.Tests.AccessControl;
using EasyFinance.Common.Tests.Financial;
using EasyFinance.Common.Tests.FinancialProject;
using EasyFinance.Domain.Models.AccessControl;
using EasyFinance.Domain.Models.Financial;
using EasyFinance.Domain.Models.FinancialProject;
using EasyFinance.Domain.AccessControl;
using EasyFinance.Domain.Financial;
using EasyFinance.Domain.FinancialProject;
using EasyFinance.Persistence.DatabaseContext;
using EasyFinance.Persistence.Repositories;
using FluentAssertions;
Expand Down
24 changes: 11 additions & 13 deletions EasyFinance.Application.Tests/IncomeServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using EasyFinance.Common.Tests.AccessControl;
using EasyFinance.Common.Tests.Financial;
using EasyFinance.Common.Tests.FinancialProject;
using EasyFinance.Domain.Models.AccessControl;
using EasyFinance.Domain.Models.Financial;
using EasyFinance.Domain.Models.FinancialProject;
using EasyFinance.Domain.AccessControl;
using EasyFinance.Domain.Financial;
using EasyFinance.Domain.FinancialProject;
using EasyFinance.Infrastructure;
using EasyFinance.Persistence.DatabaseContext;
using EasyFinance.Persistence.Repositories;
Expand Down Expand Up @@ -114,14 +114,13 @@ public async Task CreateAsync_WithDefaultIncome_ShouldThrowException()
var projectId = Guid.NewGuid();
Income? income = default;

var expectedMessage = string.Format(ValidationMessages.PropertyCantBeNullOrEmpty, "income") + " (Parameter 'income')";

// Act
Func<Task> func = async () => await this.IncomeService.CreateAsync(user, projectId, income);
var result = await IncomeService.CreateAsync(user, projectId, income);

// Assert
await func.Should().ThrowExactlyAsync<ArgumentNullException>()
.WithMessage(expectedMessage);
result.Succeeded.Should().BeFalse();
result.Messages.Should().HaveCount(1);
result.Messages.First().Description.Should().Be(string.Format(ValidationMessages.PropertyCantBeNullOrEmpty, nameof(income)));
}

[Fact]
Expand All @@ -132,14 +131,13 @@ public async Task CreateAsync_WithDefaultUser_ShouldThrowException()
var projectId = Guid.NewGuid();
var income = new Income();

var expectedMessage = string.Format(ValidationMessages.PropertyCantBeNullOrEmpty, "user") + " (Parameter 'user')";

// Act
Func<Task> func = async () => await this.IncomeService.CreateAsync(user, projectId, income);
var result = await IncomeService.CreateAsync(user, projectId, income);

// Assert
await func.Should().ThrowExactlyAsync<ArgumentNullException>()
.WithMessage(expectedMessage);
result.Succeeded.Should().BeFalse();
result.Messages.Should().HaveCount(1);
result.Messages.First().Description.Should().Be(string.Format(ValidationMessages.PropertyCantBeNullOrEmpty, nameof(user)));
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions EasyFinance.Application.Tests/ProjectServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using EasyFinance.Common.Tests.AccessControl;
using EasyFinance.Common.Tests.Financial;
using EasyFinance.Common.Tests.FinancialProject;
using EasyFinance.Domain.Models.AccessControl;
using EasyFinance.Domain.Models.Financial;
using EasyFinance.Domain.Models.FinancialProject;
using EasyFinance.Domain.AccessControl;
using EasyFinance.Domain.Financial;
using EasyFinance.Domain.FinancialProject;
using EasyFinance.Persistence.DatabaseContext;
using EasyFinance.Persistence.Repositories;
using FluentAssertions;
Expand Down
12 changes: 7 additions & 5 deletions EasyFinance.Application.Tests/UserServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.IdentityModel.Tokens.Jwt;
using System.Net;
using System.Security.Claims;
using System.Text;
using EasyFinance.Application.Features.ExpenseItemService;
using EasyFinance.Application.Features.ExpenseService;
using EasyFinance.Application.Features.IncomeService;
using EasyFinance.Application.Features.ProjectService;
using EasyFinance.Application.Features.UserService;
using EasyFinance.Domain.Models.AccessControl;
using EasyFinance.Domain.AccessControl;
using EasyFinance.Infrastructure;
using EasyFinance.Infrastructure.DTOs;
using FluentAssertions;
using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens;
Expand Down Expand Up @@ -170,13 +172,13 @@ public async Task GenerateConfirmationMessageAsync_ViewerUser_ShouldReturnDefaul
};

this.projectServiceMock.Setup(x => x.GetProjectsWhereUserIsSoleAdminAsync(It.IsAny<User>()))
.ReturnsAsync([]);
.ReturnsAsync(AppResponse<IList<string>>.Success([]));

// Act
var result = await this.userService.GenerateConfirmationMessageAsync(user);
var results = await this.userService.GenerateConfirmationMessageAsync(user);

// Assert
result.Should().Be(ValidationMessages.WarningMessageToUserWhoWantsToDeleteAccount);
results.Should().Be(ValidationMessages.WarningMessageToUserWhoWantsToDeleteAccount);
}

[Fact]
Expand All @@ -188,7 +190,7 @@ public async Task GenerateConfirmationMessageAsync_SoleAdminUser_ShouldReturnAdm
};

this.projectServiceMock.Setup(x => x.GetProjectsWhereUserIsSoleAdminAsync(It.IsAny<User>()))
.ReturnsAsync(["Test Project"]);
.ReturnsAsync(AppResponse<IList<string>>.Success(new List<string> { "Test Project" }));

// Act
var result = await this.userService.GenerateConfirmationMessageAsync(user);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Linq;
using EasyFinance.Domain.Models;
using EasyFinance.Domain;

namespace EasyFinance.Application.Contracts.Persistence
{
Expand Down
6 changes: 3 additions & 3 deletions EasyFinance.Application/Contracts/Persistence/IUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Threading.Tasks;
using EasyFinance.Domain.Models.AccessControl;
using EasyFinance.Domain.Models.Financial;
using EasyFinance.Domain.Models.FinancialProject;
using EasyFinance.Domain.AccessControl;
using EasyFinance.Domain.Financial;
using EasyFinance.Domain.FinancialProject;

namespace EasyFinance.Application.Contracts.Persistence
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EasyFinance.Application.DTOs.AccessControl
{
public class UserDeleteRequestDTO
{
public string ConfirmationToken { get; set; } = string.Empty;
}
}
10 changes: 10 additions & 0 deletions EasyFinance.Application/DTOs/AccessControl/UserRequestDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace EasyFinance.Application.DTOs.AccessControl
{
public class UserRequestDTO
{
public string FirstName { get; set; } = "Default";
public string LastName { get; set; } = "Default";
public string PreferredCurrency { get; set; } = string.Empty;
public string TimeZoneId { get; set; } = string.Empty;
}
}
39 changes: 39 additions & 0 deletions EasyFinance.Application/DTOs/AccessControl/UserResponseDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using EasyFinance.Domain.AccessControl;
using EasyFinance.Infrastructure.Validators;
using System;

namespace EasyFinance.Application.DTOs.AccessControl
{
public class UserResponseDTO
{
public UserResponseDTO(User user)
{
TimeZoneValidator.TryGetTimeZoneInfo(user.TimeZoneId, out var timeZoneInfo);

if (user != null)
{
Id = user.Id;
Email = user.Email;
FirstName = user.FirstName;
LastName = user.LastName;
PreferredCurrency = user.PreferredCurrency;
TimeZone = timeZoneInfo;
Enabled = user.Enabled;
IsFirstLogin = user.HasIncompletedInformation;
EmailConfirmed = user.EmailConfirmed;
TwoFactorEnabled = user.TwoFactorEnabled;
}
}

public Guid Id { get; set; }
public string Email { get; set; } = string.Empty;
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string PreferredCurrency { get; set; } = string.Empty;
public TimeZoneInfo TimeZone { get; set; }
public bool Enabled { get; set; }
public bool IsFirstLogin { get; set; }
public bool EmailConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using EasyFinance.Domain.AccessControl;
using System;

namespace EasyFinance.Application.DTOs.AccessControl
{
public class UserSearchResponseDTO
{
public UserSearchResponseDTO(User user)
{
if (user != null)
{
Id = user.Id;
FirstName = user.FirstName;
LastName = user.LastName;
Email = user.Email;
}
}

public Guid Id { get; protected set; }
public string FirstName { get; protected set; }
public string LastName { get; protected set; }
public string Email { get; protected set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace EasyFinance.Application.DTOs.Financial
{
public class BaseExpenseRequestDTO : BaseFinancialDTO
{
public ICollection<ExpenseItemRequestDTO> Items { get; set; } = new List<ExpenseItemRequestDTO>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace EasyFinance.Application.DTOs.Financial
{
public class BaseExpenseResponseDTO : BaseFinancialDTO
{
public ICollection<ExpenseItemResponseDTO> Items { get; set; } = new List<ExpenseItemResponseDTO>();
}
}
11 changes: 11 additions & 0 deletions EasyFinance.Application/DTOs/Financial/BaseFinancialDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace EasyFinance.Application.DTOs.Financial
{
public abstract class BaseFinancialDTO
{
public string Name { get; set; } = string.Empty;
public DateTime Date { get; set; }
public decimal Amount { get; set; }
}
}
7 changes: 7 additions & 0 deletions EasyFinance.Application/DTOs/Financial/CategoryRequestDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EasyFinance.Application.DTOs.Financial
{
public class CategoryRequestDTO
{
public string Name { get; set; }
}
}
13 changes: 13 additions & 0 deletions EasyFinance.Application/DTOs/Financial/CategoryResponseDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using EasyFinance.Domain.Financial;
using System;
using System.Collections.Generic;

namespace EasyFinance.Application.DTOs.Financial
{
public class CategoryResponseDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<Expense> Expenses { get; set; } = new List<Expense>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EasyFinance.Application.DTOs.Financial
{
public class ExpenseItemRequestDTO : BaseExpenseRequestDTO
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace EasyFinance.Application.DTOs.Financial
{
public class ExpenseItemResponseDTO : BaseExpenseResponseDTO
{
public Guid Id { get; set; }
}
}
7 changes: 7 additions & 0 deletions EasyFinance.Application/DTOs/Financial/ExpenseRequestDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EasyFinance.Application.DTOs.Financial
{
public class ExpenseRequestDTO : BaseExpenseRequestDTO
{
public int Budget { get; set; }
}
}
10 changes: 10 additions & 0 deletions EasyFinance.Application/DTOs/Financial/ExpenseResponseDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace EasyFinance.Application.DTOs.Financial
{
public class ExpenseResponseDTO : BaseExpenseResponseDTO
{
public Guid Id { get; set; }
public int Budget { get; set; }
}
}
6 changes: 6 additions & 0 deletions EasyFinance.Application/DTOs/Financial/IncomeRequestDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EasyFinance.Application.DTOs.Financial
{
public class IncomeRequestDTO : BaseFinancialDTO
{
}
}
9 changes: 9 additions & 0 deletions EasyFinance.Application/DTOs/Financial/IncomeResponseDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace EasyFinance.Application.DTOs.Financial
{
public class IncomeResponseDTO : BaseFinancialDTO
{
public Guid Id { get; set; }
}
}
10 changes: 10 additions & 0 deletions EasyFinance.Application/DTOs/FinancialProject/ProjectRequestDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using EasyFinance.Domain.FinancialProject;

namespace EasyFinance.Application.DTOs.FinancialProject
{
public class ProjectRequestDTO
{
public string Name { get; set; } = string.Empty;
public ProjectType Type { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Text.Json.Serialization;
using EasyFinance.Domain.FinancialProject;

namespace EasyFinance.Application.DTOs.FinancialProject
{
public class ProjectResponseDTO
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public ProjectType Type { get; set; }
}
}
1 change: 1 addition & 0 deletions EasyFinance.Application/EasyFinance.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="8.0.10" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;
using EasyFinance.Application.Contracts.Persistence;
using EasyFinance.Domain.Models.AccessControl;
using EasyFinance.Domain.AccessControl;

namespace EasyFinance.Application.Features.AccessControlService
{
Expand Down
Loading

0 comments on commit 68413ee

Please sign in to comment.