Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Services/e-sender/O2NextGen.ESender.Api.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "O2NextGen.ESender.Business"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "O2NextGen.ESender.Impl", "O2NextGen.ESender.Impl\O2NextGen.ESender.Impl.csproj", "{912084D1-1E1A-4170-A345-375621788E06}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "O2NextGen.ESender.Data", "O2NextGen.ESender.Data\O2NextGen.ESender.Data.csproj", "{307B36BA-BFB2-4294-93DB-6C2F3455C06A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{912084D1-1E1A-4170-A345-375621788E06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{912084D1-1E1A-4170-A345-375621788E06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{912084D1-1E1A-4170-A345-375621788E06}.Release|Any CPU.Build.0 = Release|Any CPU
{307B36BA-BFB2-4294-93DB-6C2F3455C06A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{307B36BA-BFB2-4294-93DB-6C2F3455C06A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{307B36BA-BFB2-4294-93DB-6C2F3455C06A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{307B36BA-BFB2-4294-93DB-6C2F3455C06A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using O2NextGen.ESender.Data;

namespace O2NextGen.ESender.Api.Extensions
{
internal static class DatabaseExtensions
{
internal static async Task EnsureDbUpdate(this IWebHost host)
{
using (var scope = host.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<ESenderDbContext>();
await context.Database.MigrateAsync();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
Expand All @@ -10,6 +11,7 @@
using O2NextGen.ESender.Api.Filters;
using O2NextGen.ESender.Api.Helpers;
using O2NextGen.ESender.Business.Services;
using O2NextGen.ESender.Data;
using O2NextGen.ESender.Impl.Services;

namespace O2NextGen.ESender.Api.IoC
Expand All @@ -31,11 +33,20 @@ public static TConfig ConfigurePOCO<TConfig>(this IServiceCollection services, I
services.AddSingleton(config);
return config;
}

public static IServiceCollection AddConfigEf(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration["ConnectionString"];
services.AddDbContext<ESenderDbContext>(x =>
x.UseSqlServer(connectionString));
return services;
}

public static IServiceCollection AddBusiness(this IServiceCollection services)
{
services.AddSingleton<IEmailSenderService, InMemoryEmailSenderService>();
// services.AddSingleton<IEmailSenderService, InMemoryEmailSenderService>();
// Include DataLayer
// services.AddScoped<IEmailSenderService, EmailSenderService>();
services.AddScoped<IEmailSenderService, EmailSenderService>();
//more business services...

services.AddSingleton<IEmailSender, EmailSender>();
Expand Down
2 changes: 2 additions & 0 deletions src/Services/e-sender/O2NextGen.ESender.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using O2NextGen.ESender.Api.Extensions;
using Serilog;

namespace O2NextGen.ESender.Api
Expand Down Expand Up @@ -29,6 +30,7 @@ public static async Task<int> Main(string[] args)
var host = CreateWebHostBuilder(args).Build();
Log.Information($"############### {AppName} ##############");
Log.Information("################# Starting Application #################");
await host.EnsureDbUpdate();
await host.RunAsync();
Log.Information($"============== {AppName} - state is started =====================");
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/Services/e-sender/O2NextGen.ESender.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddRequiredMvcComponents();
services.AddBusiness();
services.AddConfigEf(AppConfiguration);
services.ConfigurePOCO<SenderConfig>(AppConfiguration.GetSection("Sender"));
}

Expand Down
3 changes: 2 additions & 1 deletion src/Services/e-sender/O2NextGen.ESender.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"SmtpServerHost": "localhost",
"SmtpServerPort": "25",
"From": "support@pfr-centr.com"
}
},
"ConnectionString": "Server=localhost;Initial Catalog=O2NextGen.ESenderDb;Persist Security Info=False;User ID=sa;Password=your@Password;Connection Timeout=30;"
}

43 changes: 43 additions & 0 deletions src/Services/e-sender/O2NextGen.ESender.Data/ESenderDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using O2NextGen.ESender.Data.Entities;

namespace O2NextGen.ESender.Data
{
public class ESenderDbContext: DbContext
{
#region Fields

public DbSet<MailRequestEntity> MailRequests { get; set; }

#endregion

#region Ctors

public ESenderDbContext(DbContextOptions<ESenderDbContext> options)
: base(options)
{
}

#endregion

#region Configure

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MailRequestEntity>(ConfigureMailRequestEntity);
}

private void ConfigureMailRequestEntity(EntityTypeBuilder<MailRequestEntity> builder)
{
builder.ToTable("MailRequest");

builder.Property(ci => ci.Id)
.HasColumnType("bigint")
.ForSqlServerUseSequenceHiLo("mailrequest_hilo")
.IsRequired();
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace O2NextGen.ESender.Data.Entities
{
public class MailRequestEntity
{
public long Id { get; set; }
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace O2NextGen.ESender.Data.Migrations
{
public partial class InitDatabase : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateSequence(
name: "mailrequest_hilo",
incrementBy: 10);

migrationBuilder.CreateTable(
name: "MailRequest",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false),
From = table.Column<string>(nullable: true),
To = table.Column<string>(nullable: true),
Subject = table.Column<string>(nullable: true),
Body = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_MailRequest", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "MailRequest");

migrationBuilder.DropSequence(
name: "mailrequest_hilo");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using O2NextGen.ESender.Data;

namespace O2NextGen.ESender.Data.Migrations
{
[DbContext(typeof(ESenderDbContext))]
partial class ESenderDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.6-servicing-10079")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("Relational:Sequence:.mailrequest_hilo", "'mailrequest_hilo', '', '1', '10', '', '', 'Int64', 'False'")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("O2NextGen.ESender.Data.Entities.MailRequestEntity", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:HiLoSequenceName", "mailrequest_hilo")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.SequenceHiLo);

b.Property<string>("Body");

b.Property<string>("From");

b.Property<string>("Subject");

b.Property<string>("To");

b.HasKey("Id");

b.ToTable("MailRequest");
});
#pragma warning restore 612, 618
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.1">
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1">
</PackageReference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System.Collections.Generic;
using O2NextGen.ESender.Business.Models;
using O2NextGen.ESender.Data.Entities;

namespace O2NextGen.ESender.Impl.Mappings
{
//
// {
// public static EmailRequest ToService(this EmailRequest entity)
// {
// return entity != null ? new EmailRequest() {Id = entity.Id, Name = entity.Name} : null;
// }
//
// public static EmailRequestEntity ToEntity(this EmailRequest model)
// {
// return model != null ? new EmailRequestEntity() {Id = model.Id, Name = model.Name} : null;
// }
//
// public static IReadOnlyCollection<EmailRequest>
// ToService(this IReadOnlyCollection<EmailRequestEntity> entities) =>
// entities.MapCollection(ToService);
// }
internal static class EmailRequestMappings
{
public static EmailRequest ToService(this MailRequestEntity entity)
{
return entity != null ? new EmailRequest() {Id = entity.Id, From = entity.From, To = entity.To, Body = entity.Body,Subject = entity.Subject} : null;
}

public static MailRequestEntity ToEntity(this EmailRequest model)
{
return model != null ? new MailRequestEntity() {Id = model.Id, From = model.From, To = model.To, Body = model.Body,Subject = model.Subject} : null;
}

public static IReadOnlyCollection<EmailRequest>
ToService(this IReadOnlyCollection<MailRequestEntity> entities) =>
entities.MapCollection(ToService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<ProjectReference Include="..\O2NextGen.ESender.Business\O2NextGen.ESender.Business.csproj" />
<ProjectReference Include="..\O2NextGen.ESender.Data\O2NextGen.ESender.Data.csproj" />
</ItemGroup>

</Project>
Loading