Skip to content

Commit c51c26f

Browse files
committed
Refatoração de Cliente Endereços
1 parent cb0d6ec commit c51c26f

File tree

12 files changed

+84
-103
lines changed

12 files changed

+84
-103
lines changed

src/services/JSE.Clientes.API/Configuration/ApiConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static class ApiConfig
88
{
99
public static void AddApiConfiguration(this IServiceCollection services, IConfiguration configuration)
1010
{
11-
services.AddDbContext<ClienteContext>(options =>
11+
services.AddDbContext<ClientesContext>(options =>
1212
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
1313

1414
services.AddControllers();

src/services/JSE.Clientes.API/Configuration/DependencyInjectionConfig.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using JSE.Clientes.API.Data;
55
using JSE.Clientes.API.Data.Repository;
66
using JSE.Clientes.API.Models;
7-
using JSE.Clientes.API.Services;
87
using JSE.Core.Mediator;
98
using JSE.WebAPI.Core.User;
109
using MediatR;
@@ -27,8 +26,8 @@ public static void RegisterServices(this IServiceCollection services)
2726

2827
services.AddScoped<INotificationHandler<ClienteRegistradoEvent>, ClienteEventHandler>();
2928

30-
services.AddScoped<IClienteRepository, ClienteRepository>();
31-
services.AddScoped<ClienteContext>();
29+
services.AddScoped<IClienteRepository, ClientesRepository>();
30+
services.AddScoped<ClientesContext>();
3231
}
3332
}
3433
}

src/services/JSE.Clientes.API/Data/ClienteContext.cs renamed to src/services/JSE.Clientes.API/Data/ClientesContext.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,18 @@
88

99
namespace JSE.Clientes.API.Data
1010
{
11-
public class ClienteContext : DbContext, IUnitOfWork
11+
public sealed class ClientesContext : DbContext, IUnitOfWork
1212
{
1313
private readonly IMediatorHandler _mediatorHandler;
1414

15-
public ClienteContext(DbContextOptions<ClienteContext> options, IMediatorHandler mediatorHandler)
15+
public ClientesContext(DbContextOptions<ClientesContext> options, IMediatorHandler mediatorHandler)
1616
: base(options)
1717
{
1818
_mediatorHandler = mediatorHandler;
1919
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
2020
ChangeTracker.AutoDetectChangesEnabled = false;
2121
}
2222

23-
public ClienteContext(DbContextOptions<ClienteContext> options)
24-
: base(options)
25-
{
26-
27-
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
28-
ChangeTracker.AutoDetectChangesEnabled = true;
29-
}
30-
3123
public DbSet<Cliente> Clientes { get; set; }
3224
public DbSet<Endereco> Enderecos { get; set; }
3325

@@ -43,21 +35,21 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4335
foreach (var relationship in modelBuilder.Model.GetEntityTypes()
4436
.SelectMany(e => e.GetForeignKeys())) relationship.DeleteBehavior = DeleteBehavior.ClientSetNull;
4537

46-
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ClienteContext).Assembly);
38+
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ClientesContext).Assembly);
4739
}
4840

4941
public async Task<bool> Commit()
5042
{
51-
var success = await base.SaveChangesAsync() > 0;
52-
if (success) await _mediatorHandler.PublishEvent(this);
43+
var sucesso = await base.SaveChangesAsync() > 0;
44+
if (sucesso) await _mediatorHandler.PublicarEventos(this);
5345

54-
return success;
46+
return sucesso;
5547
}
5648
}
5749

5850
public static class MediatorExtension
5951
{
60-
public static async Task PublishEvent<T>(this IMediatorHandler mediator, T ctx) where T : DbContext
52+
public static async Task PublicarEventos<T>(this IMediatorHandler mediator, T ctx) where T : DbContext
6153
{
6254
var domainEntities = ctx.ChangeTracker
6355
.Entries<Entity>()
@@ -71,12 +63,11 @@ public static async Task PublishEvent<T>(this IMediatorHandler mediator, T ctx)
7163
.ForEach(entity => entity.Entity.LimparEventos());
7264

7365
var tasks = domainEvents
74-
.Select(async (domainEvent) =>
75-
{
66+
.Select(async (domainEvent) => {
7667
await mediator.PublishEvent(domainEvent);
7768
});
7869

7970
await Task.WhenAll(tasks);
8071
}
8172
}
82-
}
73+
}

src/services/JSE.Clientes.API/Data/Repository/ClienteRepository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace JSE.Clientes.API.Data.Repository
66
{
7-
public class ClienteRepository : IClienteRepository
7+
public class ClientesRepository : IClienteRepository
88
{
9-
private readonly ClienteContext _context;
9+
private readonly ClientesContext _context;
1010

11-
public ClienteRepository(ClienteContext context)
11+
public ClientesRepository(ClientesContext context)
1212
{
1313
_context = context;
1414
}

src/services/JSE.Clientes.API/JSE.Clientes.API.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
<DockerfileContext>..\..\..</DockerfileContext>
1010
</PropertyGroup>
1111

12-
<ItemGroup>
13-
<Compile Remove="Migrations\20241012133227_Initial.cs" />
14-
<Compile Remove="Migrations\20241012133227_Initial.Designer.cs" />
15-
</ItemGroup>
16-
1712
<ItemGroup>
1813
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
1914
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.2.0" />
@@ -30,7 +25,6 @@
3025

3126
<ItemGroup>
3227
<Folder Include="Application\Queries\" />
33-
<Folder Include="Migrations\" />
3428
</ItemGroup>
3529

3630
<ItemGroup>

src/services/JSE.Clientes.API/Migrations/20241108194137_Clientes.Designer.cs renamed to src/services/JSE.Clientes.API/Migrations/20241114155008_Clientes.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/JSE.Clientes.API/Migrations/20241108194137_Clientes.cs renamed to src/services/JSE.Clientes.API/Migrations/20241114155008_Clientes.cs

File renamed without changes.

src/services/JSE.Clientes.API/Migrations/ClienteContextModelSnapshot.cs renamed to src/services/JSE.Clientes.API/Migrations/ClientesContextModelSnapshot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace JSE.Clientes.API.Migrations
1212
{
13-
[DbContext(typeof(ClienteContext))]
14-
partial class ClienteContextModelSnapshot : ModelSnapshot
13+
[DbContext(typeof(ClientesContext))]
14+
partial class ClientesContextModelSnapshot : ModelSnapshot
1515
{
1616
protected override void BuildModel(ModelBuilder modelBuilder)
1717
{

src/web/JSE.WebApp.MVC/Models/EnderecoViewModel.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@ namespace JSE.WebApp.MVC.Models
55
{
66
public class EnderecoViewModel
77
{
8-
[Required]
8+
[Required(ErrorMessage = "O campo {0} é obrigatório")]
99
public string Logradouro { get; set; }
10-
[Required]
10+
11+
[Required(ErrorMessage = "O campo {0} é obrigatório")]
1112
[DisplayName("Número")]
1213
public string Numero { get; set; }
1314
public string Complemento { get; set; }
14-
[Required]
15+
16+
[Required(ErrorMessage = "O campo {0} é obrigatório")]
1517
public string Bairro { get; set; }
16-
[Required]
18+
19+
[Required(ErrorMessage = "O campo {0} é obrigatório")]
1720
[DisplayName("CEP")]
1821
public string Cep { get; set; }
19-
[Required]
22+
23+
[Required(ErrorMessage = "O campo {0} é obrigatório")]
2024
public string Cidade { get; set; }
21-
[Required]
25+
26+
[Required(ErrorMessage = "O campo {0} é obrigatório")]
2227
public string Estado { get; set; }
2328

2429
public override string ToString()

src/web/JSE.WebApp.MVC/Views/Pedido/_Endereco.cshtml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
if (Model.Endereco == null)
77
{
88
<div class="text-left">
9-
<a href="javascript:void(0)" class="btn btn-primary float-md-left" data-toggle="modal" data-target="#enderecoModal"> <i class="fa fa-location-arrow"></i> Adicionar Endereço</a>
9+
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#enderecoModal">
10+
Adicionar Endereço
11+
</button>
1012
</div>
1113
}
1214
else

0 commit comments

Comments
 (0)