Skip to content

Commit

Permalink
Merge pull request #174 from soat-fiap/publish_product_created_event
Browse files Browse the repository at this point in the history
feat: publish product created event
  • Loading branch information
italopessoa authored Nov 16, 2024
2 parents 9b8201a + dc63c77 commit 9a82b58
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 61 deletions.
42 changes: 11 additions & 31 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:

push:
pull_request:

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
Expand All @@ -29,7 +29,6 @@ env:

jobs:
build:
if: github.ref != 'refs/heads/main' && github.event_name != 'pull_request'
runs-on: ubuntu-latest

steps:
Expand All @@ -41,40 +40,21 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --no-restore

- name: Test
env:
AWS_REGION: us-east-1
run: dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage" --logger trx --results-directory coverage --settings tests/coverage.runsettings
run: dotnet test --no-build --verbosity normal

- name: Code Coverage Summary Report
if: false
uses: irongut/CodeCoverageSummary@v1.3.0
with:
filename: 'coverage/*/coverage.cobertura.xml'
badge: true
format: 'markdown'
output: 'both'

- name: Add Coverage PR Comment
continue-on-error: true
uses: marocchino/sticky-pull-request-comment@v2
if: false && github.event_name == 'pull_request'
with:
recreate: true
path: code-coverage-results.md

- name: Write to Job Summary
if: false
run: cat code-coverage-results.md >> $GITHUB_STEP_SUMMARY

build-and-analyze:
needs: build
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
name: SonarCloud
runs-on: windows-latest
Expand Down Expand Up @@ -128,7 +108,7 @@ jobs:
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
semantic-release:
needs: [build-and-analyze]
needs: [build]
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
name: Create Semantic Release
runs-on: ubuntu-latest
Expand All @@ -152,16 +132,16 @@ jobs:

- name: Install dependencies
run: npm clean-install

- name: Verify the integrity of provenance attestations and registry signatures for installed dependencies
run: npm audit signatures

- name: Release
id: semantic-release
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
run: |
npx semantic-release
if [ -f VERSION ]; then
VERSION=$(cat VERSION)
Expand Down Expand Up @@ -317,4 +297,4 @@ jobs:
workspace: ${{ vars.TF_WORKSPACE }}
configuration_version: ${{ needs.create-app.outputs.config_version }}
message: "Create Destroy run from GitHub Actions CI ${{ github.sha }}"
is_destroy: true
is_destroy: true
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.MySql" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.10" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3"/>
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.0.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bmb.Domain.Core" Version="0.0.10" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.12" />
<PackageReference Include="Microsoft.Build.Framework" Version="17.8.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task<Order> Execute(Customer? customer, List<SelectedProduct> selec

var trackingCode = await _orderTrackingCodeService.GetNextAsync();
var order = new Order(customer, trackingCode, products);
await _analyticsPublisher.PublishIntegrationAsync(Teste(order));
await _analyticsPublisher.PublishIntegrationAsync(MapToEvent(order));
return order;
}

Expand All @@ -48,10 +48,10 @@ public async Task<Order> Execute(Customer? customer, List<SelectedProduct> selec
return product;
}

private OrderCreated Teste(Order order)
private OrderCreated MapToEvent(Order order)
{
var orderItemsReplica = order.OrderItems.Select(i =>
new OrderCreated.OrderItemReplicaDto(i.OrderId, i.OrderId, i.ProductName, i.UnitPrice, i.Quantity));
new OrderCreated.OrderItemReplicaDto(i.ProductId, i.OrderId, i.ProductName, i.UnitPrice, i.Quantity));

var customer = default(OrderCreated.CustomerReplicaDto);
if (order.Customer is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using Bmb.Domain.Core.Events;
using Bmb.Domain.Core.Interfaces;
using Bmb.Domain.Core.ValueObjects;
using ProductCreated = Bmb.Domain.Core.Events.Integration.ProductCreated;

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

public class CreateProductUseCase(IProductRepository repository) : ICreateProductUseCase
public class CreateProductUseCase(IProductRepository repository, IDispatcher dispatcher) : ICreateProductUseCase
{
public async Task<Product> Execute(string name, string description, ProductCategory category, decimal price,
string[] images)
Expand All @@ -14,7 +15,13 @@ public async Task<Product> Execute(string name, string description, ProductCateg
product.Create();

var newProduct = await repository.CreateAsync(product);
DomainEventTrigger.RaiseProductCreated(new ProductCreated(product));
await dispatcher.PublishIntegrationAsync(MapToEvent(product), default);
return newProduct;
}

private static ProductCreated MapToEvent(Product product)
{
return new ProductCreated(product.Id, product.Name,
product.Category.ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="AWSSDK.CognitoIdentityProvider" Version="3.7.403.3" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.1" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.12" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="AWSSDK.SQS" Version="3.7.400.11" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.10" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.12" />
<PackageReference Include="Castle.Core" Version="5.1.1" />
<PackageReference Include="MassTransit" Version="8.2.5" />
<PackageReference Include="MassTransit.AmazonSQS" Version="8.2.5" />
Expand Down
17 changes: 2 additions & 15 deletions src/FIAP.TechChallenge.ByteMeBurger.Persistence/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ from Orders o
o.Updated,
o.TrackingCode,
o.CustomerId as Id,
p.Id,
p.OrderId,
o.PaymentId,
oi.ProductId,
oi.ProductName,
oi.Quantity,
oi.UnitPrice
from Orders o
inner join OrderItems oi on oi.OrderId = o.Id
left join Payments p on p.OrderId = o.Id;";
inner join OrderItems oi on oi.OrderId = o.Id;";

internal const string UpdateOrderStatusQuery =
"UPDATE Orders SET Status=@Status, Updated=@Updated WHERE Id = @Id";
Expand All @@ -46,17 +44,6 @@ from Orders o
"insert into OrderItems (OrderId, ProductId, ProductName, UnitPrice, Quantity) " +
"values (@OrderId, @ProductId, @ProductName, @UnitPrice, @Quantity);";

internal const string InsertPaymentQuery =
"insert into Payments (Id, OrderId, Status, ExternalReference, Created, PaymentType, Amount) " +
"values (@Id, @OrderId, @Status, @ExternalReference, @Created, @PaymentType, @Amount);";

internal const string GetPaymentQuery = "select * from Payments where Id = @Id;";

internal const string GetPaymentByExternalReferenceQuery = "select * from Payments where PaymentType = @PaymentType and ExternalReference = @ExternalReference;";

internal const string UpdatePaymentStatusQuery =
"UPDATE Payments SET Status=@Status, Updated=@Updated WHERE Id = @Id";

internal const string InsertProductQuery =
"insert into Products (Id, Name, Description, Category, Price, Images) values (@Id, @Name, @Description, @Category, @Price, @Images);";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bmb.Domain.Core" Version="0.0.1" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.12" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task<ReadOnlyCollection<Order>> GetAllAsync()

return order;
},
splitOn: "Id, ProductId"
splitOn: "Id , PaymentId, ProductId"
);

logger.LogInformation("Retrieved {Count} orders", ordersDictionary.Count);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Bmb.Domain.Core.Events;
using FIAP.TechChallenge.ByteMeBurger.Application.UseCases.Products;
using Bmb.Domain.Core.Interfaces;

Expand All @@ -11,11 +12,13 @@ public abstract class BaseProductsUseCaseTests
protected readonly IDeleteProductUseCase _deleteProductUseCase;
protected readonly IFindProductsByCategoryUseCase _findProductsByCategoryUseCase;
protected readonly IGetAllProductsUseCase _getAllProductsUseCase;
protected readonly Mock<IDispatcher> _dispatcher;

public BaseProductsUseCaseTests()
{
_productRepository = new Mock<IProductRepository>();
_createProductUseCase = new CreateProductUseCase(_productRepository.Object);
_dispatcher = new Mock<IDispatcher>();
_createProductUseCase = new CreateProductUseCase(_productRepository.Object, _dispatcher.Object);
_updateProductUseCase = new UpdateProductUseCase(_productRepository.Object);
_deleteProductUseCase = new DeleteProductUseCase(_productRepository.Object);
_findProductsByCategoryUseCase = new FindProductsByCategoryUseCase(_productRepository.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.1" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.12" />
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
Expand Down
4 changes: 2 additions & 2 deletions tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ resource "kubernetes_deployment" "deployment_api" {
}
env_from {
secret_ref {
name = "secret-mercadopago"
name = "secret-api"
}
}
}
Expand Down Expand Up @@ -217,4 +217,4 @@ resource "kubernetes_horizontal_pod_autoscaler_v2" "hpa_api" {
}
}
}
}
}

0 comments on commit 9a82b58

Please sign in to comment.