From d9accc92347d37a0b86efe391e0c33aeb47ec09c Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Fri, 17 Oct 2025 08:41:30 +0200 Subject: [PATCH] Creating all entities and dbcontext --- .gitignore | 5 +++ .idea/.idea.BlogPlatform/.idea/.gitignore | 13 ++++++ .../.idea.BlogPlatform/.idea/indexLayout.xml | 8 ++++ .idea/.idea.BlogPlatform/.idea/vcs.xml | 6 +++ BlogPlatform.sln | 16 ++++++++ BlogPlatform/BlogPlatform.csproj | 24 +++++++++++ BlogPlatform/BlogPlatform.http | 6 +++ BlogPlatform/BlogPlatformDbContext.cs | 30 ++++++++++++++ BlogPlatform/Models/Comment.cs | 16 ++++++++ BlogPlatform/Models/Post.cs | 17 ++++++++ BlogPlatform/Models/User.cs | 16 ++++++++ BlogPlatform/Program.cs | 27 ++++++++++++ BlogPlatform/Properties/launchSettings.json | 41 +++++++++++++++++++ BlogPlatform/appsettings.Development.json | 8 ++++ BlogPlatform/appsettings.json | 9 ++++ 15 files changed, 242 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.idea.BlogPlatform/.idea/.gitignore create mode 100644 .idea/.idea.BlogPlatform/.idea/indexLayout.xml create mode 100644 .idea/.idea.BlogPlatform/.idea/vcs.xml create mode 100644 BlogPlatform.sln create mode 100644 BlogPlatform/BlogPlatform.csproj create mode 100644 BlogPlatform/BlogPlatform.http create mode 100644 BlogPlatform/BlogPlatformDbContext.cs create mode 100644 BlogPlatform/Models/Comment.cs create mode 100644 BlogPlatform/Models/Post.cs create mode 100644 BlogPlatform/Models/User.cs create mode 100644 BlogPlatform/Program.cs create mode 100644 BlogPlatform/Properties/launchSettings.json create mode 100644 BlogPlatform/appsettings.Development.json create mode 100644 BlogPlatform/appsettings.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/.idea/.idea.BlogPlatform/.idea/.gitignore b/.idea/.idea.BlogPlatform/.idea/.gitignore new file mode 100644 index 0000000..b78cf2c --- /dev/null +++ b/.idea/.idea.BlogPlatform/.idea/.gitignore @@ -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 diff --git a/.idea/.idea.BlogPlatform/.idea/indexLayout.xml b/.idea/.idea.BlogPlatform/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.BlogPlatform/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.BlogPlatform/.idea/vcs.xml b/.idea/.idea.BlogPlatform/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.BlogPlatform/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BlogPlatform.sln b/BlogPlatform.sln new file mode 100644 index 0000000..09be521 --- /dev/null +++ b/BlogPlatform.sln @@ -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 diff --git a/BlogPlatform/BlogPlatform.csproj b/BlogPlatform/BlogPlatform.csproj new file mode 100644 index 0000000..cd04abb --- /dev/null +++ b/BlogPlatform/BlogPlatform.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + diff --git a/BlogPlatform/BlogPlatform.http b/BlogPlatform/BlogPlatform.http new file mode 100644 index 0000000..b4c2f6c --- /dev/null +++ b/BlogPlatform/BlogPlatform.http @@ -0,0 +1,6 @@ +@BlogPlatform_HostAddress = http://localhost:5040 + +GET {{BlogPlatform_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/BlogPlatform/BlogPlatformDbContext.cs b/BlogPlatform/BlogPlatformDbContext.cs new file mode 100644 index 0000000..304a74f --- /dev/null +++ b/BlogPlatform/BlogPlatformDbContext.cs @@ -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 Comments { get; set; } // Table des Commentaires + public DbSet Posts { get; set; } // Table des publications + public DbSet 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) + { + } +} \ No newline at end of file diff --git a/BlogPlatform/Models/Comment.cs b/BlogPlatform/Models/Comment.cs new file mode 100644 index 0000000..b417228 --- /dev/null +++ b/BlogPlatform/Models/Comment.cs @@ -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; } +} \ No newline at end of file diff --git a/BlogPlatform/Models/Post.cs b/BlogPlatform/Models/Post.cs new file mode 100644 index 0000000..c3d8985 --- /dev/null +++ b/BlogPlatform/Models/Post.cs @@ -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? Comments { get; set; } +} \ No newline at end of file diff --git a/BlogPlatform/Models/User.cs b/BlogPlatform/Models/User.cs new file mode 100644 index 0000000..d096003 --- /dev/null +++ b/BlogPlatform/Models/User.cs @@ -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? Comments { get; set; } + public List? Posts { get; set; } +} \ No newline at end of file diff --git a/BlogPlatform/Program.cs b/BlogPlatform/Program.cs new file mode 100644 index 0000000..97e7f60 --- /dev/null +++ b/BlogPlatform/Program.cs @@ -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(); + +// On construit l'application en lui donnant vie +WebApplication app = builder.Build(); +app.UseAuthentication() + .UseAuthorization() + .UseFastEndpoints() + .UseSwaggerGen(); + +app.UseHttpsRedirection(); + +app.Run(); \ No newline at end of file diff --git a/BlogPlatform/Properties/launchSettings.json b/BlogPlatform/Properties/launchSettings.json new file mode 100644 index 0000000..d90c262 --- /dev/null +++ b/BlogPlatform/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/BlogPlatform/appsettings.Development.json b/BlogPlatform/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/BlogPlatform/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/BlogPlatform/appsettings.json b/BlogPlatform/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/BlogPlatform/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}