Creating all entities and dbcontext

This commit is contained in:
2025-10-17 08:41:30 +02:00
commit d9accc9234
15 changed files with 242 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

13
.idea/.idea.BlogPlatform/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/.idea.BlogPlatform.iml
/contentModel.xml
/projectSettingsUpdater.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

6
.idea/.idea.BlogPlatform/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

16
BlogPlatform.sln Normal file
View File

@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlogPlatform", "BlogPlatform\BlogPlatform.csproj", "{B132EF03-5CDA-4FD0-AB1B-3F12F064478C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B132EF03-5CDA-4FD0-AB1B-3F12F064478C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B132EF03-5CDA-4FD0-AB1B-3F12F064478C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B132EF03-5CDA-4FD0-AB1B-3F12F064478C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B132EF03-5CDA-4FD0-AB1B-3F12F064478C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FastEndpoints" Version="7.0.1" />
<PackageReference Include="FastEndpoints.Security" Version="7.0.1" />
<PackageReference Include="FastEndpoints.Swagger" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.19"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.20">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.20" />
<PackageReference Include="PasswordGenerator" Version="2.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,6 @@
@BlogPlatform_HostAddress = http://localhost:5040
GET {{BlogPlatform_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@@ -0,0 +1,30 @@
using BlogPlatform.Models;
using Microsoft.EntityFrameworkCore;
namespace BlogPlatform;
public class BlogPlatformDbContext : DbContext
{
// Tables représentées par les entités
public DbSet<Comment> Comments { get; set; } // Table des Commentaires
public DbSet<Post> Posts { get; set; } // Table des publications
public DbSet<User> Users { get; set; } // Table des utilisateurs
// Configuration de la connexion à la base de données
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString =
"Server=romaric-thibault.fr;" + // Serveur SQL
"Database=mathys_BlogPlatform;" + // Nom de la base
"User Id=mathys;" + // Utilisateur
"Password=Onto9-Cage-Afflicted;" + // Mot de passe
"TrustServerCertificate=true;"; // Accepte certificat auto-signé
optionsBuilder.UseSqlServer(connectionString);
}
// Personnalisation du modèle (non utilisée ici)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}

View File

@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
namespace BlogPlatform.Models;
public class Comment
{
[Key] public int Id { get; set; }
[Required, Length(1, 500)] public string? Content { get; set; }
[Required] public DateOnly CreatedAt { get; set; }
public Post? Post { get; set; }
[Required] public int PostId { get; set; }
public User? User { get; set; }
[Required] public int UserId { get; set; }
}

View File

@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace BlogPlatform.Models;
public class Post
{
[Key] public int Id { get; set; }
[Required, Length(1, 200)] public string? Title { get; set; }
[Required] public string? Content { get; set; }
[Required] public int Likes { get; set; }
[Required] public DateOnly CreatedAt { get; set; }
public User? User { get; set; }
[Required] public int UserId { get; set; }
public List<Comment>? Comments { get; set; }
}

View File

@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
namespace BlogPlatform.Models;
public class User
{
[Key] public int Id { get; set; }
[Required, Length(3, 20)] public string? Username { get; set; }
[Required, Length(3, 50)] public string? Email { get; set; }
[Required, Length(60, 60)] public string? Password { get; set; }
[Required, Length(24, 24)] public string? Salt { get; set; }
[Required] public DateOnly CreatedAt { get; set; }
public List<Comment>? Comments { get; set; }
public List<Post>? Posts { get; set; }
}

27
BlogPlatform/Program.cs Normal file
View File

@@ -0,0 +1,27 @@
using BlogPlatform;
using FastEndpoints;
using FastEndpoints.Swagger;
using FastEndpoints.Security;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// On ajoute ici FastEndpoints, un framework REPR et Swagger aux services disponibles dans le projet
builder.Services
.AddAuthenticationJwtBearer(s => s.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong")
.AddAuthorization()
.AddFastEndpoints()
.SwaggerDocument();
// On ajoute ici la configuration de la base de données
builder.Services.AddDbContext<BlogPlatformDbContext>();
// On construit l'application en lui donnant vie
WebApplication app = builder.Build();
app.UseAuthentication()
.UseAuthorization()
.UseFastEndpoints()
.UseSwaggerGen();
app.UseHttpsRedirection();
app.Run();

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60181",
"sslPort": 44311
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5040",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7275;http://localhost:5040",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}