-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PostgreSQL database connection (with secrets support 🤞)
- Loading branch information
Showing
15 changed files
with
225 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
namespace HappyBirthdayWorld.Api.Migrations | ||
{ | ||
public partial class Initial : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "BirthRecords", | ||
columns: table => new | ||
{ | ||
Name = table.Column<string>(nullable: false), | ||
DateOfBirth = table.Column<DateTime>(nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_BirthRecords", x => x.Name); | ||
}); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "BirthRecords"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// <auto-generated /> | ||
using System; | ||
using HappyBirthdayWorld.Api.Repositories; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; | ||
|
||
namespace HappyBirthdayWorld.Api.Migrations | ||
{ | ||
[DbContext(typeof(BirthdayContext))] | ||
partial class BirthdayContextModelSnapshot : ModelSnapshot | ||
{ | ||
protected override void BuildModel(ModelBuilder modelBuilder) | ||
{ | ||
#pragma warning disable 612, 618 | ||
modelBuilder | ||
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn) | ||
.HasAnnotation("ProductVersion", "2.1.4-rtm-31024") | ||
.HasAnnotation("Relational:MaxIdentifierLength", 63); | ||
|
||
modelBuilder.Entity("HappyBirthdayWorld.Api.Models.BirthRecord", b => | ||
{ | ||
b.Property<string>("Name") | ||
.ValueGeneratedOnAdd(); | ||
|
||
b.Property<DateTime>("DateOfBirth"); | ||
|
||
b.HasKey("Name"); | ||
|
||
b.ToTable("BirthRecords"); | ||
}); | ||
#pragma warning restore 612, 618 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace HappyBirthdayWorld.Api.Models | ||
{ | ||
public class BirthRecord | ||
{ | ||
public BirthRecord(string name, DateTime dateOfBirth) | ||
{ | ||
Name = name; | ||
DateOfBirth = dateOfBirth; | ||
} | ||
|
||
[Key] | ||
public string Name { get; set; } | ||
|
||
public DateTime DateOfBirth { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Linq; | ||
using HappyBirthdayWorld.Api.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace HappyBirthdayWorld.Api.Repositories | ||
{ | ||
public class DatabaseBirthdayRepository : IBirthdayRepository | ||
{ | ||
private readonly ILogger<DatabaseBirthdayRepository> logger; | ||
|
||
private readonly BirthdayContext birthdayContext; | ||
|
||
public DatabaseBirthdayRepository(ILogger<DatabaseBirthdayRepository> logger, BirthdayContext birthdayContext) | ||
{ | ||
this.logger = logger; | ||
this.birthdayContext = birthdayContext; | ||
} | ||
|
||
public bool TryGetDateOfBirth(string name, out DateTime dateOfBirth) | ||
{ | ||
try | ||
{ | ||
dateOfBirth = birthdayContext.BirthRecords.First(br => br.Name == name).DateOfBirth; | ||
return true; | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.LogInformation(e, "Failed to retrieve date of birth"); | ||
} | ||
dateOfBirth = new DateTime(); | ||
return false; | ||
} | ||
|
||
public void PutDateOfBirth(BirthRecord birthRecord) | ||
{ | ||
var existingRecord = birthdayContext.Find<BirthRecord>(birthRecord.Name); | ||
|
||
if (existingRecord == null) | ||
{ | ||
birthdayContext.Add(birthRecord); | ||
} | ||
else | ||
{ | ||
if (existingRecord.DateOfBirth == birthRecord.DateOfBirth) return; | ||
existingRecord.DateOfBirth = birthRecord.DateOfBirth; | ||
} | ||
|
||
birthdayContext.SaveChanges(); | ||
} | ||
} | ||
|
||
public class BirthdayContext : DbContext | ||
{ | ||
public BirthdayContext(DbContextOptions options) : base(options) | ||
{ | ||
} | ||
|
||
public DbSet<BirthRecord> BirthRecords { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
using System; | ||
using HappyBirthdayWorld.Api.Models; | ||
|
||
namespace HappyBirthdayWorld.Api.Repositories | ||
{ | ||
|
||
public interface IBirthdayRepository | ||
{ | ||
bool TryGetDateOfBirth(string name, out DateTime dateOfBirth); | ||
|
||
void PutDateOfBirth(string name, DateTime dateOfBirth); | ||
void PutDateOfBirth(BirthRecord birthRecord); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters