Skip to content

Commit

Permalink
dbContextCreation
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigojap committed Feb 3, 2021
1 parent 94b5fb9 commit 4debb96
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 11 deletions.
20 changes: 18 additions & 2 deletions VirtuaMind.Infrastructure/DI/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using VirtualMind.Application.Interfaces;
using VirtuaMind.Infrastructure.Persistence;
using VirtuaMind.Infrastructure.RestServices.ExternalServices;

namespace VirtuaMind.Infrastructure.DI
Expand All @@ -8,9 +10,23 @@ public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
{
services.AddScoped<IBancoProvinciaRestService, BancoProvinciaService>();
AddDatabase(services);
AddRestServicesDependency(services);

return services;
}

private static void AddDatabase(IServiceCollection services)
{
services.AddDbContext<VirtualMindDbContext>(options =>
options.UseInMemoryDatabase("VirtualMindDB"));

services.AddScoped<IVirtualMindDbContext>(provider => provider.GetService<VirtualMindDbContext>());
}

private static void AddRestServicesDependency(IServiceCollection services)
{
services.AddScoped<IBancoProvinciaRestService, BancoProvinciaService>();
}
}
}
35 changes: 35 additions & 0 deletions VirtuaMind.Infrastructure/Persistence/VirtualMindDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading;
using System.Threading.Tasks;
using VirtualMind.Application.Interfaces;
using VirtualMind.Domain.Entities;

namespace VirtuaMind.Infrastructure.Persistence
{
public class VirtualMindDbContext : DbContext, IVirtualMindDbContext
{
public VirtualMindDbContext(DbContextOptions options): base(options)
{
}

public DbSet<Operation> Operations { get; set; }

public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
foreach (Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<AuditableEntity> entry in ChangeTracker.Entries<AuditableEntity>())
{
switch (entry.State)
{
case EntityState.Added:
entry.Entity.Created = DateTime.Now;
break;
}
}

var result = await base.SaveChangesAsync(cancellationToken);

return result;
}
}
}
1 change: 1 addition & 0 deletions VirtuaMind.Infrastructure/VirtuaMind.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="RestSharp" Version="106.11.7" />
</ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions VirtualMind.Application/Interfaces/IVirtualMindDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
using System.Threading;
using System.Threading.Tasks;
using VirtualMind.Domain.Entities;

namespace VirtualMind.Application.Interfaces
{
public interface IVirtualMindDbContext
{
DbSet<Operation> Operations { get; set; }

Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken());
}
}
1 change: 0 additions & 1 deletion VirtualMind.Application/Queries/GetCurrencyExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using VirtualMind.Application.DTOs;
using VirtualMind.Application.Interfaces;

namespace VirtualMind.Application.Queries
{
Expand Down
1 change: 1 addition & 0 deletions VirtualMind.Application/VirtualMind.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="9.5.0" />
<PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 28 additions & 3 deletions VirtualMind.WebApp/Controllers/ExchangeController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;
using VirtualMind.Application.DTOs;
using VirtualMind.Application.Interfaces;
using VirtualMind.Application.Queries;

namespace VirtualMind.WebApp.Controllers
Expand All @@ -12,13 +14,17 @@ namespace VirtualMind.WebApp.Controllers
[Route("[controller]")]
public class ExchangeController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly IMediator _mediator;
private readonly ILogger<ExchangeController> _logger;
private readonly IMediator _mediator;
private readonly IVirtualMindDbContext _virtualMindDbContext;

public ExchangeController(ILogger<WeatherForecastController> logger, IMediator mediator)
public ExchangeController(ILogger<ExchangeController> logger,
IMediator mediator,
IVirtualMindDbContext virtualMindDbContext)
{
_logger = logger;
_mediator = mediator;
_virtualMindDbContext = virtualMindDbContext;
}

[HttpGet]
Expand All @@ -28,5 +34,24 @@ public async Task<IEnumerable<ExchangeRateDTO>> GetExchangeRate([FromQuery]GetCu

return response;
}

[HttpPost]
public async Task<IActionResult> PostExchangeOperation()
{
//await _virtualMindDbContext.Operations.AddAsync(new Domain.Entities.Operation
//{
// Currency = Domain.Enums.Currency.BRL,
// CurrentQuote = 10,
// PurchasedAmount = 15,
// RequestedAmount = 15,
// UserId = 1
//});

//await _virtualMindDbContext.SaveChangesAsync();

//var list = await _virtualMindDbContext.Operations.AsNoTracking().ToListAsync();

return Ok();
}
}
}
10 changes: 5 additions & 5 deletions VirtualMind.WebApp/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
{
"Logging": {
"LogLevel": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
},
"AllowedHosts": "*"
}

0 comments on commit 4debb96

Please sign in to comment.