feat: working on models (#11)

This commit is contained in:
cswimr 2025-01-29 21:04:56 +00:00
parent 24849b3246
commit 75e18507aa
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087
6 changed files with 65 additions and 24 deletions

27
Data/Moderation.cs Normal file
View file

@ -0,0 +1,27 @@
namespace Myhm.Data;
public class Moderation
{
public int Id { get; set; }
public string UserId { get; set; } = default!;
public ModerationTypes Type { get; set; }
public string Reason { get; set; } = default!;
public ModerationReasons ReasonType { get; set; }
public DateTime CreatedAt { get; set; }
}
public enum ModerationTypes
{
Ban,
Warn,
}
// TODO: Get reasons from @MYHM and add them here
public enum ModerationReasons
{
Spamming,
Harassment,
HateSpeech,
NSFW,
Other,
}

View file

@ -1,8 +1,8 @@
namespace Myhm.Models
namespace Myhm.Data
{
public class MyhmConfiguration
{
public required string GlobalToken { get; init; }
public required string AuthToken { get; init; }
public required string DatabaseConnectionString { get; init; }
public required DatabaseTypes DatabaseType { get; init; }
public bool UseSentry { get; init; } = true;

View file

@ -1,15 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Myhm.Models;
using static EnumStringValues.EnumExtensions;
namespace Myhm.Data
{
public class MyhmContext(DbContextOptions<MyhmContext> options) : DbContext(options)
{
public DbSet<Moderation> Moderation { get; set; } = default!;
public DbSet<Token> Token { get; set; } = default!;
//public DbSet<Myhm.Models.Ban> Ban { get; set; } = default!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Moderation>()
.Property(m => m.Type)
.HasConversion(v => v.GetStringValue(), v => v.ParseToEnum<ModerationTypes>());
modelBuilder
.Entity<Moderation>()
.Property(m => m.ReasonType)
.HasConversion(v => v.GetStringValue(), v => v.ParseToEnum<ModerationReasons>());
}
}
}

9
Data/Token.cs Normal file
View file

@ -0,0 +1,9 @@
namespace Myhm.Data;
public class Token
{
public int Id { get; set; }
public string String { get; set; } = default!;
public int DiscordUserId { get; set; }
public DateTime CreatedAt { get; set; }
}

View file

@ -2,34 +2,30 @@ using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Myhm.Data;
using Myhm.Models;
using Myhm.Util.SwaggerTheme;
using Newtonsoft.Json;
var builder = WebApplication.CreateBuilder(args);
// Configuration
var config = builder.Configuration.GetSection("Configuration").Get<MyhmConfiguration>();
builder.Services.Configure<MyhmConfiguration>(builder.Configuration.GetSection("Configuration"));
if (config?.GlobalToken == "CHANGE-ME")
if ((config?.AuthToken?.Length ?? 0) < 64 && !builder.Environment.IsDevelopment())
{
throw new InvalidOperationException(
"Global token is not a valid value, please set the `CONFIGURATION__GLOBALTOKEN` environment variable to a random value. `openssl rand -hex 64` is a good way to generate a random value."
"Global token is not a valid value, please set the `CONFIGURATION__GLOBALTOKEN` environment variable to a random value of at least 64 characters. `openssl rand -hex 64` is a good way to generate a random value."
);
}
// Initialize Database
builder.Services.AddDbContextFactory<MyhmContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("MyhmContext"))
);
// Sentry
if (config?.UseSentry == true)
{
builder.WebHost.UseSentry();
}
// Add services to the container.
builder.Services.AddHttpLogging(o => { });
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(opts =>
@ -97,11 +93,9 @@ if (config?.LogRequests == true)
app.UseHttpLogging();
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
@ -109,12 +103,12 @@ app.UseHttpsRedirection();
//app.UseAntiforgery();
// if (app.Environment.IsDevelopment())
// {
// app.Logger.LogInformation(
// "Development mode detected, printing configuration: {Configuration}",
// config?.ToJson()
// );
// }
if (app.Environment.IsDevelopment())
{
app.Logger.LogInformation(
"Development mode detected, printing configuration: {Configuration}",
config != null ? JsonConvert.SerializeObject(config) : "null"
);
}
app.Run();

View file

@ -13,8 +13,10 @@
<ItemGroup>
<EmbeddedResource Include="Util\SwaggerTheme\modern.custom.css" />
<PackageReference Include="AspNetCore.SwaggerUI.Themes" Version="2.0.0" />
<PackageReference Include="EnumStringValues" Version="4.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
<PackageReference Include="Sentry.AspNetCore" Version="5.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />