From 4debb96d875cfd970ddcf62c1721d38d7469dd2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Jos=C3=A9=20Alves=20Pires?= Date: Wed, 3 Feb 2021 11:15:58 -0300 Subject: [PATCH] dbContextCreation --- .../DI/DependencyInjection.cs | 20 +++++++++-- .../Persistence/VirtualMindDbContext.cs | 35 +++++++++++++++++++ .../VirtuaMind.Infrastructure.csproj | 1 + .../Interfaces/IVirtualMindDbContext.cs | 14 ++++++++ .../Queries/GetCurrencyExchange.cs | 1 - .../VirtualMind.Application.csproj | 1 + .../Controllers/ExchangeController.cs | 31 ++++++++++++++-- VirtualMind.WebApp/appsettings.json | 10 +++--- 8 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 VirtuaMind.Infrastructure/Persistence/VirtualMindDbContext.cs create mode 100644 VirtualMind.Application/Interfaces/IVirtualMindDbContext.cs diff --git a/VirtuaMind.Infrastructure/DI/DependencyInjection.cs b/VirtuaMind.Infrastructure/DI/DependencyInjection.cs index 5297d56..ce6b7cd 100644 --- a/VirtuaMind.Infrastructure/DI/DependencyInjection.cs +++ b/VirtuaMind.Infrastructure/DI/DependencyInjection.cs @@ -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 @@ -8,9 +10,23 @@ public static class DependencyInjection { public static IServiceCollection AddInfrastructure(this IServiceCollection services) { - services.AddScoped(); + AddDatabase(services); + AddRestServicesDependency(services); return services; } + + private static void AddDatabase(IServiceCollection services) + { + services.AddDbContext(options => + options.UseInMemoryDatabase("VirtualMindDB")); + + services.AddScoped(provider => provider.GetService()); + } + + private static void AddRestServicesDependency(IServiceCollection services) + { + services.AddScoped(); + } } } diff --git a/VirtuaMind.Infrastructure/Persistence/VirtualMindDbContext.cs b/VirtuaMind.Infrastructure/Persistence/VirtualMindDbContext.cs new file mode 100644 index 0000000..a046443 --- /dev/null +++ b/VirtuaMind.Infrastructure/Persistence/VirtualMindDbContext.cs @@ -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 Operations { get; set; } + + public override async Task SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()) + { + foreach (Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry in ChangeTracker.Entries()) + { + switch (entry.State) + { + case EntityState.Added: + entry.Entity.Created = DateTime.Now; + break; + } + } + + var result = await base.SaveChangesAsync(cancellationToken); + + return result; + } + } +} diff --git a/VirtuaMind.Infrastructure/VirtuaMind.Infrastructure.csproj b/VirtuaMind.Infrastructure/VirtuaMind.Infrastructure.csproj index bd37091..6e00c01 100644 --- a/VirtuaMind.Infrastructure/VirtuaMind.Infrastructure.csproj +++ b/VirtuaMind.Infrastructure/VirtuaMind.Infrastructure.csproj @@ -5,6 +5,7 @@ + diff --git a/VirtualMind.Application/Interfaces/IVirtualMindDbContext.cs b/VirtualMind.Application/Interfaces/IVirtualMindDbContext.cs new file mode 100644 index 0000000..ad2956d --- /dev/null +++ b/VirtualMind.Application/Interfaces/IVirtualMindDbContext.cs @@ -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 Operations { get; set; } + + Task SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()); + } +} diff --git a/VirtualMind.Application/Queries/GetCurrencyExchange.cs b/VirtualMind.Application/Queries/GetCurrencyExchange.cs index 97341c4..cf942c4 100644 --- a/VirtualMind.Application/Queries/GetCurrencyExchange.cs +++ b/VirtualMind.Application/Queries/GetCurrencyExchange.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Threading.Tasks; using VirtualMind.Application.DTOs; -using VirtualMind.Application.Interfaces; namespace VirtualMind.Application.Queries { diff --git a/VirtualMind.Application/VirtualMind.Application.csproj b/VirtualMind.Application/VirtualMind.Application.csproj index 382af60..08ffe43 100644 --- a/VirtualMind.Application/VirtualMind.Application.csproj +++ b/VirtualMind.Application/VirtualMind.Application.csproj @@ -9,6 +9,7 @@ + diff --git a/VirtualMind.WebApp/Controllers/ExchangeController.cs b/VirtualMind.WebApp/Controllers/ExchangeController.cs index f2546ec..d9d43b4 100644 --- a/VirtualMind.WebApp/Controllers/ExchangeController.cs +++ b/VirtualMind.WebApp/Controllers/ExchangeController.cs @@ -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 @@ -12,13 +14,17 @@ namespace VirtualMind.WebApp.Controllers [Route("[controller]")] public class ExchangeController : ControllerBase { - private readonly ILogger _logger; - private readonly IMediator _mediator; + private readonly ILogger _logger; + private readonly IMediator _mediator; + private readonly IVirtualMindDbContext _virtualMindDbContext; - public ExchangeController(ILogger logger, IMediator mediator) + public ExchangeController(ILogger logger, + IMediator mediator, + IVirtualMindDbContext virtualMindDbContext) { _logger = logger; _mediator = mediator; + _virtualMindDbContext = virtualMindDbContext; } [HttpGet] @@ -28,5 +34,24 @@ public async Task> GetExchangeRate([FromQuery]GetCu return response; } + + [HttpPost] + public async Task 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(); + } } } diff --git a/VirtualMind.WebApp/appsettings.json b/VirtualMind.WebApp/appsettings.json index ad75fee..84d430b 100644 --- a/VirtualMind.WebApp/appsettings.json +++ b/VirtualMind.WebApp/appsettings.json @@ -1,10 +1,10 @@ -{ +{ "Logging": { - "LogLevel": { + "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" - } - }, -"AllowedHosts": "*" + } + }, + "AllowedHosts": "*" }