From d0c0519ca0c23e6a80f984a8648aaf55e28c1dcc Mon Sep 17 00:00:00 2001 From: Kevin MONDEJAR Date: Thu, 20 Mar 2025 12:01:14 +0100 Subject: [PATCH] ajout console test + continuation dbmanager + injection fournisseur --- WF_EF_Api/ConsoleTest/ConsoleTest.csproj | 8 ++ WF_EF_Api/ConsoleTest/Program.cs | 42 +++++++- WF_EF_Api/Contextlib/DbCommentaryManager.cs | 19 +++- WF_EF_Api/Contextlib/DbFavoriteManager.cs | 40 +++++++ WF_EF_Api/Contextlib/DbImagesManager.cs | 101 ++++++++++++++++++ WF_EF_Api/Contextlib/DbQuestionSerice.cs | 73 +++++++++++++ WF_EF_Api/Contextlib/WTFContext.cs | 15 ++- WF_EF_Api/Shared/IImagesService.cs | 2 +- WF_EF_Api/StubbedContextLib/StubWTFContext.cs | 14 ++- WF_EF_Api/TestEf/Program.cs | 2 + WF_EF_Api/TestEf/TestEf.csproj | 10 ++ WF_EF_Api/WF_EF_Api.sln | 6 -- WF_EF_Api/XUnitTest/UnitTest1.cs | 11 -- WF_EF_Api/XUnitTest/XUnitTest.csproj | 5 + 14 files changed, 324 insertions(+), 24 deletions(-) create mode 100644 WF_EF_Api/Contextlib/DbFavoriteManager.cs create mode 100644 WF_EF_Api/Contextlib/DbImagesManager.cs create mode 100644 WF_EF_Api/Contextlib/DbQuestionSerice.cs create mode 100644 WF_EF_Api/TestEf/Program.cs create mode 100644 WF_EF_Api/TestEf/TestEf.csproj delete mode 100644 WF_EF_Api/XUnitTest/UnitTest1.cs diff --git a/WF_EF_Api/ConsoleTest/ConsoleTest.csproj b/WF_EF_Api/ConsoleTest/ConsoleTest.csproj index 206b89a..9a4ee54 100644 --- a/WF_EF_Api/ConsoleTest/ConsoleTest.csproj +++ b/WF_EF_Api/ConsoleTest/ConsoleTest.csproj @@ -7,4 +7,12 @@ enable + + + + + + + + diff --git a/WF_EF_Api/ConsoleTest/Program.cs b/WF_EF_Api/ConsoleTest/Program.cs index 127ed44..6a3e921 100644 --- a/WF_EF_Api/ConsoleTest/Program.cs +++ b/WF_EF_Api/ConsoleTest/Program.cs @@ -1 +1,41 @@ -Console.WriteLine("bonjour"); \ No newline at end of file +using Contextlib; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using StubbedContextLib; +using static System.Net.WebRequestMethods; + +var connection = new SqliteConnection("DataSource=:memory:"); +connection.Open(); +var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + +using (var _context = new StubWTFContext(options)) +{ + _context.Database.EnsureCreated(); + + // ---- Test Image ---- // + + var imageManager = new DbImagesManager(_context); + await imageManager.AddImage(new Entity.Images() { Id = 11, ImgPath = "https://www.bing.com/ck/a?!&&p=390428c2820add92760900204667aa721b17d4eb9e8537c91544d76283d06b14JmltdHM9MTc0MjQyODgwMA&ptn=3&ver=2&hsh=4&fclid=297ef5ed-ac44-66f2-2498-e058adb06776&u=a1aHR0cHM6Ly93d3cucG9rZXBlZGlhLmZyL01hamFzcGlj&ntb=1" }); + + // ---- Test Character ---- // + + var characterManager = new DbCharacterManager(_context); + + + await characterManager.AddCharacter(new Entity.Character() { Id = 11, Name = "Majespic", IdImage = 11}); + + // recupération données + var characters = await characterManager.GetAll(); + // affichage des dponnées récupérer + foreach (var charac in characters.items) + { + Console.WriteLine("(" + charac.Id + ") " + charac.Name + " / Image ref :" + charac.IdImage); + } + + +} + + diff --git a/WF_EF_Api/Contextlib/DbCommentaryManager.cs b/WF_EF_Api/Contextlib/DbCommentaryManager.cs index 558aa46..56c0398 100644 --- a/WF_EF_Api/Contextlib/DbCommentaryManager.cs +++ b/WF_EF_Api/Contextlib/DbCommentaryManager.cs @@ -73,7 +73,7 @@ namespace Contextlib public async Task> GetAllComment() { - var comments = _context.comments.ToList(); + var comments = await _context.comments.ToListAsync(); return new PaginationResult(comments.Count, 0, comments.Count, comments); } @@ -133,7 +133,7 @@ namespace Contextlib public async Task LastCommentId() { - var last = _context.comments.OrderByDescending(x => x.Id).FirstOrDefault(); + var last = await _context.comments.OrderByDescending(x => x.Id).FirstOrDefaultAsync(); if(last == null) { return 0; @@ -154,7 +154,20 @@ namespace Contextlib public async Task UpdateComment(int id, Commentary comment) { - throw new NotImplementedException(); + var com = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync(); + if (comment == null) + { + throw new ArgumentNullException(nameof(comment), "The updated comment data cannot be null."); + } + if (com == null) + { + throw new KeyNotFoundException($"No comments found with the given ID: {id}."); + } + com.Comment = comment.Comment; + com.DateCommentary = comment.DateCommentary; + com.IdQuote = comment.IdQuote; + com.IdUser = comment.IdUser; + await _context.SaveChangesAsync(); } } } diff --git a/WF_EF_Api/Contextlib/DbFavoriteManager.cs b/WF_EF_Api/Contextlib/DbFavoriteManager.cs new file mode 100644 index 0000000..314da25 --- /dev/null +++ b/WF_EF_Api/Contextlib/DbFavoriteManager.cs @@ -0,0 +1,40 @@ +using Entity; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contextlib +{ + public class DbFavoriteManager : IFavoriteService + { + private WTFContext _context; + + public DbFavoriteManager(WTFContext context) + { + _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); + } + + public async Task AddFavorite(int quoteid, int userId) + { + throw new NotImplementedException(); + } + + public async Task RemoveAllFavoriteForQuote(int quoteId) + { + throw new NotImplementedException(); + } + + public async Task RemoveAllFavoriteForUser(int userId) + { + throw new NotImplementedException(); + } + + public async Task RemoveFavorite(int quoteid, int userId) + { + throw new NotImplementedException(); + } + } +} diff --git a/WF_EF_Api/Contextlib/DbImagesManager.cs b/WF_EF_Api/Contextlib/DbImagesManager.cs new file mode 100644 index 0000000..2ef9d7b --- /dev/null +++ b/WF_EF_Api/Contextlib/DbImagesManager.cs @@ -0,0 +1,101 @@ +using Entity; +using Microsoft.EntityFrameworkCore; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace Contextlib +{ + public class DbImagesManager : IImagesService + { + private WTFContext _context; + + public DbImagesManager(WTFContext context) + { + _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); + } + + public async Task AddImage(Images image) + { + await _context.AddAsync(image); + await _context.SaveChangesAsync(); + } + + public async Task> GetAllImage() + { + var images = await _context.images.ToListAsync(); + return new PaginationResult(images.Count, 0, images.Count, images); + } + + public async Task GetImageById(int id) + { + var image = await _context.images.Where(x => x.Id == id).FirstOrDefaultAsync(); + if (image == null) + { + throw new KeyNotFoundException($"No image found with the given ID: {id}."); + } + return image; + } + + public async Task GetLastImageId() + { + var last = await _context.images.OrderByDescending(x => x.Id).FirstOrDefaultAsync(); + if (last == null) + { + return 0; + } + return last.Id; + } + + public async Task> GetSomeImage(int index, int pageSize) + { + var images = await _context.images.ToListAsync(); + if (!images.Any()) + { + throw new KeyNotFoundException($"No images found"); + } + if ((index * pageSize + pageSize) > images.Count) + { + if (pageSize > images.Count) + { + return new PaginationResult(images.Count(), index, pageSize, images); + } + else + { + return new PaginationResult(pageSize, index, pageSize, images.Skip(index * pageSize - (((index * pageSize) + pageSize) - images.Count)).Take(pageSize).ToList()); + } + } + return new PaginationResult(images.Count, index, pageSize, images.Skip(index * pageSize).Take(pageSize).ToList()); + } + + public async Task RemoveImage(int id) + { + var image = await _context.images.Where(x => x.Id == id).FirstOrDefaultAsync(); + if (image == null) + { + throw new KeyNotFoundException($"No image found with the given ID: {id}."); + } + _context.images.Remove(image); + await _context.SaveChangesAsync(); + } + + public async Task UpdateImage(int id, Images image) + { + var img = await _context.images.Where(x => x.Id == id).FirstOrDefaultAsync(); + if (image == null) + { + throw new ArgumentNullException(nameof(image), "The updated image data cannot be null."); + } + if (img == null) + { + throw new KeyNotFoundException($"No image found with the given ID: {id}."); + } + img.ImgPath = image.ImgPath; + await _context.SaveChangesAsync(); + } + } +} diff --git a/WF_EF_Api/Contextlib/DbQuestionSerice.cs b/WF_EF_Api/Contextlib/DbQuestionSerice.cs new file mode 100644 index 0000000..010f38c --- /dev/null +++ b/WF_EF_Api/Contextlib/DbQuestionSerice.cs @@ -0,0 +1,73 @@ +using Entity; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contextlib +{ + public class DbQuestionSerice : IQuestionService + { + public Task AddQuestion(Question question) + { + throw new NotImplementedException(); + } + + public Task CountQuestions() + { + throw new NotImplementedException(); + } + + public Task> GetAllQuestion() + { + throw new NotImplementedException(); + } + + public Task> GetInvalidQuestion(int index, int pageSize) + { + throw new NotImplementedException(); + } + + public Task GetQuestionById(int id) + { + throw new NotImplementedException(); + } + + public Task GetRandomQuestion() + { + throw new NotImplementedException(); + } + + public Task GetRandomQuestionQuoteToCharacter() + { + throw new NotImplementedException(); + } + + public Task GetRandomQuestionQuoteToSource() + { + throw new NotImplementedException(); + } + + public Task> GetSomeQuestion(int index, int pageSize) + { + throw new NotImplementedException(); + } + + public Task RemoveQuestion(int id) + { + throw new NotImplementedException(); + } + + public Task UpdateQuestion(int id, Question question) + { + throw new NotImplementedException(); + } + + public Task ValidateQuestion(int id, bool isvalid) + { + throw new NotImplementedException(); + } + } +} diff --git a/WF_EF_Api/Contextlib/WTFContext.cs b/WF_EF_Api/Contextlib/WTFContext.cs index e0f0a31..4b95f3b 100644 --- a/WF_EF_Api/Contextlib/WTFContext.cs +++ b/WF_EF_Api/Contextlib/WTFContext.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using Entity; @@ -75,7 +76,19 @@ namespace Contextlib ); } + public WTFContext() + { } + + public WTFContext(DbContextOptions options) + : base(options) + { } + protected override void OnConfiguring(DbContextOptionsBuilder options) - => options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Wf-Database.mdf;Trusted_Connection=True;"); + { + if (!options.IsConfigured) + { + options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Wf-Database.mdf;Trusted_Connection=True;"); + } + } } } diff --git a/WF_EF_Api/Shared/IImagesService.cs b/WF_EF_Api/Shared/IImagesService.cs index e2d0465..0bc6bf0 100644 --- a/WF_EF_Api/Shared/IImagesService.cs +++ b/WF_EF_Api/Shared/IImagesService.cs @@ -10,7 +10,7 @@ namespace Shared { // Retrieves an image by its unique identifier (id). // 'id' is the unique identifier of the image. - Task GetImageById(string id); + Task GetImageById(int id); // Retrieves all images, with pagination support. // This returns a list of all images in the system. diff --git a/WF_EF_Api/StubbedContextLib/StubWTFContext.cs b/WF_EF_Api/StubbedContextLib/StubWTFContext.cs index ffc1d06..7c04527 100644 --- a/WF_EF_Api/StubbedContextLib/StubWTFContext.cs +++ b/WF_EF_Api/StubbedContextLib/StubWTFContext.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Contextlib; using Entity; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; namespace StubbedContextLib { @@ -133,10 +134,21 @@ namespace StubbedContextLib new Source() { Id = 4, Title = "Harry Potter à l'école des sorcier", TypeSrc = TypeSrcEnum.movie, Year = 1997 }, new Source() { Id = 5, Title = "Star Wars, épisode IV : Un nouvel espoir", TypeSrc = TypeSrcEnum.movie, Year = 1977 } ); + } - + public StubWTFContext() + { } + public StubWTFContext(DbContextOptions options) + : base(options) + { } + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + if (!options.IsConfigured) + { + options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Wf-Database.mdf;Trusted_Connection=True;"); + } } } } diff --git a/WF_EF_Api/TestEf/Program.cs b/WF_EF_Api/TestEf/Program.cs new file mode 100644 index 0000000..83fa4f4 --- /dev/null +++ b/WF_EF_Api/TestEf/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/WF_EF_Api/TestEf/TestEf.csproj b/WF_EF_Api/TestEf/TestEf.csproj new file mode 100644 index 0000000..206b89a --- /dev/null +++ b/WF_EF_Api/TestEf/TestEf.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/WF_EF_Api/WF_EF_Api.sln b/WF_EF_Api/WF_EF_Api.sln index 2b163b7..b82d44f 100644 --- a/WF_EF_Api/WF_EF_Api.sln +++ b/WF_EF_Api/WF_EF_Api.sln @@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contextlib", "Contextlib\Co EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{5CD69B14-C6AE-4628-A374-996C486E25F2}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestModel2Entities", "TestModel2Entities\TestModel2Entities.csproj", "{2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model2Entities", "Model2entities\Model2Entities.csproj", "{4A1CBA3D-C798-4E19-865F-39F919F1205A}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnitTest", "XUnitTest\XUnitTest.csproj", "{48002CA2-7CFF-4077-90CF-392476320CE3}" @@ -49,10 +47,6 @@ Global {5CD69B14-C6AE-4628-A374-996C486E25F2}.Debug|Any CPU.Build.0 = Debug|Any CPU {5CD69B14-C6AE-4628-A374-996C486E25F2}.Release|Any CPU.ActiveCfg = Release|Any CPU {5CD69B14-C6AE-4628-A374-996C486E25F2}.Release|Any CPU.Build.0 = Release|Any CPU - {2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}.Release|Any CPU.Build.0 = Release|Any CPU {4A1CBA3D-C798-4E19-865F-39F919F1205A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4A1CBA3D-C798-4E19-865F-39F919F1205A}.Debug|Any CPU.Build.0 = Debug|Any CPU {4A1CBA3D-C798-4E19-865F-39F919F1205A}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/WF_EF_Api/XUnitTest/UnitTest1.cs b/WF_EF_Api/XUnitTest/UnitTest1.cs deleted file mode 100644 index f8a0369..0000000 --- a/WF_EF_Api/XUnitTest/UnitTest1.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace XUnitTest -{ - public class UnitTest1 - { - [Fact] - public void Test1() - { - - } - } -} \ No newline at end of file diff --git a/WF_EF_Api/XUnitTest/XUnitTest.csproj b/WF_EF_Api/XUnitTest/XUnitTest.csproj index 3aa9860..29b8b14 100644 --- a/WF_EF_Api/XUnitTest/XUnitTest.csproj +++ b/WF_EF_Api/XUnitTest/XUnitTest.csproj @@ -11,11 +11,16 @@ + + + + +