Skip to content

Commit

Permalink
Merge pull request #109 from soat-fiap/refactor
Browse files Browse the repository at this point in the history
refactor: change kubernetes configuration and order status UC
  • Loading branch information
italopessoa committed Jun 23, 2024
2 parents 638d74a + 3839945 commit 88a459e
Show file tree
Hide file tree
Showing 41 changed files with 144 additions and 190 deletions.
4 changes: 3 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ MYSQL_DATABASE=techchallenge
MYSQL_PORT=3306
MYSQL_USERID=techchallenge
MYSQL_PASSWORD=techchallengeFIAP
MercadoPago_WebhookSecret=231231312313
MercadoPago_WebhookSecret=1231231231232
MercadoPago_AccessToken=APP_USR-
MercadoPago_NotificationUrl=https://a42d-mercadopago
3 changes: 2 additions & 1 deletion FIAP.TechChallenge.ByteMeBurger.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "kubernetes", "kubernetes",
kubernetes\sc-slow.yaml = kubernetes\sc-slow.yaml
kubernetes\pvc-slow.yaml = kubernetes\pvc-slow.yaml
kubernetes\pod-db-sc.yaml = kubernetes\pod-db-sc.yaml
kubernetes\api-hpa.yaml = kubernetes\api-hpa.yaml
kubernetes\stress.sh = kubernetes\stress.sh
kubernetes\deploy.ps1 = kubernetes\deploy.ps1
kubernetes\rollback.ps1 = kubernetes\rollback.ps1
kubernetes\svc-seq.yaml = kubernetes\svc-seq.yaml
kubernetes\pod-seq.yaml = kubernetes\pod-seq.yaml
kubernetes\secret-db.yaml = kubernetes\secret-db.yaml
kubernetes\secret-mercadopago.yaml = kubernetes\secret-mercadopago.yaml
kubernetes\hpa-api.yaml = kubernetes\hpa-api.yaml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "database", "database", "{23D2EC49-62CF-46E2-A975-1951BB0493C6}"
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
- ConnectionStrings__MySql=Server=database;Database=${MYSQL_DATABASE};Uid=${MYSQL_USERID};Pwd=${MYSQL_PASSWORD};Port=${MYSQL_PORT}
- ASPNETCORE_ENVIRONMENT=Development
- Serilog__WriteTo__2__Args__serverUrl=http://seq:5341
- Serilog__Enrich__0=FromLogContext
- MercadoPago__WebhookSecret=${MercadoPago_WebhookSecret}
- MercadoPago__AccessToken=${MercadoPago_AccessToken}
- MercadoPago__NotificationUrl=${MercadoPago_NotificationUrl}
Expand Down
25 changes: 0 additions & 25 deletions kubernetes/api-hpa.yaml

This file was deleted.

8 changes: 4 additions & 4 deletions kubernetes/configmap-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ metadata:
app: api
data:
MySqlSettings__Server: "svc-mysql"
MySqlSettings__Database: "restaurante2"
MySqlSettings__Database: "tech_restaurante"
MySqlSettings__Port: "3306"
ConnectionStrings__MySql: "Server=svc-mysql;Database=tech_restaurante;Uid=techchallenge;Pwd=tech@1234;Port=3306"
ASPNETCORE_ENVIRONMENT: "Development"
Serilog__WriteTo__2__Args__serverUrl: "http://svc-seq:80"
Serilog__WriteTo__2__Args__formatter: "Serilog.Formatting.Json.JsonFormatter, Serilog"
MercadoPago__WebhookSecret: ""


MercadoPago__NotificationUrl: "/mercadopago"
Serilog__Enrich__0: "FromLogContext"
2 changes: 1 addition & 1 deletion kubernetes/configmap-db.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ metadata:
description: "This is a configmap for mysql-pod"
data:
MYSQL_ROOT_PASSWORD: "admin"
MYSQL_DATABASE: "restaurante2"
MYSQL_DATABASE: "tech_restaurante"


3 changes: 2 additions & 1 deletion kubernetes/deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
kubectl apply -f secret-db.yaml
kubectl apply -f configmap-api.yaml
kubectl apply -f configmap-db.yaml
kubectl apply -f secret-mercadopago.yaml
kubectl apply -f pv-db.yaml
kubectl apply -f pvc-db.yaml
kubectl apply -f svc-mysql.yaml
kubectl apply -f pod-mysql.yaml

kubectl apply -f deployment-api.yaml
kubectl apply -f svc-api.yaml
kubectl apply -f api-hpa.yaml
kubectl apply -f hpa-api.yaml

kubectl apply -f svc-seq.yaml
kubectl apply -f pod-seq.yaml
6 changes: 4 additions & 2 deletions kubernetes/deployment-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ spec:
app: api
spec:
containers:
- name: container-pod-api
image: ghcr.io/soat-fiap/fiap.techchallenge.bytemeburger/api:latest
- name: api-container
image: techchallenge/api:latest
ports:
- name: liveness-port
containerPort: 8080
Expand Down Expand Up @@ -60,3 +60,5 @@ spec:
envFrom:
- configMapRef:
name: configmap-api
- secretRef:
name: secret-mercadopago
4 changes: 2 additions & 2 deletions kubernetes/pod-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-api
name: api-pod
labels:
app: api
spec:
containers:
- name: container-pod-api
- name: api-container
image: techchallenge/api
ports:
- containerPort: 80
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/pod-db-sc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ metadata:
app: mysql-pod
spec:
containers:
- name: mysql-pod
- name: mysql-container
image: ghcr.io/soat-fiap/fiap.techchallenge.bytemeburger/db:latest
volumeMounts:
- mountPath: /var/lib/mysql
Expand Down
5 changes: 2 additions & 3 deletions kubernetes/pod-mysql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ metadata:
app: mysql-pod
spec:
containers:
- name: mysql-pod
image: ghcr.io/soat-fiap/fiap.techchallenge.bytemeburger/db:latest
- name: mysql-container
image: techchallenge/db:latest
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-persistent-storage
ports:
-
- containerPort: 80
imagePullPolicy: IfNotPresent
env:
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/pod-seq.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ metadata:
app: seq
spec:
containers:
- name: seq-pod
- name: seq-container
image: datalust/seq:latest
ports:
- containerPort: 80
Expand Down
2 changes: 2 additions & 0 deletions kubernetes/pvc-db.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-db
labels:
app: pod-mysql
spec:
accessModes:
- ReadWriteOnce
Expand Down
10 changes: 7 additions & 3 deletions kubernetes/rollback.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
# LICENSE file in the root directory of this source tree.

# Set-Location -Path "C:\path\to\your\yaml\files"
kubectl delete -f api-hpa.yaml
kubectl delete -f hpa-api.yaml
kubectl delete -f deployment-api.yaml
kubectl delete -f svc-api.yaml

kubectl delete -f pod-mysql.yaml
kubectl delete -f svc-mysql.yaml
kubectl delete -f pvc-db.yaml
kubectl delete -f pvc-db-slow.yaml
kubectl delete -f pvc-slow.yaml
kubectl delete -f pv-db.yaml

kubectl delete -f secret-mercadopago.yaml
kubectl delete -f secret-db.yaml
kubectl delete -f configmap-db.yaml
kubectl delete -f configmap-api.yaml
kubectl delete -f secret-db.yaml

kubectl delete -f svc-seq.yaml
kubectl delete -f pod-seq.yaml
4 changes: 2 additions & 2 deletions kubernetes/secret-db.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ metadata:
app: mysql-pod
type: Opaque
data:
MYSQL_USER: "dGVjaGNoYWxsZW5nZQ=="
MYSQL_PASSWORD: "dGVjaEAxMjM0"
MYSQL_USER: "dGVjaGNoYWxsZW5nZQ==" #techchallenge
MYSQL_PASSWORD: "dGVjaEAxMjM0" #tech@1234
16 changes: 16 additions & 0 deletions kubernetes/secret-mercadopago.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2024, Italo Pessoa (https://github.com/italopessoa)
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

apiVersion: v1
kind: Secret
metadata:
name: secret-mercadopago
labels:
app: api-pod
type: Opaque
data:
MercadoPago__WebhookSecret: "=="
MercadoPago__AccessToken: "=="
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,6 @@ public async Task<ActionResult<OrderDto>> Get(Guid id, CancellationToken cancell
return Ok(new OrderDto(order));
}

/// <summary>
/// Checkout order
/// </summary>
/// <param name="command">Checkout order command.</param>
/// <param name="cancellationToken">Cancellation token</param>
[Obsolete("This endpoint will be removed in the future.")]
[Route("checkout")]
[HttpPost]
public async Task<ActionResult<OrderDto>> Checkout([FromBody] CheckoutOrderCommandDto command,
CancellationToken cancellationToken)
{
logger.LogInformation("Checkout order: {Id}", command.Id);
await orderService.CheckoutAsync(command.Id);
return Ok();
}

/// <summary>
/// Update order status
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void OnOrderStatusChanged(object? sender, OrderStatusChanged e)

private void OnOrderPaymentConfirmed(object? sender, OrderPaymentConfirmed e)
{
_logger.LogInformation("Order: {Id} payment confirmed", e.Payload.Id);
_logger.LogInformation("Order: {Id} payment confirmed", e.Payload);
}

private void OnOrderCreated(object? sender, OrderCreated e)
Expand Down
13 changes: 8 additions & 5 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json.Serialization;
using FIAP.TechChallenge.ByteMeBurger.MercadoPago.Gateway.Configuration;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Serilog;
using Serilog.Events;

namespace FIAP.TechChallenge.ByteMeBurger.Api;

Expand All @@ -27,7 +27,10 @@ public static void Main(string[] args)
try
{
builder.Host.UseSerilog((context, configuration) =>
configuration.Enrich.FromLogContext()
configuration
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.ReadFrom.Configuration(context.Configuration));

// Add services to the container.
Expand Down Expand Up @@ -92,7 +95,7 @@ public static void Main(string[] args)
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();

AddHealthChecks(builder);
AddHealthChecks(builder, builder.Configuration);

var app = builder.Build();
app.UseExceptionHandler();
Expand Down Expand Up @@ -128,9 +131,9 @@ public static void Main(string[] args)
}
}

private static void AddHealthChecks(WebApplicationBuilder builder)
private static void AddHealthChecks(WebApplicationBuilder builder, IConfiguration configuration)
{
builder.Services.AddHealthChecks()
.AddMySql(provider => provider.GetRequiredService<DbConnectionStringBuilder>().ConnectionString);
.AddMySql(configuration.GetConnectionString("MySql")!);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class OrderService(
ICreateOrderUseCase createOrderUseCase,
IGetOrderDetailsUseCase getOrderDetailsUseCase,
IOrderGetAllUseCase orderGetAllUseCase,
ICheckoutOrderUseCase checkoutOrderUseCase,
IOrderRepository orderRepository,
IUpdateOrderStatusUseCase updateOrderStatusUseCase)
: IOrderService
Expand All @@ -41,14 +40,8 @@ public async Task<ReadOnlyCollection<Order>> GetAllAsync(bool listAll)
return await getOrderDetailsUseCase.Execute(id);
}

public async Task CheckoutAsync(Guid id)
{
await checkoutOrderUseCase.Execute(id);
}

public async Task<bool> UpdateStatusAsync(Guid orderId, OrderStatus newStatus)
{
var order = await updateOrderStatusUseCase.Execute(orderId, newStatus);
return await orderRepository.UpdateOrderStatusAsync(order);
return await updateOrderStatusUseCase.Execute(orderId, newStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public async Task Execute(Guid orderId)

try
{
DomainEventTrigger.RaiseOrderPaymentConfirmed(order);
await _orderRepository.UpdateOrderStatusAsync(order);
DomainEventTrigger.RaiseOrderStatusChanged(order.Id, oldStatus, order.Status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ namespace FIAP.TechChallenge.ByteMeBurger.Application.UseCases.Orders;

public interface IUpdateOrderStatusUseCase
{
Task<Order> Execute(Guid orderId, OrderStatus newStatus);
Task<bool> Execute(Guid orderId, OrderStatus newStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
// LICENSE file in the root directory of this source tree.

using FIAP.TechChallenge.ByteMeBurger.Domain.Base;
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;
using FIAP.TechChallenge.ByteMeBurger.Domain.Events;
using FIAP.TechChallenge.ByteMeBurger.Domain.Interfaces;
using FIAP.TechChallenge.ByteMeBurger.Domain.ValueObjects;

namespace FIAP.TechChallenge.ByteMeBurger.Application.UseCases.Orders;

public class UpdateOrderStatusUseCase(IOrderRepository repository) : IUpdateOrderStatusUseCase
{
public async Task<Order> Execute(Guid orderId, OrderStatus newStatus)
public async Task<bool> Execute(Guid orderId, OrderStatus newStatus)
{
var order = await repository.GetAsync(orderId);
if (order == null)
Expand All @@ -26,6 +26,7 @@ public async Task<Order> Execute(Guid orderId, OrderStatus newStatus)
throw new DomainException("Order is already completed.");
}

var oldStatus = order.Status;
Action updateStatus = newStatus switch
{
OrderStatus.InPreparation => order.ConfirmPayment,
Expand All @@ -35,6 +36,13 @@ public async Task<Order> Execute(Guid orderId, OrderStatus newStatus)
};

updateStatus.Invoke();
return order;

var updated = await repository.UpdateOrderStatusAsync(order);
if (updated)
{
DomainEventTrigger.RaiseOrderStatusChanged(order.Id, oldStatus, order.Status);
}

return updated;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CreatePaymentUseCase(IPaymentGateway paymentGateway, IOrderRepository ord
_orderRepository = orderRepository;
}

public async Task<Domain.Entities.Payment> Execute(Guid orderId)
public async Task<Domain.Entities.Payment?> Execute(Guid orderId)
{
var order = await _orderRepository.GetAsync(orderId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace FIAP.TechChallenge.ByteMeBurger.Application.UseCases.Payment;

public interface ICreatePaymentUseCase
{
Task<Domain.Entities.Payment> Execute(Guid orderId);
Task<Domain.Entities.Payment?> Execute(Guid orderId);
}
Loading

0 comments on commit 88a459e

Please sign in to comment.