Skip to content

Commit

Permalink
Set up Entity Framework as an ORM.
Browse files Browse the repository at this point in the history
* If no DefaultConnection-ConnectionString is defined, will use InMemory-database instead
* Remove unused imports
* Create initial migrations

Issue #1
  • Loading branch information
Dentsor committed Mar 8, 2021
1 parent d340884 commit 7560c4b
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 32 deletions.
12 changes: 12 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "5.0.3",
"commands": [
"dotnet-ef"
]
}
}
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
**/.idea/*
**/.vscode/*

# Ignore secrets
# Ignore DB secrets
**/secrets

# Ignore local configuration files
*.local.json


# Created by https://www.toptal.com/developers/gitignore/api/csharp
# Edit at https://www.toptal.com/developers/gitignore?templates=csharp
Expand Down
4 changes: 2 additions & 2 deletions Controllers/BadgeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace SpeiderappAPI.Controllers
[ApiController]
public class BadgeController : ControllerBase
{
private readonly BadgeContext _context;
private readonly DBContext _context;

public BadgeController(BadgeContext context)
public BadgeController(DBContext context)
{
_context = context;
}
Expand Down
71 changes: 71 additions & 0 deletions Migrations/20210308210034_InitialCreate.Designer.cs

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

38 changes: 38 additions & 0 deletions Migrations/20210308210034_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

namespace SpeiderappAPI.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "BadgeList",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "text", nullable: false),
Image = table.Column<string>(type: "text", nullable: false),
Type = table.Column<string>(type: "text", nullable: false),
Description = table.Column<string>(type: "text", nullable: false),
Author = table.Column<string>(type: "text", nullable: false),
RootNode = table.Column<int>(type: "integer", nullable: false),
CreatedAt = table.Column<string>(type: "text", nullable: false),
IsComplete = table.Column<bool>(type: "boolean", nullable: false),
Secret = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_BadgeList", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BadgeList");
}
}
}
69 changes: 69 additions & 0 deletions Migrations/BadgeContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using SpeiderappAPI.Models;

namespace SpeiderappAPI.Migrations
{
[DbContext(typeof(DBContext))]
partial class BadgeContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 63)
.HasAnnotation("ProductVersion", "5.0.3")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);

modelBuilder.Entity("SpeiderappAPI.Models.Badge", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);

b.Property<string>("Author")
.IsRequired()
.HasColumnType("text");

b.Property<string>("CreatedAt")
.IsRequired()
.HasColumnType("text");

b.Property<string>("Description")
.IsRequired()
.HasColumnType("text");

b.Property<string>("Image")
.IsRequired()
.HasColumnType("text");

b.Property<bool>("IsComplete")
.HasColumnType("boolean");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");

b.Property<int>("RootNode")
.HasColumnType("integer");

b.Property<string>("Secret")
.IsRequired()
.HasColumnType("text");

b.Property<string>("Type")
.IsRequired()
.HasColumnType("text");

b.HasKey("Id");

b.ToTable("BadgeList");
});
#pragma warning restore 612, 618
}
}
}
12 changes: 0 additions & 12 deletions Models/BadgeContext.cs

This file was deleted.

14 changes: 14 additions & 0 deletions Models/DBContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace SpeiderappAPI.Models
{
public class DBContext : DbContext
{
public DbSet<Badge> BadgeList => Set<Badge>();
private IConfiguration Configuration { get; }

public DBContext(DbContextOptions options, IConfiguration configuration) : base(options)
=> Configuration = configuration;
}
}
7 changes: 1 addition & 6 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace SpeiderappAPI
{
Expand All @@ -17,6 +11,7 @@ public static void Main(string[] args)
}

public static IHostBuilder CreateHostBuilder(string[] args) =>

Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ dotnet watch run
| DELETE | /api/Badge/\<id\> | Delete a Badge |


## Local configuration

### Development
To override configuration settings which do not need to be secure locally,
create
```appsettings.Development.local.json```.
To override/set development secrets,
use:
```bash
dotnet user-secrets set <key> <value>
```

### Production
Create ```appsettings.Production.local.json```, and

# Guidelines & conventions

## Branches
Expand Down
4 changes: 3 additions & 1 deletion SpeiderappAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<UserSecretsId>6d78ab2e-1560-4d82-a173-5c28bf728190</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.2" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
</ItemGroup>
</Project>
27 changes: 21 additions & 6 deletions Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,33 @@ namespace SpeiderappAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
public IConfigurationRoot Configuration { get; }

public Startup(IWebHostEnvironment env)
{
Configuration = configuration;
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) //load base settings
.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true) //load local settings
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) //load environment settings
.AddJsonFile($"appsettings.{env.EnvironmentName}.local.json", optional: true) //load local environment settings
.AddEnvironmentVariables();

Configuration = builder.Build();
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BadgeContext>(opt =>
opt.UseInMemoryDatabase("BadgeList"));
services.AddDbContext<DBContext>(opt =>
{
var connectionString = Configuration.GetConnectionString("DefaultConnection");
if (connectionString is null)
opt.UseInMemoryDatabase(
Configuration.GetValue<string>("DefaultInMemoryDatabaseName"));
else
opt.UseNpgsql(connectionString);
});
services.AddControllers();
}

Expand Down
3 changes: 0 additions & 3 deletions appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"DefaultConnection": "server=bifrost.local;port=3306;database=speiderapp;user=root;password=oh1CInd2YDOKPyw1z05Y"
}
}
5 changes: 5 additions & 0 deletions appsettings.Development.local.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"DefaultConnection": "host=SERVER_NAME_HERE;port=PORT_NUMBER;database=DATABASE_NAME;username=USERNAME_HERE;password=PASSWORD_HERE"
}
}
3 changes: 2 additions & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"DefaultInMemoryDatabaseName": "speiderapp"
}

0 comments on commit 7560c4b

Please sign in to comment.