Skip to content

Commit

Permalink
Merge pull request #28 from soat-fiap/1-create-endpoint-to-register-c…
Browse files Browse the repository at this point in the history
…ustomers

feat: create endpoint to register customers
  • Loading branch information
italopessoa committed May 1, 2024
2 parents a8e831d + dd8c8b8 commit 9e9c246
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 97 deletions.
8 changes: 7 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ MONGO_DATABASE=mongo_database
MONGO_PORT=27017

MONGO_INITDB_ROOT_USERNAME=mongo_user
MONGO_INITDB_ROOT_PASSWORD=mongo_password
MONGO_INITDB_ROOT_PASSWORD=mongo_password

MYSQL_SERVER=localhost
MYSQL_DATABASE=techchallenge
MYSQL_PORT=3306
MYSQL_USERID=techchallenge
MYSQL_PASSWORD=techchallengeFIAP
11 changes: 9 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
context: ./
dockerfile: src/FIAP.TechChallenge.ByteMeBurger.Api/Dockerfile
tags:
- techchallenge/api3
- techchallenge/apirestaurante
restart: always
healthcheck:
test: curl --fail http://localhost:8080/health || exit 1
interval: 60s
retries: 5
start_period: 20s
timeout: 5s
networks:
- tech-challenge
ports:
Expand All @@ -19,6 +25,7 @@
- MySqlSettings__Port=${MYSQL_PORT}
- MySqlSettings__UserId=${MYSQL_USERID}
- MySqlSettings__Password=${MYSQL_PASSWORD}
- ASPNETCORE_ENVIRONMENT=Development

database:
container_name: mysql_database
Expand All @@ -43,7 +50,7 @@
image: adminer
restart: always
ports:
- 8082:8080
- 27017:27017

networks:
tech-challenge:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task<ActionResult<ReadOnlyCollection<OrderDto>>> Get(CancellationTo
public async Task<ActionResult<OrderDto>> Get(Guid id, CancellationToken cancellationToken)
{
if (Guid.Empty == id)
return BadRequest("Invalid OrderId");
return BadRequest("Invalid OrderId: An order ID must not be empty.");

var order = await _orderService.GetAsync(id);
if (order is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.MySql" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3"/>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;

namespace FIAP.TechChallenge.ByteMeBurger.Api.Model;

public class CreateOrderCommandDto
{
public string? CustomerId { get; set; }
public List<OrderItemDto> Items { get; set; }
}

public class OrderItemDto
{
public Guid Id { get; set; }

public Guid OrderId { get; set; }

public Guid ProductId { get; set; }

public int Quantity { get; set; }

public decimal UnitPrice { get; set; }

public string ProductName { get; set; }

public OrderItemDto()
{
}

public OrderItemDto(OrderItem orderItem)
{
Id = orderItem.Id;
OrderId = orderItem.OrderId;
ProductId = orderItem.ProductId;
Quantity = orderItem.Quantity;
UnitPrice = orderItem.UnitPrice;
ProductName = orderItem.ProductName;
}
public List<OrderItemDto> Items { get; set; } = new();
}
33 changes: 16 additions & 17 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Model/OrderDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@ namespace FIAP.TechChallenge.ByteMeBurger.Api.Model;

public class OrderDto
{
public Guid Id { get; set; }

public CustomerDto Customer { get; set; }

public string? TrackingCode { get; set; }


public List<OrderItemDto> OrderItems { get; set; }

public decimal Total { get; set; }

public OrderStatus Status { get; set; }

public DateTime CreationDate { get; set; }

public DateTime LastUpdate { get; set; }

public OrderDto()
{

Expand All @@ -38,4 +21,20 @@ public OrderDto(Order order)
LastUpdate = order.LastUpdate;
OrderItems = order.OrderItems.Select(o => new OrderItemDto(o)).ToList();
}

public Guid Id { get; set; }

public CustomerDto Customer { get; set; }

public string? TrackingCode { get; set; }

public List<OrderItemDto> OrderItems { get; set; }

public decimal Total { get; set; }

public OrderStatus Status { get; set; }

public DateTime CreationDate { get; set; }

public DateTime LastUpdate { get; set; }
}
32 changes: 32 additions & 0 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Model/OrderItemDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;

namespace FIAP.TechChallenge.ByteMeBurger.Api.Model;

public class OrderItemDto
{
public Guid Id { get; set; }

public Guid OrderId { get; set; }

public Guid ProductId { get; set; }

public int Quantity { get; set; }

public decimal UnitPrice { get; set; }

public string ProductName { get; set; }

public OrderItemDto()
{
}

public OrderItemDto(OrderItem orderItem)
{
Id = orderItem.Id;
OrderId = orderItem.OrderId;
ProductId = orderItem.ProductId;
Quantity = orderItem.Quantity;
UnitPrice = orderItem.UnitPrice;
ProductName = orderItem.ProductName;
}
}
59 changes: 17 additions & 42 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Outgoing;
using FIAP.TechChallenge.ByteMeBurger.Domain.ValueObjects;
using FIAP.TechChallenge.ByteMeBurger.Infrastructure.Repository;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;

Expand All @@ -28,56 +30,27 @@ public static void Main(string[] args)
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();

builder.Services.AddScoped<ICustomerService, CustomerService>();
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IOrderService, OrderService>();
// builder.Services.AddSingleton<IOrderRepository, InMemoryOrderRepository>();
builder.Services.AddScoped<IOrderRepository, OrderRepositoryDapper>();
builder.Services.Configure<MySqlSettings>(builder.Configuration.GetSection(nameof(MySqlSettings)));
builder.Services.AddSingleton<DbConnectionStringBuilder>(provider =>
{
var mySqlOptions = provider.GetService<IOptions<MySqlSettings>>();
return new MySqlConnectionStringBuilder()
{
Server = mySqlOptions.Value.Server,
Database = mySqlOptions.Value.Database,
Port = mySqlOptions.Value.Port,
Password = mySqlOptions.Value.Password,
UserID = mySqlOptions.Value.UserId
};
});
builder.Services.AddTransient<IDbConnection>(provider =>
{
DbProviderFactories.RegisterFactory("MySql.Data.MySqlClient", MySqlClientFactory.Instance);
var builder = provider.GetRequiredService<DbConnectionStringBuilder>();
var providerFactory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
var conn = providerFactory.CreateConnection();
conn.ConnectionString = builder.ConnectionString;
return conn;
});
builder.Services.AddSingleton<ICustomerRepository>(new InMemoryCustomerRepository(new[]
{
new Customer("663.781.241-24", "Pietro Thales Anderson Rodrigues", "pietro_thales_rodrigues@silicotex.net")
}));
builder.ConfigServicesDependencies();
builder.Services.RegisterServices();

builder.Services.AddSingleton<IProductRepository>(new InMemoryProductRepository(new[]
{
new Product("pao com ovo", "pao com ovo", ProductCategory.Meal, 2.5m, []),
new Product("milkshake chocrante", "milkshake tijolo do bob'as", ProductCategory.SweatsNTreats, 2.5m, []),
new Product("h20", "h20", ProductCategory.Beverage, 2.5m, []),
new Product("batata frita", "batata frita", ProductCategory.FriesAndSides, 2.5m, [])
}));
builder.Services.AddHealthChecks()
.AddMySql(provider => provider.GetRequiredService<DbConnectionStringBuilder>().ConnectionString);

var app = builder.Build();

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

app.UseHealthChecks("/health", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

app.UseHttpsRedirection();

Expand All @@ -86,4 +59,6 @@ public static void Main(string[] args)
app.MapControllers();
app.Run();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.Domain.ValueObjects;
using FIAP.TechChallenge.ByteMeBurger.Infrastructure.Repository;

namespace FIAP.TechChallenge.ByteMeBurger.Api;

internal static class ServiceCollectionExtensions
{
internal static void RegisterServices(this IServiceCollection services)
{
services.AddScoped<ICustomerService, CustomerService>();
services.AddScoped<IProductService, ProductService>();
services.AddScoped<IOrderService, OrderService>();
services.AddScoped<IOrderRepository, OrderRepositoryDapper>();
services.AddScoped<ICustomerRepository, CustomerRepositoryDapper>();
services.AddSingleton<ICustomerRepository>(new InMemoryCustomerRepository(new[]
{
new Customer("663.781.241-24", "Pietro Thales Anderson Rodrigues", "pietro_thales_rodrigues@silicotex.net")
}));

services.AddSingleton<IProductRepository>(new InMemoryProductRepository(new[]
{
new Product("pao com ovo", "pao com ovo", ProductCategory.Meal, 2.5m, []),
new Product("milkshake chocrante", "milkshake tijolo do bob'as", ProductCategory.SweatsNTreats, 2.5m, []),
new Product("h20", "h20", ProductCategory.Beverage, 2.5m, []),
new Product("batata frita", "batata frita", ProductCategory.FriesAndSides, 2.5m, [])
}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Data;
using System.Data.Common;
using FIAP.TechChallenge.ByteMeBurger.Api.Configuration;
using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;

namespace FIAP.TechChallenge.ByteMeBurger.Api;

internal static class WebApplicationBuilderExtensions
{
internal static void ConfigServicesDependencies(this WebApplicationBuilder builder)
{
builder.Services.AddSingleton<DbConnectionStringBuilder>(provider =>
{
var mySqlOptions = provider.GetService<IOptions<MySqlSettings>>();
return new MySqlConnectionStringBuilder()
{
Server = mySqlOptions!.Value.Server,
Database = mySqlOptions.Value.Database,
Port = mySqlOptions.Value.Port,
Password = mySqlOptions.Value.Password,
UserID = mySqlOptions.Value.UserId
};
});
builder.Services.AddTransient<IDbConnection>(provider =>
{
DbProviderFactories.RegisterFactory("MySql.Data.MySqlClient", MySqlClientFactory.Instance);
var requiredService = provider.GetRequiredService<DbConnectionStringBuilder>();
var providerFactory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
var conn = providerFactory.CreateConnection();
conn.ConnectionString = requiredService.ConnectionString;
return conn;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Data;
using Dapper;
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;
using FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Outgoing;

namespace FIAP.TechChallenge.ByteMeBurger.Infrastructure.Repository;

public class CustomerRepositoryDapper : ICustomerRepository
{
private readonly IDbConnection _dbConnection;

public CustomerRepositoryDapper(IDbConnection dbConnection)
{
_dbConnection = dbConnection;
}

public async Task<Customer?> FindByCpfAsync(string cpf)
{
return await _dbConnection.QuerySingleOrDefaultAsync<Customer>("SELECT * FROM Customer WHERE Id=@Id",
new { Id = cpf });
}

public async Task<Customer> CreateAsync(Customer customer)
{
var rowsAffected = await _dbConnection.ExecuteAsync(
"insert into Customer (Id, Name, Email) values (@Id, @Name, @Email);",
customer);

return customer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace FIAP.TechChallenge.ByteMeBurger.Infrastructure.Repository;
[ExcludeFromCodeCoverage]
public class InMemoryOrderRepository : IOrderRepository
{
private readonly List<Order> _orders = [];
private readonly List<Order> _orders = new();

public Task<Order> CreateAsync(Order order)
{
Expand Down
Loading

0 comments on commit 9e9c246

Please sign in to comment.