Skip to content

Commit

Permalink
fix: fix problem with the connection of the data base and the data ma…
Browse files Browse the repository at this point in the history
…nagement of entires
  • Loading branch information
JorgeHB69 committed Sep 12, 2024
1 parent 84cbdf8 commit 76468f5
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 23 deletions.
2 changes: 1 addition & 1 deletion scripts/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CREATE TABLE IF NOT EXISTS product (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
description TEXT NOT NULL,
weight_gr INTEGER NOT NULL,
weight_gr INTEGER NOT NULL CHECK (weight_gr >= 0),
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT NULL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ IConfiguration configuration
new Dictionary<Type, object>()
{
{ typeof(Client), new ClientTable(_.GetRequiredService<IDbConnectionFactory>()) },
{ typeof(Product), new ProductTable(_.GetRequiredService<IDbConnectionFactory>()) },
}
));

Expand All @@ -42,6 +43,7 @@ IConfiguration configuration
private static IServiceCollection ConfigureRepositories(this IServiceCollection services)
{
_ = services.AddScoped<IRepository<Client>, ClientRepository>();
_ = services.AddScoped<IRepository<Product>, ProductRepository>();

return services;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DistributionCenter.Application.Repositories.Concretes;

using Bases;
using Contexts.Interfaces;
using Domain.Entities.Concretes;

public class ProductRepository(IContext context) : BaseRepository<Product>(context) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace DistributionCenter.Application.Tables.Components.Information.Concretes;

using Bases;

public class ProductTableInformation : BaseEntityTableInformation
{
protected override string ObtainTableName()
{
return "product";
}

protected override string ObtainGetByIdFields()
{
return "name, description, weight_gr AS Weight";
}

protected override string ObtainCreateFields()
{
return "name, description, weight_gr";
}

protected override string ObtainCreateValues()
{
return "@Name, @Description, @Weight";
}

protected override string ObtainUpdateFields()
{
return "name = @Name, description = @Description, weight_gr = @Weight";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace DistributionCenter.Application.Tables.Core.Concretes;

using Bases;
using Components.Information.Concretes;
using Components.Information.Interfaces;
using Connections.Interfaces;
using Domain.Entities.Concretes;

public class ProductTable(IDbConnectionFactory dbConnectionFactory) : BaseDapperTable<Product>(dbConnectionFactory)
{
public override ITableInformation GetInformation()
{
return new ProductTableInformation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class Product : BaseEntity
{
public required string Name { get; set; }
public required string Description { get; set; }
public required double Weight { get; set; }
public required int Weight { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class CreateProductDto : ICreateDto<Product>
{
public required string Name { get; init; }
public required string Description { get; init; }
public required double Weight { get; init; }
public required int Weight { get; init; }

public Product ToEntity()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public CreateProductValidator()
.SizeRange(3, 128, "The description has a limit of 128 characters");

_ = RuleFor(static product => product.Weight)
.DecimalSize(2, "The weight should contain only 2 decimals");
.NonNegatives("The weight can't be a negative number")
.NumberRange(0, 1000000, "The weight has a limit of 1000000 gr/ml");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,22 @@ public static IValidationBuilder<double> DecimalSize(
}, message);
}

public static IValidationBuilder<int> NumberRange(
this IValidationBuilder<int> builder,
uint? min,
uint? max,
string message)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

return builder.AddRule(x => (x >= (min ?? uint.MinValue) && x <= (max ?? uint.MaxValue)), message);
}

public static IValidationBuilder<int> NonNegatives(this IValidationBuilder<int> builder, string message)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

return builder.AddRule(x => x >= 0, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void CreateProductTest()
// Define Input and output
string expectedName = "Pepsico Zero 2Lts.";
string expectedDescription = "Fresh analcoholic beverage without sugar.";
double expectedWeight = 10.53;
uint expectedWeight = 15325;

// Execute actual operation
Product product = new()
Expand All @@ -34,8 +34,8 @@ public void EditProductTest()
string editedName = "Pepsico Zero 3Lts.";
string previousDescription = "Fresh analcoholic beverage without sugar.";
string editedDescription = "Fresh analcoholic beverage without sugar for diabetics.";
double previousWeight = 10.53;
double editedWeight = 200f;
uint previousWeight = 234567;
uint editedWeight = 123;

Product product = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void ToEntity_ReturnsCorrectClient()
{
Name = "Pepsi Light Popular",
Description = "Some long description",
Weight = 1000f,
Weight = 1000,
};

// Execute actual operation
Expand All @@ -37,15 +37,15 @@ public void VerifyThatTheDataWasValidatedSuccessfully()
{
Name = "Sh",
Description = "or",
Weight = 13.5352342,
Weight = 532342,
};

CreateProductDto validDto =
new()
{
Name = "Valid Name 2",
Description = "Valid Description",
Weight = 13.53,
Weight = 48253,
};

// Execute actual operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class UpdateProductDtoTests
public void FromEntity_UpdatesAndReturnsCorrectProduct()
{
// Define Input and Output
double expectedWeight = 100f;
uint expectedWeight = 100;
Product product =
new()
{
Expand Down Expand Up @@ -38,7 +38,7 @@ public void FromEntity_UpdatesAndReturnsCorrectProduct()
public void FromEntity_UpdatesWithNullsAndReturnsCorrectProduct()
{
// Define Input and Output
double expectedWeight = 100f;
uint expectedWeight = 10034;
Product product =
new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public void VerifyThanProductNameHasAtLeast3Characters()
{
Name = invalidName,
Description = "Some long and valid description",
Weight = 1234.45
Weight = 1234
};
CreateProductDto validDto = new()
{
Name = validName,
Description = "Some long and valid description",
Weight = 1234.45
Weight = 1234
};

// Verify actual result
Expand All @@ -46,13 +46,13 @@ public void VerifyThanProductNameHasLeastThan64Characters()
{
Name = invalidName,
Description = "Some long and valid description",
Weight = 10.35
Weight = 10
};
CreateProductDto validDto = new()
{
Name = validName,
Description = "Some long and valid description",
Weight = 10.43
Weight = 1867
};

// Verify actual result
Expand All @@ -73,13 +73,13 @@ public void VerifyThanProductDescriptionHasAtLeast3Characters()
{
Name = "Pepsi Zero 2Lts",
Description = invalidDescription,
Weight = 10.23
Weight = 1000
};
CreateProductDto validDto = new()
{
Name = "Pepsi Zero 2Lts",
Description = validDescription,
Weight = 10.35
Weight = 1000
};

// Verify actual result
Expand All @@ -103,13 +103,13 @@ public void VerifyThanProductDescriptionHasLeastThan128Characters()
{
Name = "Pepsi Zero 2Lts",
Description = invalidDescription,
Weight = 10.53
Weight = 1000
};
CreateProductDto validDto = new()
{
Name = "Pepsi Zero 2Lts",
Description = validDescription,
Weight = 10.34
Weight = 1000
};

// Verify actual result
Expand All @@ -118,12 +118,12 @@ public void VerifyThanProductDescriptionHasLeastThan128Characters()
}

[Fact]
public void VerifyThanProductWeightHasTwoDecimalNumbers()
public void VerifyThanProductWeightHasALimitDecimalNumbers()
{
// Define Input and Output
CreateProductValidator validator = new();
double invalidWeight = 130.5243;
double validWeight = 130.52;
uint invalidWeight = 1492850;
uint validWeight = 149285;

// Execute actual operation
CreateProductDto invalidDto = new()
Expand Down

0 comments on commit 76468f5

Please sign in to comment.