Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added entityframework #15

Merged
merged 2 commits into from
Oct 3, 2021
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
5 changes: 4 additions & 1 deletion EmojiSvc/Domain/Emoji.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace EmojiSvc.Domain
using System.ComponentModel.DataAnnotations;

namespace EmojiSvc.Domain
{
public class Emoji
{
public string Unicode { get; set; } = null!;
[Key]
public string Shortcode { get; set; } = null!;
}
}
11 changes: 9 additions & 2 deletions EmojiSvc/EmojiSvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.39.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Api" Version="1.2.0-alpha4" />
<PackageReference Include="OpenTelemetry.Api" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.AWS" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta2" />
<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.GrpcCore" Version="1.0.0-beta3" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.2.0-alpha4" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc7" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc7" />
Expand Down
36 changes: 36 additions & 0 deletions EmojiSvc/Migrations/20210929204927_InitialCreate.Designer.cs

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

28 changes: 28 additions & 0 deletions EmojiSvc/Migrations/20210929204927_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace EmojiSvc.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Emojies",
columns: table => new
{
Shortcode = table.Column<string>(type: "TEXT", nullable: false),
Unicode = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Emojies", x => x.Shortcode);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Emojies");
}
}
}
36 changes: 36 additions & 0 deletions EmojiSvc/Migrations/20210929210343_Populate.Designer.cs

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

27 changes: 27 additions & 0 deletions EmojiSvc/Migrations/20210929210343_Populate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using EmojiSvc.Domain.Impl;
using EmojiSvc.Persistence.Impl;
using Microsoft.EntityFrameworkCore.Migrations;

namespace EmojiSvc.Migrations
{
public partial class Populate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("delete from Emojies;");
foreach (var emoji in EmojiDefinitions.Top100Emojis)
{
migrationBuilder.InsertData("Emojies", columns: new[] { "Shortcode", "Unicode" }, values: new object[]
{
emoji,
EmojiDefinitions.CodeMap[emoji]
});
}
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("delete from Emojies;");
}
}
}
34 changes: 34 additions & 0 deletions EmojiSvc/Migrations/EmojiContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <auto-generated />
using EmojiSvc.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace EmojiSvc.Migrations
{
[DbContext(typeof(EmojiDbContext))]
partial class EmojiContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "5.0.10");

modelBuilder.Entity("EmojiSvc.Domain.Emoji", b =>
{
b.Property<string>("Shortcode")
.HasColumnType("TEXT");

b.Property<string>("Unicode")
.IsRequired()
.HasColumnType("TEXT");

b.HasKey("Shortcode");

b.ToTable("Emojies");
});
#pragma warning restore 612, 618
}
}
}
25 changes: 25 additions & 0 deletions EmojiSvc/Persistence/EmojiDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EmojiSvc.Domain;
using Microsoft.EntityFrameworkCore;

namespace EmojiSvc.Persistence
{
public class EmojiDbContext: DbContext
{
public DbSet<Emoji> Emojies { get; set; }
public string DbPath { get; private set; }

public EmojiDbContext(DbContextOptions<EmojiDbContext> options): base(options)
{
DbPath = "emojies.db";
}

// The following configures EF to create a Sqlite database file in the
// special "local" folder for your platform.
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source={DbPath}");
}
}
1 change: 1 addition & 0 deletions EmojiSvc/Persistence/IEmojiRepo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EmojiSvc.Domain;

namespace EmojiSvc.Persistence
Expand Down
20 changes: 20 additions & 0 deletions EmojiSvc/Persistence/Impl/DatabaseEmojiRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.Linq;
using EmojiSvc.Domain;

namespace EmojiSvc.Persistence.Impl
{
public class DatabaseEmojiRepo: IEmojiRepo
{
private readonly EmojiDbContext _dbContext;

public DatabaseEmojiRepo(EmojiDbContext dbContext)
{
_dbContext = dbContext;
}
public IReadOnlyCollection<Emoji> List()
{
return _dbContext.Emojies.ToList().AsReadOnly();
}
}
}
6 changes: 5 additions & 1 deletion EmojiSvc/Persistence/Impl/InMemoryAllEmoji.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EmojiSvc.Domain;

namespace EmojiSvc.Persistence.Impl
Expand All @@ -19,6 +20,9 @@ public InMemoryAllEmoji()
}
}

public IReadOnlyCollection<Emoji> List() => _emojis.AsReadOnly();
public IReadOnlyCollection<Emoji> List()
{
return _emojis.AsReadOnly();
}
}
}
10 changes: 8 additions & 2 deletions EmojiSvc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
// .ConfigureLogging(builder => builder.AddOpenTelemetry())
.UseSerilog((context, loggerConfiguration) =>
{
loggerConfiguration
.ReadFrom.Configuration(context.Configuration)
.Enrich.FromLogContext()
Expand All @@ -35,8 +36,13 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
.Enrich.WithThreadId()
.Enrich.WithThreadName()
.Enrich.WithExceptionDetails()
.WriteTo.Console(new RenderedCompactJsonFormatter())
.WriteTo.Seq(context.Configuration["SEQ_URI"]))
.WriteTo.Console(new RenderedCompactJsonFormatter());
if (!string.IsNullOrEmpty(context.Configuration["SEQ_URI"]))
{
loggerConfiguration.WriteTo.Seq(context.Configuration["SEQ_URI"]);
}

})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
Expand Down
19 changes: 16 additions & 3 deletions EmojiSvc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using EmojiSvc.Persistence;
using EmojiSvc.Persistence.Impl;
using EmojiSvc.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using OpenTelemetry;
using OpenTelemetry.Contrib.Extensions.AWSXRay.Trace;
Expand All @@ -31,10 +32,11 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
services.AddSingleton<IEmojiRepo, InMemoryAllEmoji>();
services.AddTransient<IEmojiRepo, DatabaseEmojiRepo>();
services.AddTransient<IAllEmoji, AllEmoji>();
services.AddAutoMapper(typeof(EmojiProfile));
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); //AWS
services.AddDbContext<EmojiDbContext>(builder => builder.UseSqlite(_configuration.GetConnectionString("DefaultConnection")));
var resourceBuilder = ResourceBuilder.CreateDefault()
.AddService(Assembly.GetEntryAssembly()?.GetName().Name)
.AddTelemetrySdk();
Expand All @@ -44,14 +46,19 @@ public void ConfigureServices(IServiceCollection services)
builder
.AddAspNetCoreInstrumentation(options =>
{
options.RecordException = true;
options.RecordException = false;
options.EnableGrpcAspNetCoreSupport = true;
})
.AddXRayTraceId()
.AddAWSInstrumentation()
.AddGrpcCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddHttpClientInstrumentation(options => options.RecordException = false)
.AddGrpcClientInstrumentation()
.AddEntityFrameworkCoreInstrumentation(options =>
{
options.SetDbStatementForStoredProcedure = true;
options.SetDbStatementForText = true;
})
.SetResourceBuilder(resourceBuilder);
var consoleExport = _configuration.GetValue<bool>("CONSOLE_EXPORT");
if (consoleExport)
Expand Down Expand Up @@ -90,6 +97,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseRouting();

using (var scope = app.ApplicationServices.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<EmojiDbContext>();
db.Database.Migrate();
}

app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<EmojiGrpcSvc>();
Expand Down
3 changes: 3 additions & 0 deletions EmojiSvc/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"DefaultConnection": "DataSource=Emojies.db"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
1 change: 0 additions & 1 deletion EmojiUI/Controllers/VoteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public async Task<IActionResult> Vote([FromQuery] string choice)
{
using var activity = Activity.Current?.Source.StartActivity(nameof(Vote));
activity?.SetTag("vote.choice", choice);
activity?.SetBaggage("voteshortcode", choice);
if (await _voteService.Vote(choice))
return Accepted();
return BadRequest();
Expand Down
13 changes: 7 additions & 6 deletions EmojiUI/EmojiUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.AWS" Version="1.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.2.0-alpha4" />
<PackageReference Include="Serilog.Enrichers.Span" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.1" />
<PackageReference Include="OpenTelemetry.Api" Version="1.2.0-alpha4" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.2" />
<PackageReference Include="OpenTelemetry.Api" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.GrpcCore" Version="1.0.0-beta3" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.2.0-alpha4" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc7" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc7" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.0.0-rc7" />
Expand Down
Loading