From 4322330745d18bb28f7deee1a54ae3c748645d92 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sun, 25 Apr 2021 00:10:17 +0200 Subject: Initial commit --- src/Data/Database/AppDbContext.cs | 25 +++++++++++++++++++++++++ src/Data/Database/Base.cs | 10 ++++++++++ src/Data/Database/User.cs | 20 ++++++++++++++++++++ src/Data/Dtos/LoginRequestDto.cs | 9 +++++++++ src/Data/General/AppPath.cs | 27 +++++++++++++++++++++++++++ src/Data/General/LoggedInUser.cs | 10 ++++++++++ src/Data/Result/ErrorResult.cs | 13 +++++++++++++ src/Data/Static/AppJsonSettings.cs | 17 +++++++++++++++++ src/Data/Static/AppPaths.cs | 16 ++++++++++++++++ 9 files changed, 147 insertions(+) create mode 100644 src/Data/Database/AppDbContext.cs create mode 100644 src/Data/Database/Base.cs create mode 100644 src/Data/Database/User.cs create mode 100644 src/Data/Dtos/LoginRequestDto.cs create mode 100644 src/Data/General/AppPath.cs create mode 100644 src/Data/General/LoggedInUser.cs create mode 100644 src/Data/Result/ErrorResult.cs create mode 100644 src/Data/Static/AppJsonSettings.cs create mode 100644 src/Data/Static/AppPaths.cs (limited to 'src/Data') diff --git a/src/Data/Database/AppDbContext.cs b/src/Data/Database/AppDbContext.cs new file mode 100644 index 0000000..9bfabc4 --- /dev/null +++ b/src/Data/Database/AppDbContext.cs @@ -0,0 +1,25 @@ +using Microsoft.EntityFrameworkCore; + +namespace IOL.WebApi.Template.Data.Database +{ + public class AppDbContext : DbContext + { + public AppDbContext(DbContextOptions options) : base(options) { } + public DbSet Users { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) { +#if false + var seedUser = new User("admin@example.org"); + seedUser.Id = Guid.NewGuid(); + seedUser.HashAndSetPassword("asdf1234"); + modelBuilder.Entity().HasData(seedUser); +#endif + + modelBuilder.Entity(e => { + e.ToTable("users"); + }); + + base.OnModelCreating(modelBuilder); + } + } +} diff --git a/src/Data/Database/Base.cs b/src/Data/Database/Base.cs new file mode 100644 index 0000000..4b1ea55 --- /dev/null +++ b/src/Data/Database/Base.cs @@ -0,0 +1,10 @@ +using System; + +namespace IOL.WebApi.Template.Data.Database +{ + public class Base + { + public Guid Id { get; set; } + public DateTime Created { get; set; } + } +} diff --git a/src/Data/Database/User.cs b/src/Data/Database/User.cs new file mode 100644 index 0000000..31513a3 --- /dev/null +++ b/src/Data/Database/User.cs @@ -0,0 +1,20 @@ +using IOL.Helpers; + +namespace IOL.WebApi.Template.Data.Database +{ + public class User : Base + { + public User(string username) => Username = username; + + public string Username { get; set; } + public string Password { get; set; } + + public void HashAndSetPassword(string password) { + Password = PasswordHelper.HashPassword(password); + } + + public bool VerifyPassword(string password) { + return PasswordHelper.Verify(password, Password); + } + } +} diff --git a/src/Data/Dtos/LoginRequestDto.cs b/src/Data/Dtos/LoginRequestDto.cs new file mode 100644 index 0000000..bed49c4 --- /dev/null +++ b/src/Data/Dtos/LoginRequestDto.cs @@ -0,0 +1,9 @@ +namespace IOL.WebApi.Template.Data.Dtos +{ + public class LoginRequestDto + { + public string Username { get; set; } + public string Password { get; set; } + public bool Persist { get; set; } + } +} diff --git a/src/Data/General/AppPath.cs b/src/Data/General/AppPath.cs new file mode 100644 index 0000000..241cf65 --- /dev/null +++ b/src/Data/General/AppPath.cs @@ -0,0 +1,27 @@ +using System.IO; +using IOL.Helpers; + +namespace IOL.WebApi.Template.Data.General +{ + public sealed record AppPath + { + public string HostPath { get; init; } + public string WebPath { get; init; } + + public string GetHostPathForFilename(string filename, string fallback = "") { + if (filename.IsNullOrWhiteSpace()) { + return fallback; + } + + return Path.Combine(HostPath, filename); + } + + public string GetWebPathForFilename(string filename, string fallback = "") { + if (filename.IsNullOrWhiteSpace()) { + return fallback; + } + + return Path.Combine(WebPath, filename); + } + } +} diff --git a/src/Data/General/LoggedInUser.cs b/src/Data/General/LoggedInUser.cs new file mode 100644 index 0000000..d278d3f --- /dev/null +++ b/src/Data/General/LoggedInUser.cs @@ -0,0 +1,10 @@ +using System; + +namespace IOL.WebApi.Template.Data.General +{ + public class LoggedInUser + { + public Guid Id { get; set; } + public string Username { get; set; } + } +} diff --git a/src/Data/Result/ErrorResult.cs b/src/Data/Result/ErrorResult.cs new file mode 100644 index 0000000..3e585b3 --- /dev/null +++ b/src/Data/Result/ErrorResult.cs @@ -0,0 +1,13 @@ +namespace IOL.WebApi.Template.Data.Result +{ + public class ErrorResult + { + public ErrorResult(string title = default, string text = default) { + Title = title; + Text = text; + } + + public string Title { get; set; } + public string Text { get; set; } + } +} diff --git a/src/Data/Static/AppJsonSettings.cs b/src/Data/Static/AppJsonSettings.cs new file mode 100644 index 0000000..8aad1ba --- /dev/null +++ b/src/Data/Static/AppJsonSettings.cs @@ -0,0 +1,17 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; +using Microsoft.AspNetCore.Mvc; + +namespace IOL.WebApi.Template.Data.Static +{ + public static class AppJsonSettings + { + public static Action Value { get; } = options => { + options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve; + options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; + options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString; + options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + }; + } +} diff --git a/src/Data/Static/AppPaths.cs b/src/Data/Static/AppPaths.cs new file mode 100644 index 0000000..64a249a --- /dev/null +++ b/src/Data/Static/AppPaths.cs @@ -0,0 +1,16 @@ +using System.IO; +using IOL.WebApi.Template.Data.General; + +namespace IOL.WebApi.Template.Data.Static +{ + public static class AppPaths + { + public static AppPath AppData => new() { + HostPath = Path.Combine(Directory.GetCurrentDirectory(), "AppData") + }; + + public static AppPath DataProtectionKeys => new() { + HostPath = Path.Combine(Directory.GetCurrentDirectory(), "AppData", "data-protection-keys") + }; + } +} -- cgit v1.3