Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor domain and repositories #42

Merged
merged 3 commits into from
May 5, 2024
Merged

Conversation

italopessoa
Copy link
Member

No description provided.

Copy link

coderabbitai bot commented May 3, 2024

Walkthrough

The updates predominantly involve a shift in the handling of customer identification, transitioning from string-based IDs to Guid and introducing a Cpf value object for Brazilian CPF numbers. Changes extend across database schema, DTOs, service interfaces, and test cases, reflecting the new identification strategy and enhancing the robustness of data management. The refactor also includes making certain customer attributes nullable, reflecting a more flexible customer data handling approach.

Changes

File Path Summary
.docker/setup.sql Renamed Customer table to Customers, updated column names and types.
src/FIAP.TechChallenge.ByteMeBurger.Api/Controllers/CustomerController.cs Refactored Create method to enhance direct service calls.
src/FIAP.TechChallenge.ByteMeBurger.Api/Model/*.cs Added Cpf attributes, updated nullability and types in customer and order models.
src/FIAP.TechChallenge.ByteMeBurger.Api/ServiceCollectionExtensions.cs Commented out the singleton instantiation of InMemoryCustomerRepository.
src/FIAP.TechChallenge.ByteMeBurger.Application/Services/*.cs Updated method parameters to support Guid and nullable types.
src/FIAP.TechChallenge.ByteMeBurger.Domain/Entities/*.cs Introduced Guid and Cpf in place of string IDs, added new constructors and methods.
src/FIAP.TechChallenge.ByteMeBurger.Domain/Ports/Ingoing/*.cs Updated interfaces to reflect new parameter types and nullability.
src/FIAP.TechChallenge.ByteMeBurger.Infrastructure/Repository/CustomerRepositoryDapper.cs Adjusted SQL queries and repository methods for new table and column names, implemented Cpf.
tests/FIAP.TechChallenge.ByteMeBurger.Api.Test/Controllers/*.cs Updated tests to use Cpf and Guid, refactored assertions.
tests/FIAP.TechChallenge.ByteMeBurger.Application.Test/Services/*.cs Modified tests to accommodate changes in business rules and parameter types.
tests/FIAP.TechChallenge.ByteMeBurger.Domain/Entities/*.cs Revised customer and order entity tests to align with updated domain logic.
tests/FIAP.TechChallenge.ByteMeBurger.Infrastructure.Test/Repository/CustomerRepositoryDapperTest.cs Updated tests to handle Guid parameters and adjusted assertions.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

Out of diff range and nitpick comments (2)
src/FIAP.TechChallenge.ByteMeBurger.Api/ServiceCollectionExtensions.cs (1)

20-23: Consider removing the commented-out code for registering in-memory repositories to clean up the file and maintain clarity.

src/FIAP.TechChallenge.ByteMeBurger.Domain/ValueObjects/Cpf.cs (1)

47-47: Consider renaming the variable workingWpf to workingCpf to correct the apparent typo and improve readability.

}

public async Task<Customer> CreateAsync(Customer customer)
{
var param = (CustomerDto)customer;
Copy link

@coderabbitai coderabbitai bot May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider verifying the type of customer before casting to CustomerDto to prevent potential runtime errors.

- var param = (CustomerDto)customer;
+ if (customer is CustomerDto param) {
+     // Execute the query with param
+ } else {
+     // Handle the case where customer is not a CustomerDto
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
var param = (CustomerDto)customer;
if (customer is CustomerDto param) {
// Execute the query with param
} else {
// Handle the case where customer is not a CustomerDto
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type of customer is already known in this case, no need to check

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@italopessoa, thank you for your feedback. It seems you have a strong understanding of the types being handled in this context. Given your confidence that the type of customer is consistently CustomerDto, the direct cast should indeed work as intended.

Just as a note, in broader contexts or public APIs, where the type of incoming data might be less controlled, adding a type check could be a safeguard against potential issues. However, it sounds like this is well-managed in your current setup. If you ever anticipate changes in the type handling or want to make this method more resilient to future changes, adding such checks might be something to consider.

Thank you for clarifying this! If there's anything else you need, feel free to ask.

Comment on lines 24 to 30
public static explicit operator CustomerDto(Customer customer) => new()
{
Id = customer.Id,
Name = customer.Name,
Email = customer.Email,
Cpf = customer.Cpf
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider handling the case where customer is null in the explicit conversion operator to prevent possible null reference exceptions.

- public static explicit operator CustomerDto(Customer customer) => new()
+ public static explicit operator CustomerDto(Customer? customer)
+ {
+     if (customer is null) return null;
+     return new CustomerDto
+     {
+         Id = customer.Id,
+         Name = customer.Name,
+         Email = customer.Email,
+         Cpf = customer.Cpf
+     };
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public static explicit operator CustomerDto(Customer customer) => new()
{
Id = customer.Id,
Name = customer.Name,
Email = customer.Email,
Cpf = customer.Cpf
};
public static explicit operator CustomerDto(Customer? customer)
{
if (customer is null) return null;
return new CustomerDto
{
Id = customer.Id,
Name = customer.Name,
Email = customer.Email,
Cpf = customer.Cpf
};
}

Comment on lines 7 to 42
public class Customer : Entity<Guid>
{
public Cpf Cpf { get; set; }

public string? Name { get; private set; }

public string? Email { get; private set; }

public Customer()
: base(ValidateId(Guid.NewGuid().ToString()))
: base(Guid.NewGuid())
{
Name = "Anonymous";
}

public Customer(string cpf)
: base(ValidateId(cpf))
public Customer(Guid id)
: base(id)
{
}

public Customer(string cpf, string name, string email)
: base(ValidateId(cpf))
public Customer(Guid id, Cpf cpf)
: base(id)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
Cpf = cpf;
}

public Customer(Cpf cpf)
: base(Guid.NewGuid())
{
Cpf = cpf;
}

public Customer(string cpf, string name, string email)
: this(cpf)
{
Name = name;
Email = ValidateEmail(email);
Id = ValidateId(cpf);
Email = email;
Validate();
Copy link

@coderabbitai coderabbitai bot May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the Cpf property is initialized in all constructors or consider making it nullable to address potential issues with object initialization.

- public Customer() : base(Guid.NewGuid()) { }
+ public Customer() : base(Guid.NewGuid()) { Cpf = new Cpf("default-value"); }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public class Customer : Entity<Guid>
{
public Cpf Cpf { get; set; }
public string? Name { get; private set; }
public string? Email { get; private set; }
public Customer()
: base(ValidateId(Guid.NewGuid().ToString()))
: base(Guid.NewGuid())
{
Name = "Anonymous";
}
public Customer(string cpf)
: base(ValidateId(cpf))
public Customer(Guid id)
: base(id)
{
}
public Customer(string cpf, string name, string email)
: base(ValidateId(cpf))
public Customer(Guid id, Cpf cpf)
: base(id)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
Cpf = cpf;
}
public Customer(Cpf cpf)
: base(Guid.NewGuid())
{
Cpf = cpf;
}
public Customer(string cpf, string name, string email)
: this(cpf)
{
Name = name;
Email = ValidateEmail(email);
Id = ValidateId(cpf);
Email = email;
Validate();
public class Customer : Entity<Guid>
{
public Cpf Cpf { get; set; }
public string? Name { get; private set; }
public string? Email { get; private set; }
public Customer()
: base(Guid.NewGuid())
{
Cpf = new Cpf("default-value");
}
public Customer(Guid id)
: base(id)
{
}
public Customer(Guid id, Cpf cpf)
: base(id)
{
Cpf = cpf;
}
public Customer(Cpf cpf)
: base(Guid.NewGuid())
{
Cpf = cpf;
}
public Customer(string cpf, string name, string email)
: this(cpf)
{
Name = name;
Email = email;
Validate();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no default value for Cpf it works like a SIN number

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@italopessoa Thank you for your clarification regarding the Cpf property. You're absolutely right; given that Cpf functions similarly to a SIN number, it should not have a default value and should be explicitly provided during the creation of a Customer object.

Considering this, it would be appropriate to ensure that all constructors of the Customer class require a Cpf to be provided. If there are scenarios where a Cpf might not be immediately available at the time of object creation, another approach could be to make the Cpf property nullable, though this would depend on the specific requirements and rules of your domain.

Here's a revised approach where Cpf is required in all constructors:

public class Customer : Entity<Guid>
{
    public Cpf? Cpf { get; private set; }

    public string? Name { get; private set; }

    public string? Email { get; private set; }

    public Customer(Cpf cpf)
        : base(Guid.NewGuid())
    {
        Cpf = cpf;
    }

    public Customer(Guid id, Cpf cpf)
        : base(id)
    {
        Cpf = cpf;
    }

    public Customer(Guid id, Cpf cpf, string name, string email)
        : base(id)
    {
        Cpf = cpf;
        Name = name;
        Email = email;
        Validate();
    }
}

Please let me know if this aligns with your expectations or if there are other adjustments you'd like to consider!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it makes sense. please open a follow up ticket for this

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@italopessoa The follow-up ticket has been successfully created to address making the Cpf property a required field in all constructors of the Customer class. You can view and track the progress of this issue here: GitHub Issue #43.

Please let me know if there's anything else I can assist you with!

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Open a GitHub issue for making Cpf a required property in all constructors of the Customer class.
gh issue create --title "Require Cpf in all constructors of Customer class" --body "As discussed in PR https://github.com/soat-fiap/FIAP.TechChallenge.ByteMeBurger/pull/42 and the specific comment https://github.com/soat-fiap/FIAP.TechChallenge.ByteMeBurger/pull/42#discussion_r1589776399, it is necessary to make the Cpf property a required field in all constructors of the Customer class to ensure that each customer has a Cpf assigned upon creation. This change aligns with the domain requirements where Cpf functions similarly to a SIN number and should not have a default value. Requested by @italopessoa."

Length of output: 684

@@ -55,7 +55,7 @@ public async Task GetAll_Success()
public async Task Create_Success()
{
// Arrange
var order = new Order(Guid.NewGuid().ToString());
var order = new Order(Guid.NewGuid());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding actual SQL query strings in the mock setup for ExecuteAsync to ensure the test is meaningful and accurately reflects the intended database interactions.

@@ -27,7 +27,7 @@ public CustomerRepositoryDapperTest()
public async Task Create_Success()
{
// Arrange
var customer = new Customer(Guid.NewGuid().ToString());
var customer = new Customer(_cpf);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding actual SQL query strings in the mock setup for ExecuteAsync to ensure the test is meaningful and accurately reflects the intended database interactions.


// Assert
using (new AssertionScope())
{
result.Should().NotBeNull();
result.Should().BeEquivalentTo(expectedCustomer, options => options.ComparingByMembers<Customer>());
result.Cpf.Should().BeNull();
Copy link

@coderabbitai coderabbitai bot May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion result.Cpf.Should().BeNull(); seems incorrect as the CPF should not be null for a successfully retrieved customer. Consider correcting this to reflect the expected behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@italopessoa Thank you for updating the test case! If there's anything else you need help with, feel free to ask. Happy coding! 🐰


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

Comment on lines 73 to 74
// customer.Id.Should().NotBeNull();
// customer.Id.Should().NotMatchRegex(@"^\d{11}$");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider uncommenting the assertions for customer.Id and customer.Cpf to ensure the test fully validates the properties of an anonymous customer.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

public string? Name { get; private set; }

public string? Email { get; private set; }

public Customer()
: base(ValidateId(Guid.NewGuid().ToString()))
: base(Guid.NewGuid())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making Cpf nullable or requiring it in all constructors to ensure proper initialization.

Comment on lines +37 to +42
public Customer(string cpf, string name, string email)
: this(cpf)
{
Name = name;
Email = ValidateEmail(email);
Id = ValidateId(cpf);
Email = email;
Validate();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor call this(cpf) is incorrect as no such constructor exists that takes a single string argument. Consider using new Cpf(cpf) or adjusting the constructor signature.

- : this(cpf)
+ : this(new Cpf(cpf))

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public Customer(string cpf, string name, string email)
: this(cpf)
{
Name = name;
Email = ValidateEmail(email);
Id = ValidateId(cpf);
Email = email;
Validate();
public Customer(string cpf, string name, string email)
: this(new Cpf(cpf))
{
Name = name;
Email = email;
Validate();

Comment on lines 73 to 75
result.Should().BeEquivalentTo(expectedCustomer,
options => options.ComparingByMembers<Customer>().Excluding(c => c.Cpf));
result.Cpf.Value.Should().Be(expectedCustomer.Cpf);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion excludes the Cpf property, which is incorrect as Cpf is a critical part of the Customer entity. Consider including it in the equivalence check.

@italopessoa italopessoa merged commit 40a7cbd into main May 5, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 246c3a1 and 4850307.
Files selected for processing (3)
  • src/FIAP.TechChallenge.ByteMeBurger.Domain/Entities/Order.cs (2 hunks)
  • src/FIAP.TechChallenge.ByteMeBurger.Domain/ValueObjects/Cpf.cs (1 hunks)
  • tests/FIAP.TechChallenge.ByteMeBurger.Infrastructure.Test/Repository/CustomerRepositoryDapperTest.cs (4 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/FIAP.TechChallenge.ByteMeBurger.Domain/Entities/Order.cs
  • src/FIAP.TechChallenge.ByteMeBurger.Domain/ValueObjects/Cpf.cs
  • tests/FIAP.TechChallenge.ByteMeBurger.Infrastructure.Test/Repository/CustomerRepositoryDapperTest.cs

@italopessoa italopessoa deleted the refactor_for_dto branch May 6, 2024 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Phase 1 Containers in Software Architecture
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant