From d97b62fc9d63672a332b61a7d0f41231d7ef1454 Mon Sep 17 00:00:00 2001 From: Goran Date: Tue, 20 Jan 2026 18:55:57 +0100 Subject: [PATCH] db init --- Database/AppDbContext.cs | 61 ++++++++++++++++++++++++++++++++++++++++ Emberend.csproj | 6 ++++ Program.cs | 44 ++++++++++++++++++++++------- appsettings.json | 3 ++ 4 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 Database/AppDbContext.cs diff --git a/Database/AppDbContext.cs b/Database/AppDbContext.cs new file mode 100644 index 0000000..c9d2f33 --- /dev/null +++ b/Database/AppDbContext.cs @@ -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 options) : base(options) { } + public DbSet Users => Set(); + + protected override void OnModelCreating(ModelBuilder model) + { + model.Entity() + .Property(u => u.Interests) + .HasConversion( + v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null), + v => JsonSerializer.Deserialize(v, (JsonSerializerOptions?)null) ?? Array.Empty() + ); + + model.Entity().HasKey(u => u.Id); + model.Entity().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(); + + + 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); +} diff --git a/Emberend.csproj b/Emberend.csproj index bcd877d..6b7b9e6 100644 --- a/Emberend.csproj +++ b/Emberend.csproj @@ -8,8 +8,14 @@ + + + + + + diff --git a/Program.cs b/Program.cs index 3ab515d..b1efa7b 100644 --- a/Program.cs +++ b/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(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); \ No newline at end of file diff --git a/appsettings.json b/appsettings.json index 10f68b8..5e1b9aa 100644 --- a/appsettings.json +++ b/appsettings.json @@ -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",