diff --git a/WF_EF_Api/Contextlib/Contextlib.csproj b/WF_EF_Api/Contextlib/Contextlib.csproj index 805901c..d2b9100 100644 --- a/WF_EF_Api/Contextlib/Contextlib.csproj +++ b/WF_EF_Api/Contextlib/Contextlib.csproj @@ -22,6 +22,7 @@ + diff --git a/WF_EF_Api/Contextlib/DbCharacterManager.cs b/WF_EF_Api/Contextlib/DbCharacterManager.cs index 65f19e9..db1a7c0 100644 --- a/WF_EF_Api/Contextlib/DbCharacterManager.cs +++ b/WF_EF_Api/Contextlib/DbCharacterManager.cs @@ -13,10 +13,6 @@ namespace Contextlib { private WTFContext _context; - /// - /// Initializes a new instance of the class. - /// - /// The instance used to interact with the database. public DbCharacterManager(WTFContext context) { _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); @@ -47,12 +43,7 @@ namespace Contextlib { List charLst = _context.characters.ToList(); - return Task.FromResult(new PaginationResult( - 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 - )); + return Task.FromResult(new PaginationResult(charLst.Count, 0, charLst.Count, charLst)); } /// diff --git a/WF_EF_Api/Contextlib/DbCommentaryManager.cs b/WF_EF_Api/Contextlib/DbCommentaryManager.cs new file mode 100644 index 0000000..85c7ac8 --- /dev/null +++ b/WF_EF_Api/Contextlib/DbCommentaryManager.cs @@ -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 + { + private WTFContext _context; + + public DbCommentManager(WTFContext context) + { + _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); + } + + /// + /// Adds a new comment to the database. + /// + /// The comment to add. + /// A task representing the asynchronous operation. + /// Thrown when the comment is null. + 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(); + } + + /// + /// Deletes all comments associated with a specific quote ID from the database. + /// + /// The ID of the quote whose comments need to be deleted. + /// A task representing the asynchronous operation. + /// Thrown when no comments are found for the provided quote ID. + 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(); + } + + /// + /// Deletes all comments associated with a specific user ID from the database. + /// + /// The ID of the user whose comments need to be deleted. + /// A task representing the asynchronous operation. + /// Thrown when no comments are found for the provided user ID. + 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> GetAllComment() + { + throw new NotImplementedException(); + } + + public async Task GetCommentById(int id) + { + throw new NotImplementedException(); + } + + public async Task> GetCommentByQuote(int quoteId, int index, int pageSize) + { + throw new NotImplementedException(); + } + + public async Task> GetCommentByUser(int userId, int index, int pageSize) + { + throw new NotImplementedException(); + } + + public async Task LastCommentId() + { + throw new NotImplementedException(); + } + + public async Task RemoveComment(int id) + { + throw new NotImplementedException(); + } + + public async Task UpdateComment(int id, Commentary comment) + { + throw new NotImplementedException(); + } + } +} diff --git a/WF_EF_Api/Contextlib/WTFContext.cs b/WF_EF_Api/Contextlib/WTFContext.cs index 02880c7..ad5e4e5 100644 --- a/WF_EF_Api/Contextlib/WTFContext.cs +++ b/WF_EF_Api/Contextlib/WTFContext.cs @@ -10,20 +10,40 @@ namespace Contextlib { public class WTFContext : DbContext { - public DbSet admins { get; set; } + //public DbSet admins { get; set; } public DbSet characters { get; set; } public DbSet comments { get; set; } public DbSet dailyquotes { get; set; } - public DbSet favorites { get; set; } + //public DbSet favorites { get; set; } public DbSet images { get; set; } public DbSet question { get; set; } public DbSet quizzes { get; set; } - public DbSet quizQuestions { get; set; } + //public DbSet quizQuestions { get; set; } public DbSet quotes { get; set; } //public DbSet records { get; set; } public DbSet sources { get; set; } public DbSet users { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity() + .HasMany(q => q.Favorite) + .WithMany(u => u.Favorite) + .UsingEntity( + l => l.HasOne().WithMany().HasForeignKey(q => q.IdQuote), + r => r.HasOne().WithMany().HasForeignKey(u => u.IdUsers) + ); + + modelBuilder.Entity() + .HasMany(q => q.Questions) + .WithMany(u => u.Quizs) + .UsingEntity( + l => l.HasOne().WithMany().HasForeignKey(q => q.IdQuestion), + r => r.HasOne().WithMany().HasForeignKey(u => u.IdQuiz) + ); + } protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=WfDatabase.mdf;Trusted_Connection=True;"); diff --git a/WF_EF_Api/Entity/Favorite.cs b/WF_EF_Api/Entity/Favorite.cs index 51165d5..ec60347 100644 --- a/WF_EF_Api/Entity/Favorite.cs +++ b/WF_EF_Api/Entity/Favorite.cs @@ -10,16 +10,8 @@ namespace Entity { public class Favorite { - [Key] - [ForeignKey(nameof(Users))] public int IdUsers { get; set; } - [Key] - [ForeignKey(nameof(Quote))] public int IdQuote { get; set; } - - public Users User { get; set; } = null!; - - public Quote Quote { get; set; } = null!; } } diff --git a/WF_EF_Api/Entity/Question.cs b/WF_EF_Api/Entity/Question.cs index df66009..036747d 100644 --- a/WF_EF_Api/Entity/Question.cs +++ b/WF_EF_Api/Entity/Question.cs @@ -38,6 +38,6 @@ namespace Entity [StringLength(1)] public string CorrectAnswer { get; set; } - public ICollection Quizs { get; } = new List(); + public ICollection Quizs { get; set; } = new List(); } } diff --git a/WF_EF_Api/Entity/QuizQuestion.cs b/WF_EF_Api/Entity/QuizQuestion.cs index eaf51c3..5e7086b 100644 --- a/WF_EF_Api/Entity/QuizQuestion.cs +++ b/WF_EF_Api/Entity/QuizQuestion.cs @@ -10,16 +10,8 @@ namespace Entity { public class QuizQuestion { - [Key] - [ForeignKey(nameof(Quiz))] public int IdQuiz { get; set; } - [Key] - [ForeignKey(nameof(Question))] public int IdQuestion { get; set; } - - public Quiz Quiz { get; set; } = null!; - - public Question Question { get; set; } = null!; } } diff --git a/WF_EF_Api/Entity/Quote.cs b/WF_EF_Api/Entity/Quote.cs index adc1dd8..e91cd13 100644 --- a/WF_EF_Api/Entity/Quote.cs +++ b/WF_EF_Api/Entity/Quote.cs @@ -43,6 +43,6 @@ namespace Entity public ICollection commentaries { get; set; } = new List(); - public ICollection Favorite { get; set; } = new List(); + public ICollection Favorite { get; } = new List(); } } diff --git a/WF_EF_Api/Entity/Users.cs b/WF_EF_Api/Entity/Users.cs index 81aa506..80e38a7 100644 --- a/WF_EF_Api/Entity/Users.cs +++ b/WF_EF_Api/Entity/Users.cs @@ -39,6 +39,6 @@ namespace Entity public ICollection Commentary { get; set; } = new List(); - public ICollection Favorite { get; set; } = new List(); + public ICollection Favorite { get; set; } = new List(); } } diff --git a/WF_EF_Api/Shared/IComentaryService.cs b/WF_EF_Api/Shared/IComentaryService.cs index 2cf4fe1..d5c4fc6 100644 --- a/WF_EF_Api/Shared/IComentaryService.cs +++ b/WF_EF_Api/Shared/IComentaryService.cs @@ -46,7 +46,7 @@ namespace Shared // Deletes all comments made by a specific user. // '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. Task LastCommentId(); diff --git a/WF_EF_Api/StubbedContextLib/StubWTFContext.cs b/WF_EF_Api/StubbedContextLib/StubWTFContext.cs index 8fda1c6..bd533d8 100644 --- a/WF_EF_Api/StubbedContextLib/StubWTFContext.cs +++ b/WF_EF_Api/StubbedContextLib/StubWTFContext.cs @@ -15,9 +15,9 @@ namespace StubbedContextLib { base.OnModelCreating(modelBuilder); - modelBuilder.Entity().HasData( + /*modelBuilder.Entity().HasData( new Admin() { IdUsers = 1 } - ); + );*/ modelBuilder.Entity().HasData( new Character() { Id = 1 , Name = "Alan Grant", IdImage = 1}, @@ -111,7 +111,7 @@ namespace StubbedContextLib new Quote() { Id = 10, Content = "La quoi ?", IdCharacter = 10 , IdSource = 4 , IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vf, Likes = 11025 } ); - + modelBuilder.Entity().HasData( new Source() { Id = 1, Title = "Jurassic Park", TypeSrc = TypeSrcEnum.movie, Year = 1993 }, new Source() { Id = 2, Title = "Le Seigneur des anneaux : La Communauté de l'anneau", TypeSrc = TypeSrcEnum.movie, Year = 2001 },