Skip to content

Commit

Permalink
Merge pull request #30 from soat-fiap/3-add-new-endpoint-to-register-…
Browse files Browse the repository at this point in the history
…new-products

feat: add endpoint integration to create products
  • Loading branch information
italopessoa committed May 2, 2024
2 parents 572b6ee + e972fd6 commit e40825f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .docker/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ create table Customer
);


create table Product
create table Products
(
Id char(36) not null comment 'product id'
primary key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<ActionResult> Delete(Guid id, CancellationToken cancellationTo

return Created($"/{product.Id}", product);
}
catch (Exception)
catch (Exception e)
{
return BadRequest("Unable to create the product.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ internal static void RegisterServices(this IServiceCollection services)
services.AddScoped<IOrderService, OrderService>();
services.AddScoped<IOrderRepository, OrderRepositoryDapper>();
services.AddScoped<ICustomerRepository, CustomerRepositoryDapper>();
services.AddScoped<IProductRepository, ProductRepositoryDapper>();
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, [])
}));
// 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
Expand Up @@ -43,6 +43,10 @@ public async Task<Order> CreateAsync(Order order)
transaction.Rollback();
throw;
}
finally
{
_dbConnection.Close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Collections.ObjectModel;
using System.Data;
using Dapper;
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;
using FIAP.TechChallenge.ByteMeBurger.Domain.Ports.Outgoing;
using FIAP.TechChallenge.ByteMeBurger.Domain.ValueObjects;

namespace FIAP.TechChallenge.ByteMeBurger.Infrastructure.Repository;

public class ProductRepositoryDapper : IProductRepository
{
private readonly IDbConnection _dbConnection;

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

public Task<Product?> FindByIdAsync(Guid id)
{
throw new NotImplementedException();
}

public async Task<Product> CreateAsync(Product product)
{
var affectedRows = await _dbConnection.ExecuteAsync(
"insert into Products (Id, Name, Description, Category, Price, Images) values (@Id, @Name, @Description, @Category, @Price, @Images);",
new
{
product.Id,
product.Name,
product.Description,
product.Category,
product.Price,
Images = string.Join("|", product.Images)
});
return product;
}

public Task<bool> DeleteAsync(Guid productId)
{
throw new NotImplementedException();
}

public Task<ReadOnlyCollection<Product>> GetAll()
{
throw new NotImplementedException();
}

public Task<ReadOnlyCollection<Product>> FindByCategory(ProductCategory category)
{
throw new NotImplementedException();
}

public Task<bool> UpdateAsync(Product product)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Data;
using Dapper;
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;
using FIAP.TechChallenge.ByteMeBurger.Domain.ValueObjects;
using FIAP.TechChallenge.ByteMeBurger.Infrastructure.Repository;
using FluentAssertions;
using FluentAssertions.Execution;
using JetBrains.Annotations;
using Moq;
using Moq.Dapper;

namespace FIAP.TechChallenge.ByteMeBurger.Infrastructure.Test.Repository;

[TestSubject(typeof(ProductRepositoryDapper))]
public class ProductRepositoryDapperTest
{
private readonly Mock<IDbConnection> _mockConnection;
private readonly ProductRepositoryDapper _target;

public ProductRepositoryDapperTest()
{
_mockConnection = new Mock<IDbConnection>();
_target = new ProductRepositoryDapper(_mockConnection.Object);
}

[Fact]
public async Task Create_Success()
{
// Arrange
var product = new Product("product", "description", ProductCategory.Beverage, 10, ["image1"]);

_mockConnection.SetupDapperAsync(c => c.ExecuteAsync("", null, null, null, null))
.ReturnsAsync(1);

// Act
var result = await _target.CreateAsync(product);

// Assert
using (new AssertionScope())
{
result.Should().NotBeNull();
result.Should().Be(product);
}
}
}

0 comments on commit e40825f

Please sign in to comment.