test many to many non fonctionel

pull/5/head
Kevin MONDEJAR 1 month ago
parent 7ff1bacb96
commit 7694c94feb

@ -22,6 +22,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Entity\Entity.csproj" /> <ProjectReference Include="..\Entity\Entity.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -13,10 +13,6 @@ namespace Contextlib
{ {
private WTFContext _context; private WTFContext _context;
/// <summary>
/// Initializes a new instance of the <see cref="DbCharacterManager"/> class.
/// </summary>
/// <param name="context">The <see cref="WTFContext"/> instance used to interact with the database.</param>
public DbCharacterManager(WTFContext context) public DbCharacterManager(WTFContext context)
{ {
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
@ -47,12 +43,7 @@ namespace Contextlib
{ {
List<Character> charLst = _context.characters.ToList(); List<Character> charLst = _context.characters.ToList();
return Task.FromResult(new PaginationResult<Character>( return Task.FromResult(new PaginationResult<Character>(charLst.Count, 0, charLst.Count, charLst));
charLst.Count, // Total count of characters in the database
0, // Current page (in this case, no pagination logic implemented)
charLst.Count, // Total number of items (same as count for no pagination)
charLst // The list of characters
));
} }
/// <summary> /// <summary>

@ -0,0 +1,111 @@
using Entity;
using Microsoft.EntityFrameworkCore;
using Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contextlib
{
public class DbCommentManager : ICommentService<Commentary>
{
private WTFContext _context;
public DbCommentManager(WTFContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
}
/// <summary>
/// Adds a new comment to the database.
/// </summary>
/// <param name="comment">The comment to add.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">Thrown when the comment is null.</exception>
public async Task AddComment(Commentary comment)
{
if (comment == null)
{
throw new ArgumentNullException(nameof(comment), "Comment cannot be null.");
}
await _context.comments.AddAsync(comment);
await _context.SaveChangesAsync();
}
/// <summary>
/// Deletes all comments associated with a specific quote ID from the database.
/// </summary>
/// <param name="quoteId">The ID of the quote whose comments need to be deleted.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided quote ID.</exception>
public async Task DeleteCommentForQuote(int quoteId)
{
// Retrieve comments for the specific quoteId
var comments = await _context.comments.Where(x => x.IdQuote == quoteId).ToListAsync();
if (!comments.Any()) // If no comments exist for this quoteId
{
throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}.");
}
_context.comments.RemoveRange(comments); // Efficiently remove all comments in one batch
await _context.SaveChangesAsync();
}
/// <summary>
/// Deletes all comments associated with a specific user ID from the database.
/// </summary>
/// <param name="userId">The ID of the user whose comments need to be deleted.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided user ID.</exception>
public async Task DeleteCommentForUser(int userId)
{
// Retrieve comments for the specific userId
var comments = await _context.comments.Where(x => x.IdUsers == userId).ToListAsync();
if (!comments.Any()) // If no comments exist for this userId
{
throw new KeyNotFoundException($"No comments found for the user ID: {userId}.");
}
_context.comments.RemoveRange(comments); // Efficiently remove all comments in one batch
await _context.SaveChangesAsync();
}
public async Task<PaginationResult<Commentary>> GetAllComment()
{
throw new NotImplementedException();
}
public async Task<Commentary> GetCommentById(int id)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Commentary>> GetCommentByQuote(int quoteId, int index, int pageSize)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Commentary>> GetCommentByUser(int userId, int index, int pageSize)
{
throw new NotImplementedException();
}
public async Task<int> LastCommentId()
{
throw new NotImplementedException();
}
public async Task RemoveComment(int id)
{
throw new NotImplementedException();
}
public async Task UpdateComment(int id, Commentary comment)
{
throw new NotImplementedException();
}
}
}

@ -10,20 +10,40 @@ namespace Contextlib
{ {
public class WTFContext : DbContext public class WTFContext : DbContext
{ {
public DbSet<Admin> admins { get; set; } //public DbSet<Admin> admins { get; set; }
public DbSet<Character> characters { get; set; } public DbSet<Character> characters { get; set; }
public DbSet<Commentary> comments { get; set; } public DbSet<Commentary> comments { get; set; }
public DbSet<DailyQuote> dailyquotes { get; set; } public DbSet<DailyQuote> dailyquotes { get; set; }
public DbSet<Favorite> favorites { get; set; } //public DbSet<Favorite> favorites { get; set; }
public DbSet<Images> images { get; set; } public DbSet<Images> images { get; set; }
public DbSet<Question> question { get; set; } public DbSet<Question> question { get; set; }
public DbSet<Quiz> quizzes { get; set; } public DbSet<Quiz> quizzes { get; set; }
public DbSet<QuizQuestion> quizQuestions { get; set; } //public DbSet<QuizQuestion> quizQuestions { get; set; }
public DbSet<Quote> quotes { get; set; } public DbSet<Quote> quotes { get; set; }
//public DbSet<RecordQuiz> records { get; set; } //public DbSet<RecordQuiz> records { get; set; }
public DbSet<Source> sources { get; set; } public DbSet<Source> sources { get; set; }
public DbSet<Users> users { get; set; } public DbSet<Users> users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Users>()
.HasMany(q => q.Favorite)
.WithMany(u => u.Favorite)
.UsingEntity<Favorite>(
l => l.HasOne<Quote>().WithMany().HasForeignKey(q => q.IdQuote),
r => r.HasOne<Users>().WithMany().HasForeignKey(u => u.IdUsers)
);
modelBuilder.Entity<Quiz>()
.HasMany(q => q.Questions)
.WithMany(u => u.Quizs)
.UsingEntity<QuizQuestion>(
l => l.HasOne<Question>().WithMany().HasForeignKey(q => q.IdQuestion),
r => r.HasOne<Quiz>().WithMany().HasForeignKey(u => u.IdQuiz)
);
}
protected override void OnConfiguring(DbContextOptionsBuilder options) protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=WfDatabase.mdf;Trusted_Connection=True;"); => options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=WfDatabase.mdf;Trusted_Connection=True;");

@ -10,16 +10,8 @@ namespace Entity
{ {
public class Favorite public class Favorite
{ {
[Key]
[ForeignKey(nameof(Users))]
public int IdUsers { get; set; } public int IdUsers { get; set; }
[Key]
[ForeignKey(nameof(Quote))]
public int IdQuote { get; set; } public int IdQuote { get; set; }
public Users User { get; set; } = null!;
public Quote Quote { get; set; } = null!;
} }
} }

@ -38,6 +38,6 @@ namespace Entity
[StringLength(1)] [StringLength(1)]
public string CorrectAnswer { get; set; } public string CorrectAnswer { get; set; }
public ICollection<Quiz> Quizs { get; } = new List<Quiz>(); public ICollection<Quiz> Quizs { get; set; } = new List<Quiz>();
} }
} }

@ -10,16 +10,8 @@ namespace Entity
{ {
public class QuizQuestion public class QuizQuestion
{ {
[Key]
[ForeignKey(nameof(Quiz))]
public int IdQuiz { get; set; } public int IdQuiz { get; set; }
[Key]
[ForeignKey(nameof(Question))]
public int IdQuestion { get; set; } public int IdQuestion { get; set; }
public Quiz Quiz { get; set; } = null!;
public Question Question { get; set; } = null!;
} }
} }

@ -43,6 +43,6 @@ namespace Entity
public ICollection<Commentary> commentaries { get; set; } = new List<Commentary>(); public ICollection<Commentary> commentaries { get; set; } = new List<Commentary>();
public ICollection<Favorite> Favorite { get; set; } = new List<Favorite>(); public ICollection<Users> Favorite { get; } = new List<Users>();
} }
} }

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

@ -46,7 +46,7 @@ namespace Shared
// Deletes all comments made by a specific user. // Deletes all comments made by a specific user.
// 'userId' is the unique identifier of the user whose comments will be deleted. // 'userId' is the unique identifier of the user whose comments will be deleted.
Task DeleteCommentForuser(int userId); Task DeleteCommentForUser(int userId);
// Retrieves the last comment ID. // Retrieves the last comment ID.
Task<int> LastCommentId(); Task<int> LastCommentId();

@ -15,9 +15,9 @@ namespace StubbedContextLib
{ {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Admin>().HasData( /*modelBuilder.Entity<Admin>().HasData(
new Admin() { IdUsers = 1 } new Admin() { IdUsers = 1 }
); );*/
modelBuilder.Entity<Character>().HasData( modelBuilder.Entity<Character>().HasData(
new Character() { Id = 1 , Name = "Alan Grant", IdImage = 1}, new Character() { Id = 1 , Name = "Alan Grant", IdImage = 1},

Loading…
Cancel
Save