Skip to content

Commit 71bab46

Browse files
authored
feat(db): add postgres database
1 parent 0ca79b7 commit 71bab46

19 files changed

+306
-36
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Micro.Starter.Api.Configs
2+
{
3+
public class DatabaseConfig
4+
{
5+
public string Host { set; get; }
6+
public int Port { set; get; }
7+
public string Name { set; get; }
8+
public string User { set; get; }
9+
public string Password { set; get; }
10+
}
11+
}

Micro.Starter.Api/WeatherForecast.cs renamed to Micro.Starter.Api/Controllers/WeatherForecast.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Micro.Starter.Api
3+
namespace Micro.Starter.Api.Controllers
44
{
55
public class WeatherForecast
66
{
@@ -12,4 +12,4 @@ public class WeatherForecast
1212

1313
public string Summary { get; set; }
1414
}
15-
}
15+
}

Micro.Starter.Api/Dockerfile

Lines changed: 0 additions & 14 deletions
This file was deleted.

Micro.Starter.Api/HealthCheck/HealthCheckController.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Threading.Tasks;
4+
using Micro.Starter.Api.Models;
5+
using Micro.Starter.Api.Repository;
36
using Microsoft.AspNetCore.Http;
47
using Microsoft.AspNetCore.Mvc;
58

@@ -9,6 +12,13 @@ namespace Micro.Starter.Api.HealthCheck
912
[Route("api/health")]
1013
public class HealthCheckController : ControllerBase
1114
{
15+
private readonly IWeatherRepository _weather;
16+
17+
public HealthCheckController(IWeatherRepository weather)
18+
{
19+
_weather = weather;
20+
}
21+
1222
[HttpGet]
1323
[ProducesResponseType(typeof(IEnumerable<HealthData>), StatusCodes.Status200OK)]
1424
public async Task<HealthData> Get()
@@ -20,9 +30,18 @@ public async Task<HealthData> Get()
2030
};
2131
}
2232

23-
private static Task<bool> GetFakeDbHealth()
33+
private async Task<bool> GetFakeDbHealth()
2434
{
25-
return Task.Run(() => true);
35+
try
36+
{
37+
await _weather.Create(new Weather());
38+
return true;
39+
}
40+
catch (Exception)
41+
{
42+
// todo: log e
43+
return false;
44+
}
2645
}
2746

2847
private static Task<bool> GetFakeCacheHealth()

Micro.Starter.Api/Micro.Starter.Api.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
<ItemGroup>
88
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.0.0-preview8.19405.7" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview8.19405.11" />
10+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.0-preview8" />
11+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
912
</ItemGroup>
1013

1114
</Project>

Micro.Starter.Api/Migrations/20190828165715_InitialCreate.Designer.cs

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
namespace Micro.Starter.Api.Migrations
4+
{
5+
public partial class InitialCreate : Migration
6+
{
7+
protected override void Up(MigrationBuilder migrationBuilder)
8+
{
9+
migrationBuilder.CreateTable(
10+
name: "Weathers",
11+
columns: table => new
12+
{
13+
Id = table.Column<string>(nullable: false),
14+
Temperature = table.Column<double>(nullable: false)
15+
},
16+
constraints: table =>
17+
{
18+
table.PrimaryKey("PK_Weathers", x => x.Id);
19+
});
20+
}
21+
22+
protected override void Down(MigrationBuilder migrationBuilder)
23+
{
24+
migrationBuilder.DropTable(
25+
name: "Weathers");
26+
}
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// <auto-generated />
2+
using Micro.Starter.Api.Models;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
6+
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
7+
8+
namespace Micro.Starter.Api.Migrations
9+
{
10+
[DbContext(typeof(ApplicationContext))]
11+
partial class ApplicationContextModelSnapshot : ModelSnapshot
12+
{
13+
protected override void BuildModel(ModelBuilder modelBuilder)
14+
{
15+
#pragma warning disable 612, 618
16+
modelBuilder
17+
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn)
18+
.HasAnnotation("ProductVersion", "3.0.0-preview8.19405.11")
19+
.HasAnnotation("Relational:MaxIdentifierLength", 63);
20+
21+
modelBuilder.Entity("Micro.Starter.Api.Models.Weather", b =>
22+
{
23+
b.Property<string>("Id")
24+
.HasColumnType("text");
25+
26+
b.Property<double>("Temperature")
27+
.HasColumnType("double precision");
28+
29+
b.HasKey("Id");
30+
31+
b.ToTable("Weathers");
32+
});
33+
#pragma warning restore 612, 618
34+
}
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Micro.Starter.Api.Configs;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.Options;
4+
using Npgsql;
5+
6+
namespace Micro.Starter.Api.Models
7+
{
8+
public class ApplicationContext : DbContext
9+
{
10+
private readonly DatabaseConfig _db;
11+
public DbSet<Weather> Weathers { set; get; }
12+
13+
public ApplicationContext(DbContextOptions options, IOptions<DatabaseConfig> dbOption) : base(options)
14+
{
15+
_db = dbOption.Value;
16+
}
17+
18+
protected ApplicationContext(IOptions<DatabaseConfig> dbOption)
19+
{
20+
_db = dbOption.Value;
21+
}
22+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
23+
{
24+
var connection = new NpgsqlConnectionStringBuilder
25+
{
26+
Host = _db.Host,
27+
Port = _db.Port,
28+
Database = _db.Name,
29+
Username = _db.User,
30+
Password = _db.Password,
31+
SslMode = SslMode.Disable
32+
};
33+
optionsBuilder.UseNpgsql(connection.ConnectionString);
34+
}
35+
36+
protected override void OnModelCreating(ModelBuilder builder)
37+
{
38+
base.OnModelCreating(builder);
39+
}
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Micro.Starter.Api.Models
2+
{
3+
public class Weather
4+
{
5+
public string Id { set; get; }
6+
public double Temperature { set; get; }
7+
}
8+
}

0 commit comments

Comments
 (0)