Build qui marche , verifier que la supretion / insertion / deplacement marche entre :

- User/Commentary/Quote
- User/Favorite/Quote
pull/5/head
kekentin 1 month ago
parent d8f941f8ec
commit 8ccde0b125

@ -63,7 +63,7 @@ namespace Contextlib
public async Task DeleteCommentForUser(int userId)
{
// Retrieve comments for the specific userId
var comments = await _context.comments.Where(x => x.IdUsers == userId).ToListAsync();
var comments = await _context.comments.Where(x => x.IdUser == userId).ToListAsync();
if (!comments.Any()) // If no comments exist for this userId
{
throw new KeyNotFoundException($"No comments found for the user ID: {userId}.");

@ -34,17 +34,38 @@ namespace Contextlib
.UsingEntity<Favorite>(
l => l.HasOne(f => f.Quote)
.WithMany()
.OnDelete(DeleteBehavior.ClientCascade)
.HasForeignKey(f => f.IdQuote),
r => r.HasOne(f => f.Users)
.WithMany()
.OnDelete(DeleteBehavior.ClientCascade)
.HasForeignKey(f => f.IdUsers)
);
modelBuilder.Entity<Users>()
.HasMany(u => u.Quotes)
.WithOne(q => q.User)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasForeignKey(q => q.IdUsersPropose);
modelBuilder.Entity<Users>()
.HasMany<Quote>()
.WithMany()
.UsingEntity<Commentary>(
i => i.HasKey(e => new { e.IdUser, e.IdQuote })
);
modelBuilder.Entity<Commentary>()
.HasOne(c => c.User)
.WithMany()
.HasForeignKey(c => c.IdUser);
modelBuilder.Entity<Quote>()
.HasMany(q => q.Commentarys)
.WithOne(c => c.Quote)
.OnDelete(DeleteBehavior.ClientCascade)
.HasForeignKey(c => c.IdQuote);
modelBuilder.Entity<Quiz>()
.HasMany(q => q.Questions)
.WithMany(u => u.Quizs)

@ -16,7 +16,7 @@ namespace Entity
[Required]
[ForeignKey(nameof(Users))]
public int IdUsers { get; set; }
public int IdUser { get; set; }
[Required]
[ForeignKey(nameof(Quote))]
@ -32,6 +32,6 @@ namespace Entity
public Quote Quote { get; set; } = null!;
public Users Users { get; set; } = null!;
public Users User { get; set; } = null!;
}
}

@ -15,7 +15,7 @@ namespace Entity
public int Id { get; set; }
[Required]
[StringLength(50)]
[StringLength(100)]
public string Content { get; set; }
[Required]
@ -35,11 +35,12 @@ namespace Entity
[ForeignKey(nameof(Source))]
public int IdSource { get; set; }
[Required]
[ForeignKey(nameof(Users))]
public int IdUsersPropose { get; set; }
public Users User { get; set; } = null!;
public int? IdUsersPropose { get; set; }
//Réson de pour quoi j'ai mis le user en nullable et mis .OnDelete(DeleteBehavior.ClientSetNull) dans WTFContext
//https://learn.microsoft.com/fr-fr/ef/core/saving/cascade-delete
//Les suppressions en cascade sont nécessaires quand une entité dépendante/enfant ne peut plus être associée à son entité principale/parente actuelle. Cela peut se produire à la suite de la suppression de lentité principale/parente, ou quand lentité principale/parente existe toujours mais que lentité dépendante/enfant ne lui est plus associée.
public Users? User { get; set; } = null!;
public Source Source { get; set; } = null!;
@ -47,7 +48,7 @@ namespace Entity
public ICollection<DailyQuote> DailyQuotes { get; set; } = new List<DailyQuote>();
public ICollection<Commentary> commentaries { get; set; } = new List<Commentary>();
public ICollection<Commentary> Commentarys { get; set; } = new List<Commentary>();
public ICollection<Users> Favorite { get; } = new List<Users>();
}

@ -15,7 +15,7 @@ namespace Entity
public int Id { get; set; }
[Required]
[StringLength(50)]
[StringLength(100)]
public string Title { get; set; }
[Required]

@ -37,8 +37,6 @@ namespace Entity
public ICollection<Quote> Quotes { get; set; } = new List<Quote>();
public ICollection<Commentary> Commentary { get; set; } = new List<Commentary>();
public ICollection<Quote> Favorite { get; set; } = new List<Quote>();
}
}

@ -12,7 +12,7 @@ using StubbedContextLib;
namespace StubbedContextLib.Migrations
{
[DbContext(typeof(StubWTFContext))]
[Migration("20250314112216_migrationTest1")]
[Migration("20250317163102_migrationTest1")]
partial class migrationTest1
{
/// <inheritdoc />
@ -112,11 +112,11 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entity.Commentary", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
b.Property<int>("IdUser")
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("IdQuote")
.HasColumnType("int");
b.Property<string>("Comment")
.IsRequired()
@ -127,36 +127,34 @@ namespace StubbedContextLib.Migrations
.HasColumnType("date")
.HasColumnName("DateCommentary");
b.Property<int>("IdQuote")
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int>("IdUsers")
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.HasKey("Id");
b.HasKey("IdUser", "IdQuote");
b.HasIndex("IdQuote");
b.HasIndex("IdUsers");
b.ToTable("comments");
b.HasData(
new
{
Id = 1,
IdUser = 2,
IdQuote = 1,
Comment = "Ce film est le meilleur",
DateCommentary = new DateTime(2025, 2, 3, 0, 0, 0, 0, DateTimeKind.Unspecified),
IdQuote = 1,
IdUsers = 2
Id = 1
},
new
{
Id = 2,
IdUser = 3,
IdQuote = 1,
Comment = "Very good",
DateCommentary = new DateTime(2025, 3, 11, 0, 0, 0, 0, DateTimeKind.Unspecified),
IdQuote = 1,
IdUsers = 3
Id = 2
});
});
@ -575,8 +573,8 @@ namespace StubbedContextLib.Migrations
b.Property<string>("Content")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<int>("IdCharacter")
.HasColumnType("int");
@ -584,7 +582,7 @@ namespace StubbedContextLib.Migrations
b.Property<int>("IdSource")
.HasColumnType("int");
b.Property<int>("IdUsersPropose")
b.Property<int?>("IdUsersPropose")
.HasColumnType("int");
b.Property<bool>("IsValid")
@ -729,8 +727,8 @@ namespace StubbedContextLib.Migrations
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<int>("TypeSrc")
.HasColumnType("int");
@ -923,20 +921,20 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entity.Commentary", b =>
{
b.HasOne("Entity.Quote", "Quote")
.WithMany("commentaries")
.WithMany("Commentarys")
.HasForeignKey("IdQuote")
.OnDelete(DeleteBehavior.Cascade)
.OnDelete(DeleteBehavior.ClientCascade)
.IsRequired();
b.HasOne("Entity.Users", "Users")
.WithMany("Commentary")
.HasForeignKey("IdUsers")
b.HasOne("Entity.Users", "User")
.WithMany()
.HasForeignKey("IdUser")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Quote");
b.Navigation("Users");
b.Navigation("User");
});
modelBuilder.Entity("Entity.DailyQuote", b =>
@ -955,13 +953,13 @@ namespace StubbedContextLib.Migrations
b.HasOne("Entity.Quote", "Quote")
.WithMany()
.HasForeignKey("IdQuote")
.OnDelete(DeleteBehavior.Cascade)
.OnDelete(DeleteBehavior.ClientCascade)
.IsRequired();
b.HasOne("Entity.Users", "Users")
.WithMany()
.HasForeignKey("IdUsers")
.OnDelete(DeleteBehavior.Cascade)
.OnDelete(DeleteBehavior.ClientCascade)
.IsRequired();
b.Navigation("Quote");
@ -1011,9 +1009,7 @@ namespace StubbedContextLib.Migrations
b.HasOne("Entity.Users", "User")
.WithMany("Quotes")
.HasForeignKey("IdUsersPropose")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
.HasForeignKey("IdUsersPropose");
b.Navigation("Character");
@ -1049,9 +1045,9 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entity.Quote", b =>
{
b.Navigation("DailyQuotes");
b.Navigation("Commentarys");
b.Navigation("commentaries");
b.Navigation("DailyQuotes");
});
modelBuilder.Entity("Entity.Source", b =>
@ -1061,8 +1057,6 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entity.Users", b =>
{
b.Navigation("Commentary");
b.Navigation("Quotes");
});
#pragma warning restore 612, 618

@ -50,7 +50,7 @@ namespace StubbedContextLib.Migrations
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
Title = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
Year = table.Column<int>(type: "int", nullable: false),
TypeSrc = table.Column<int>(type: "int", nullable: false)
},
@ -153,13 +153,13 @@ namespace StubbedContextLib.Migrations
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Content = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
Content = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
Likes = table.Column<int>(type: "int", nullable: false),
Langage = table.Column<int>(type: "int", nullable: false),
IsValid = table.Column<bool>(type: "bit", nullable: false),
IdCharacter = table.Column<int>(type: "int", nullable: false),
IdSource = table.Column<int>(type: "int", nullable: false),
IdUsersPropose = table.Column<int>(type: "int", nullable: false)
IdUsersPropose = table.Column<int>(type: "int", nullable: true)
},
constraints: table =>
{
@ -180,33 +180,31 @@ namespace StubbedContextLib.Migrations
name: "FK_quotes_users_IdUsersPropose",
column: x => x.IdUsersPropose,
principalTable: "users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "comments",
columns: table => new
{
IdUser = table.Column<int>(type: "int", nullable: false),
IdQuote = table.Column<int>(type: "int", nullable: false),
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IdUsers = table.Column<int>(type: "int", nullable: false),
IdQuote = table.Column<int>(type: "int", nullable: false),
DateCommentary = table.Column<DateTime>(type: "date", nullable: false),
Comment = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_comments", x => x.Id);
table.PrimaryKey("PK_comments", x => new { x.IdUser, x.IdQuote });
table.ForeignKey(
name: "FK_comments_quotes_IdQuote",
column: x => x.IdQuote,
principalTable: "quotes",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
principalColumn: "Id");
table.ForeignKey(
name: "FK_comments_users_IdUsers",
column: x => x.IdUsers,
name: "FK_comments_users_IdUser",
column: x => x.IdUser,
principalTable: "users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
@ -243,14 +241,12 @@ namespace StubbedContextLib.Migrations
name: "FK_favorites_quotes_IdQuote",
column: x => x.IdQuote,
principalTable: "quotes",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
principalColumn: "Id");
table.ForeignKey(
name: "FK_favorites_users_IdUsers",
column: x => x.IdUsers,
principalTable: "users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
principalColumn: "Id");
});
migrationBuilder.InsertData(
@ -378,11 +374,11 @@ namespace StubbedContextLib.Migrations
migrationBuilder.InsertData(
table: "comments",
columns: new[] { "Id", "Comment", "DateCommentary", "IdQuote", "IdUsers" },
columns: new[] { "IdQuote", "IdUser", "Comment", "DateCommentary", "Id" },
values: new object[,]
{
{ 1, "Ce film est le meilleur", new DateTime(2025, 2, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), 1, 2 },
{ 2, "Very good", new DateTime(2025, 3, 11, 0, 0, 0, 0, DateTimeKind.Unspecified), 1, 3 }
{ 1, 2, "Ce film est le meilleur", new DateTime(2025, 2, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), 1 },
{ 1, 3, "Very good", new DateTime(2025, 3, 11, 0, 0, 0, 0, DateTimeKind.Unspecified), 2 }
});
migrationBuilder.InsertData(
@ -420,11 +416,6 @@ namespace StubbedContextLib.Migrations
table: "comments",
column: "IdQuote");
migrationBuilder.CreateIndex(
name: "IX_comments_IdUsers",
table: "comments",
column: "IdUsers");
migrationBuilder.CreateIndex(
name: "IX_favorites_IdUsers",
table: "favorites",

@ -109,11 +109,11 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entity.Commentary", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
b.Property<int>("IdUser")
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("IdQuote")
.HasColumnType("int");
b.Property<string>("Comment")
.IsRequired()
@ -124,36 +124,34 @@ namespace StubbedContextLib.Migrations
.HasColumnType("date")
.HasColumnName("DateCommentary");
b.Property<int>("IdQuote")
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int>("IdUsers")
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.HasKey("Id");
b.HasKey("IdUser", "IdQuote");
b.HasIndex("IdQuote");
b.HasIndex("IdUsers");
b.ToTable("comments");
b.HasData(
new
{
Id = 1,
IdUser = 2,
IdQuote = 1,
Comment = "Ce film est le meilleur",
DateCommentary = new DateTime(2025, 2, 3, 0, 0, 0, 0, DateTimeKind.Unspecified),
IdQuote = 1,
IdUsers = 2
Id = 1
},
new
{
Id = 2,
IdUser = 3,
IdQuote = 1,
Comment = "Very good",
DateCommentary = new DateTime(2025, 3, 11, 0, 0, 0, 0, DateTimeKind.Unspecified),
IdQuote = 1,
IdUsers = 3
Id = 2
});
});
@ -572,8 +570,8 @@ namespace StubbedContextLib.Migrations
b.Property<string>("Content")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<int>("IdCharacter")
.HasColumnType("int");
@ -581,7 +579,7 @@ namespace StubbedContextLib.Migrations
b.Property<int>("IdSource")
.HasColumnType("int");
b.Property<int>("IdUsersPropose")
b.Property<int?>("IdUsersPropose")
.HasColumnType("int");
b.Property<bool>("IsValid")
@ -726,8 +724,8 @@ namespace StubbedContextLib.Migrations
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<int>("TypeSrc")
.HasColumnType("int");
@ -920,20 +918,20 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entity.Commentary", b =>
{
b.HasOne("Entity.Quote", "Quote")
.WithMany("commentaries")
.WithMany("Commentarys")
.HasForeignKey("IdQuote")
.OnDelete(DeleteBehavior.Cascade)
.OnDelete(DeleteBehavior.ClientCascade)
.IsRequired();
b.HasOne("Entity.Users", "Users")
.WithMany("Commentary")
.HasForeignKey("IdUsers")
b.HasOne("Entity.Users", "User")
.WithMany()
.HasForeignKey("IdUser")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Quote");
b.Navigation("Users");
b.Navigation("User");
});
modelBuilder.Entity("Entity.DailyQuote", b =>
@ -952,13 +950,13 @@ namespace StubbedContextLib.Migrations
b.HasOne("Entity.Quote", "Quote")
.WithMany()
.HasForeignKey("IdQuote")
.OnDelete(DeleteBehavior.Cascade)
.OnDelete(DeleteBehavior.ClientCascade)
.IsRequired();
b.HasOne("Entity.Users", "Users")
.WithMany()
.HasForeignKey("IdUsers")
.OnDelete(DeleteBehavior.Cascade)
.OnDelete(DeleteBehavior.ClientCascade)
.IsRequired();
b.Navigation("Quote");
@ -1008,9 +1006,7 @@ namespace StubbedContextLib.Migrations
b.HasOne("Entity.Users", "User")
.WithMany("Quotes")
.HasForeignKey("IdUsersPropose")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
.HasForeignKey("IdUsersPropose");
b.Navigation("Character");
@ -1046,9 +1042,9 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entity.Quote", b =>
{
b.Navigation("DailyQuotes");
b.Navigation("Commentarys");
b.Navigation("commentaries");
b.Navigation("DailyQuotes");
});
modelBuilder.Entity("Entity.Source", b =>
@ -1058,8 +1054,6 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entity.Users", b =>
{
b.Navigation("Commentary");
b.Navigation("Quotes");
});
#pragma warning restore 612, 618

@ -72,8 +72,8 @@ namespace StubbedContextLib
);
modelBuilder.Entity<Commentary>().HasData(
new Commentary() { Id = 1, Comment = "Ce film est le meilleur", DateCommentary = new DateTime(2025,2,3), IdQuote = 1, IdUsers = 2 },
new Commentary() { Id = 2, Comment = "Very good", DateCommentary = new DateTime(2025, 3, 11), IdQuote = 1, IdUsers = 3 }
new Commentary() { Id = 1, Comment = "Ce film est le meilleur", DateCommentary = new DateTime(2025,2,3), IdQuote = 1, IdUser = 2 },
new Commentary() { Id = 2, Comment = "Very good", DateCommentary = new DateTime(2025, 3, 11), IdQuote = 1, IdUser = 3 }
);
modelBuilder.Entity<DailyQuote>().HasData(

@ -18,8 +18,4 @@
<ProjectReference Include="..\Entity\Entity.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project>

Loading…
Cancel
Save