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

feat: create and search customer by cpf #15 #1 #2 #21

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.ComponentModel.DataAnnotations;
using FIAP.TechChallenge.ByteMeBurger.Api.Model;
using FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Ingoing;
using Microsoft.AspNetCore.Mvc;

namespace FIAP.TechChallenge.ByteMeBurger.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CustomerController : ControllerBase
{
private readonly ICustomerService _customerService;

public CustomerController(ICustomerService customerService)
{
_customerService = customerService;
}

[HttpGet]
public async Task<ActionResult<CustomerDto>> GetByCpf([FromQuery] [MaxLength(14)] string cpf,
CancellationToken cancellationToken)
{
var customer = await _customerService.FindByCpfAsync(cpf);
if (customer is null)
{
return NotFound();
}

return Ok(new CustomerDto(customer));
}

[HttpPost]
public async Task<ActionResult<CustomerDto>> Create([FromBody] CreateCustomerCommand createCustomerCommand,
CancellationToken cancellationToken)
{
var createCustomerTask = string.IsNullOrWhiteSpace(createCustomerCommand.Cpf)
? _customerService.CreateAnonymousAsync()
: _customerService.CreateAsync(
createCustomerCommand.Cpf,
createCustomerCommand.Name ?? string.Empty,
createCustomerCommand.Email ?? string.Empty);

await createCustomerTask.WaitAsync(cancellationToken);

return Ok(new CustomerDto(createCustomerTask.Result));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FIAP.TechChallenge.ByteMeBurger.Application\FIAP.TechChallenge.ByteMeBurger.Application.csproj" />
<ProjectReference Include="..\FIAP.TechChallenge.ByteMeBurger.Domain\FIAP.TechChallenge.ByteMeBurger.Domain.csproj" />
<ProjectReference Include="..\FIAP.TechChallenge.ByteMeBurger.Infrastructure\FIAP.TechChallenge.ByteMeBurger.Infrastructure.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;

namespace FIAP.TechChallenge.ByteMeBurger.Api.Model;

public class CreateCustomerCommand
{
public string? Name { get; set; }

[EmailAddress]
public string? Email { get; set; }

[MaxLength(14)]
public string? Cpf { get; set; }
}
23 changes: 23 additions & 0 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Model/CustomerDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;

namespace FIAP.TechChallenge.ByteMeBurger.Api.Model;

public class CustomerDto
{
public string? Id { get; set; }

public string? Name { get; set; }

public string? Email { get; set; }

public CustomerDto()
{
}

public CustomerDto(Customer customer)
{
Id = customer.Id;
Name = customer.Name;
Email = customer.Email;
}
}
24 changes: 16 additions & 8 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
using FIAP.TechChallenge.ByteMeBurger.Application.Services;
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;
using FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Ingoing;
using FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Outgoing;
using FIAP.TechChallenge.ByteMeBurger.Infrastructure.Repository;

namespace FIAP.TechChallenge.ByteMeBurger.Api;

public class Program
Expand All @@ -10,28 +16,30 @@ public static void Main(string[] args)
builder.Services.AddAuthorization();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
// https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-8.0#log-automatic-400-responses
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();

builder.Services.AddScoped<ICustomerService, CustomerService>();
builder.Services.AddSingleton<ICustomerRepository>(new InMemoryCustomerRepository(new []
{
new Customer("663.781.241-24","Pietro Thales Anderson Rodrigues","pietro_thales_rodrigues@silicotex.net")
}));

var app = builder.Build();

// Configure the HTTP request pipeline.
// if (app.Environment.IsDevelopment())
// {
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
// }
}

app.UseHttpsRedirection();

app.UseAuthorization();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapControllers();
app.Run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="Ports\Ingoing\" />
<Folder Include="Ports\Outgoing\" />
<Folder Include="Services\" />
<ProjectReference Include="..\FIAP.TechChallenge.ByteMeBurger.Domain\FIAP.TechChallenge.ByteMeBurger.Domain.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Framework" Version="17.8.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;
using FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Ingoing;
using FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Outgoing;

namespace FIAP.TechChallenge.ByteMeBurger.Application.Services;

public class CustomerService : ICustomerService
{
private readonly ICustomerRepository _repository;

public CustomerService(ICustomerRepository repository)
{
_repository = repository;
}


public async Task<Customer?> FindByCpfAsync(string cpf)
{
return await _repository.FindByCpfAsync(cpf);
}

public async Task<Customer> CreateAnonymousAsync()
{
return await _repository.CreateAsync(new Customer());
}

public async Task<Customer> CreateAsync(string cpf)
{
return await _repository.CreateAsync(new Customer(cpf));
}

public async Task<Customer> CreateAsync(string cpf, string name, string email)
{
return await _repository.CreateAsync(new Customer(cpf, name, email));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Ingoing;

namespace FIAP.TechChallenge.ByteMeBurger.Application.Services;

public class ProductService : IProductService
{
}
italopessoa marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected static bool EqualOperator(ValueObject left, ValueObject right)
{
return false;
}
return ReferenceEquals(left, right) || left.Equals(right);
return ReferenceEquals(left, right) || left!.Equals(right!);
}

protected static bool NotEqualOperator(ValueObject left, ValueObject right)
Expand All @@ -18,7 +18,7 @@ protected static bool NotEqualOperator(ValueObject left, ValueObject right)

protected abstract IEnumerable<object> GetEqualityComponents();

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj is null || obj.GetType() != GetType())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public Customer(string cpf, string name, string email)

Name = name;
Email = ValidateEmail(email);
;
Id = ValidateId(cpf);
}

Expand Down
18 changes: 16 additions & 2 deletions src/FIAP.TechChallenge.ByteMeBurger.Domain/Entities/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
namespace FIAP.TechChallenge.ByteMeBurger.Domain.Entities;

public abstract class Entity<T>(T id)
public abstract class Entity<TId>(TId id)
{
public T Id { get; protected set; } = id;
public TId Id { get; protected set; } = id;

public override bool Equals(object? obj)
{
if (obj is Entity<TId> otherObject)
{
return Id != null && Id.Equals(otherObject.Id);
}
return false;
}

public override int GetHashCode()
{
return Id.GetHashCode();
}
italopessoa marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Validate()
}
}

public void CreateOrder()
public void Checkout()
{
Validate();
CreationDate = DateTime.UtcNow;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;

namespace FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Ingoing;

public interface ICustomerService
{
Task<Customer?> FindByCpfAsync(string cpf);

Task<Customer> CreateAnonymousAsync();

Task<Customer> CreateAsync(string cpf);


Task<Customer> CreateAsync(string cpf, string name, string email);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Ingoing;

public interface IProductService
{

}
italopessoa marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace FIAP.TechChallenge.ByteMeBurger.Application.Ports.Outgoing;

public interface IBaseRepository<in TEntityId, T> where T : new()
{
Task<IReadOnlyCollection<T>> GetAll();

Task<T> FindById(TEntityId entityId);

Task<T> Update(T entity);

Task Create(T entity);

Task Delete(TEntityId entityId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;

namespace FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Outgoing;

public interface ICustomerRepository
{
Task<Customer?> FindByCpfAsync(string cpf);

Task<Customer> CreateAsync(Customer customer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Outgoing;

public interface IProductRepository
{

}
italopessoa marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FIAP.TechChallenge.ByteMeBurger.Application\FIAP.TechChallenge.ByteMeBurger.Application.csproj" />
</ItemGroup>

</Project>
Loading