db init
This commit is contained in:
parent
3529b0a41b
commit
d97b62fc9d
61
Database/AppDbContext.cs
Normal file
61
Database/AppDbContext.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Emberend.Database;
|
||||
|
||||
public sealed class AppDbContext : DbContext
|
||||
{
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
|
||||
public DbSet<User> Users => Set<User>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder model)
|
||||
{
|
||||
model.Entity<User>()
|
||||
.Property(u => u.Interests)
|
||||
.HasConversion(
|
||||
v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
|
||||
v => JsonSerializer.Deserialize<string[]>(v, (JsonSerializerOptions?)null) ?? Array.Empty<string>()
|
||||
);
|
||||
|
||||
model.Entity<User>().HasKey(u => u.Id);
|
||||
model.Entity<User>().HasIndex(u => u.Email).IsUnique();
|
||||
}
|
||||
}
|
||||
|
||||
public enum Gender : byte
|
||||
{
|
||||
Unspecified = 0,
|
||||
Male = 1,
|
||||
Female = 2,
|
||||
Other = 3
|
||||
}
|
||||
|
||||
public sealed class User
|
||||
{
|
||||
public string Id { get; set; } = null!;
|
||||
|
||||
public string Email { get; set; } = null!;
|
||||
|
||||
public string Name { get; set; } = null!;
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string ProfilePicture { get; set; } = string.Empty;
|
||||
|
||||
public DateOnly DateOfBirth { get; set; }
|
||||
public Gender Gender { get; set; } = Gender.Unspecified;
|
||||
|
||||
public string[] Interests { get; set; } = Array.Empty<string>();
|
||||
|
||||
|
||||
public string PasswordHash { get; set; } = null!;
|
||||
public string Salt { get; set; } = null!;
|
||||
|
||||
|
||||
[NotMapped]
|
||||
public int Age =>
|
||||
DateTime.Today.Year - DateOfBirth.Year
|
||||
- (DateOfBirth.ToDateTime(TimeOnly.MinValue) > DateTime.Today ? 1 : 0);
|
||||
}
|
||||
@ -8,8 +8,14 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="EntityFramework" Version="6.5.1" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.23" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Helpers\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
44
Program.cs
44
Program.cs
@ -1,11 +1,11 @@
|
||||
using Emberend.Database;
|
||||
using Emberend.Endpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
|
||||
builder.WebHost.ConfigureKestrel(options =>
|
||||
{
|
||||
@ -18,20 +18,42 @@ builder.WebHost.ConfigureKestrel(options =>
|
||||
//});
|
||||
});
|
||||
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Logging.ClearProviders();
|
||||
|
||||
builder.Services.AddDbContextPool<AppDbContext>(opt =>
|
||||
{
|
||||
opt.UseMySql(
|
||||
builder.Configuration.GetConnectionString("Default"),
|
||||
ServerVersion.AutoDetect(
|
||||
builder.Configuration.GetConnectionString("Default")
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
builder.Services.AddAuthentication("auth")
|
||||
.AddCookie("auth", o =>
|
||||
{
|
||||
o.Cookie.Name = "auth";
|
||||
o.Cookie.HttpOnly = true;
|
||||
o.Cookie.SameSite = SameSiteMode.Strict;
|
||||
o.Events.OnRedirectToLogin = ctx =>
|
||||
{
|
||||
ctx.Response.StatusCode = 401;
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
app.MapUserAccessEndpoints();
|
||||
|
||||
//Ne treba jer koristimo nginx kao relay/proxy...
|
||||
//app.UseHttpsRedirection();
|
||||
|
||||
|
||||
@ -50,3 +72,5 @@ app.Run();
|
||||
|
||||
|
||||
|
||||
public sealed record RegisterDto(string Email, string Password);
|
||||
public sealed record LoginDto(string Email, string Password);
|
||||
@ -1,4 +1,7 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"Default": "Server=192.168.10.201;Database=test;User=test;Password=PJz395WDm_RXPvd];Pooling=true;MinimumPoolSize=10;MaximumPoolSize=200;AllowLoadLocalInfile=true;"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user