diff --git a/WF_EF_Api/ConsoleTest/Program.cs b/WF_EF_Api/ConsoleTest/Program.cs
index 6a3e921..7a36b87 100644
--- a/WF_EF_Api/ConsoleTest/Program.cs
+++ b/WF_EF_Api/ConsoleTest/Program.cs
@@ -1,8 +1,10 @@
using Contextlib;
+using Entity;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using StubbedContextLib;
+using static System.Net.Mime.MediaTypeNames;
using static System.Net.WebRequestMethods;
var connection = new SqliteConnection("DataSource=:memory:");
@@ -18,24 +20,115 @@ using (var _context = new StubWTFContext(options))
// ---- 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" });
+
+
+ await imageManager.AddImage(new 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" });
+ await imageManager.AddImage(new Images() { Id = 12, ImgPath = "https://www.bing.com/images/search?view=detailV2&ccid=t57OzeAT&id=1CCCBB65825E5FB93F10CA6D29EFDBBFEB5CDF27&thid=OIP.t57OzeATZKjBDDrzXqbc5gHaE7&mediaurl=https%3a%2f%2fimg-19.commentcamarche.net%2fP51ArxVXHJKsgdTzGDaqajlWJ3s%3d%2f1500x%2fsmart%2f7b5dd43e607643fea1a61960e3f66fc4%2fccmcms-commentcamarche%2f39481621.jpg&cdnurl=https%3a%2f%2fth.bing.com%2fth%2fid%2fR.b79ececde01364a8c10c3af35ea6dce6%3frik%3dJ99c67%252fb7yltyg%26pid%3dImgRaw%26r%3d0&exph=999&expw=1500&q=image&simid=608026907577902968&ck=0D54F216D075AD6E0ABC46B3AAB7E80A&selectedIndex=19&itb=0" });
+ Console.WriteLine("---- Test ajout image (id : 11, 12)");
+ var images = await imageManager.GetAllImage();
+ foreach (var image in images.items)
+ {
+ Console.WriteLine($"- ({image.Id}) : {(image.ImgPath.Length <= 40 ? image.ImgPath : image.ImgPath.Substring(0, 40)+"...")}");
+
+
+ }
+ Console.WriteLine("");
+
+
+ await imageManager.UpdateImage(12, new Images() { ImgPath = "https://testUpdate/stub"});
+ Console.WriteLine("---- Test mise a jour image (id : 12)");
+ images = await imageManager.GetAllImage();
+ foreach (var image in images.items)
+ {
+ Console.WriteLine($"- ({image.Id}) : {(image.ImgPath.Length <= 40 ? image.ImgPath : image.ImgPath.Substring(0, 40) + "...")}");
+ }
+ Console.WriteLine("");
+
+
+ await imageManager.RemoveImage(12);
+ Console.WriteLine("---- Test suppression image (id : 12)");
+ images = await imageManager.GetAllImage();
+ foreach (var image in images.items)
+ {
+ Console.WriteLine($"- ({image.Id}) : {(image.ImgPath.Length <= 40 ? image.ImgPath : image.ImgPath.Substring(0, 40) + "...")}");
+ }
+ Console.WriteLine("");
+
+
+ Console.WriteLine("---- Test getById image (id : 11)");
+ var img = await imageManager.GetImageById(11);
+ Console.WriteLine($"- ({img.Id}) : {(img.ImgPath.Length <= 40 ? img.ImgPath : img.ImgPath.Substring(0, 40) + "...")}");
+ Console.WriteLine("");
+
+
+ Console.WriteLine("---- Test getSomme image (nb : 5, page : 1)");
+ images = await imageManager.GetSomeImage(1,5);
+ foreach (var image in images.items)
+ {
+ Console.WriteLine($"- ({image.Id}) : {(image.ImgPath.Length <= 40 ? image.ImgPath : image.ImgPath.Substring(0, 40) + "...")}");
+ }
+ Console.WriteLine("");
+
+
+ Console.WriteLine("---- Test LastId image");
+ var id = await imageManager.GetLastImageId();
+ Console.WriteLine($"- Last image id : {id}");
+ Console.WriteLine("");
+
+ Console.WriteLine("-------------------------------------------------------------------------------");
// ---- Test Character ---- //
var characterManager = new DbCharacterManager(_context);
-
- await characterManager.AddCharacter(new Entity.Character() { Id = 11, Name = "Majespic", IdImage = 11});
- // recupération données
+ await characterManager.AddCharacter(new Character() { Id = 11, Name = "Vipélière", IdImage = 11 });
+ Console.WriteLine("---- Test ajout charcter (id : 11)");
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);
+ Console.WriteLine($"- ({charac.Id}) : {charac.Name} -> {charac.IdImage} : {(charac.Images.ImgPath.Length <= 40 ? charac.Images.ImgPath : charac.Images.ImgPath.Substring(0, 40) + "...")}");
+ }
+ Console.WriteLine("");
+
+
+ await characterManager.UpdateCharacter(11,new Character() {Name = "Majespic"});
+ Console.WriteLine("---- Test mise a jour charcter (id : 11)");
+ characters = await characterManager.GetAll();
+ foreach (var charac in characters.items)
+ {
+ Console.WriteLine($"- ({charac.Id}) : {charac.Name} -> {charac.IdImage} : {(charac.Images.ImgPath.Length <= 40 ? charac.Images.ImgPath : charac.Images.ImgPath.Substring(0, 40) + "...")}");
}
+ Console.WriteLine("");
+ await characterManager.RemoveCharacter(11);
+ Console.WriteLine("---- Test sup (id : 5)");
+ characters = await characterManager.GetAll();
+ foreach (var charac in characters.items)
+ {
+ Console.WriteLine($"- ({charac.Id}) : {charac.Name} -> {charac.IdImage} : {(charac.Images.ImgPath.Length <= 40 ? charac.Images.ImgPath : charac.Images.ImgPath.Substring(0, 40) + "...")}");
+ }
+ Console.WriteLine("");
+
+
+ Console.WriteLine("---- Test GetById (id : 5)");
+ var chara = await characterManager.GetCharById(5);
+ Console.WriteLine($"- ({chara.Id}) : {chara.Name} -> {chara.IdImage} : {(chara.Images.ImgPath.Length <= 40 ? chara.Images.ImgPath : chara.Images.ImgPath.Substring(0, 40) + "...")}");
+ Console.WriteLine("");
+
+
+ Console.WriteLine("---- Test GetByName (name : Jon Snow)");
+ chara = await characterManager.GetCharByName("Jon Snow");
+ Console.WriteLine($"- ({chara.Id}) : {chara.Name} -> {chara.IdImage} : {(chara.Images.ImgPath.Length <= 40 ? chara.Images.ImgPath : chara.Images.ImgPath.Substring(0, 40) + "...")}");
+ Console.WriteLine("");
+
+
+ Console.WriteLine("---- Test LastId Character");
+ id = await characterManager.GetLastCharId();
+ Console.WriteLine($"- Last character id : {id}");
+ Console.WriteLine("");
}
+
diff --git a/WF_EF_Api/Contextlib/DbCharacterManager.cs b/WF_EF_Api/Contextlib/DbCharacterManager.cs
index db1a7c0..e0d75e1 100644
--- a/WF_EF_Api/Contextlib/DbCharacterManager.cs
+++ b/WF_EF_Api/Contextlib/DbCharacterManager.cs
@@ -41,7 +41,7 @@ namespace Contextlib
/// A task representing the asynchronous operation, with a as its result containing the full list of characters and pagination information.
public Task> GetAll()
{
- List charLst = _context.characters.ToList();
+ List charLst = _context.characters.Include(i => i.Images).ToList();
return Task.FromResult(new PaginationResult(charLst.Count, 0, charLst.Count, charLst));
}
@@ -55,6 +55,7 @@ namespace Contextlib
public async Task GetCharById(int id)
{
Character? character = await _context.characters
+ .Include(i => i.Images)
.FirstOrDefaultAsync(x => x.Id == id);
if (character == null)
@@ -73,8 +74,9 @@ namespace Contextlib
/// Thrown when no character is found with the given name.
public async Task GetCharByName(string name)
{
- Character? character = await _context.characters
- .FirstOrDefaultAsync(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
+ var character = await _context.characters
+ .Where(c => EF.Functions.Like(c.Name, name))
+ .FirstOrDefaultAsync();
if (character == null)
{
@@ -129,17 +131,26 @@ namespace Contextlib
{
throw new ArgumentNullException(nameof(character), "The updated character data cannot be null.");
}
-
+ var modif = false;
Character? charUpdated = await _context.characters.FirstOrDefaultAsync(x => x.Id == id);
if (charUpdated == null)
{
throw new KeyNotFoundException($"Error : Unable to update, no character found with the ID: {id} .");
}
-
- charUpdated.IdImage = character.IdImage;
- charUpdated.Name = character.Name;
-
- await _context.SaveChangesAsync();
+ if (character.IdImage != 0)
+ {
+ charUpdated.IdImage = character.IdImage;
+ modif = true;
+ }
+ if (character.Name != null)
+ {
+ charUpdated.Name = character.Name;
+ modif = true;
+ }
+ if (modif)
+ {
+ await _context.SaveChangesAsync();
+ }
}
}
}
diff --git a/WF_EF_Api/Contextlib/DbCommentaryManager.cs b/WF_EF_Api/Contextlib/DbCommentaryManager.cs
index 56c0398..e5bde4f 100644
--- a/WF_EF_Api/Contextlib/DbCommentaryManager.cs
+++ b/WF_EF_Api/Contextlib/DbCommentaryManager.cs
@@ -1,5 +1,6 @@
using Entity;
using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Update;
using Shared;
using System;
using System.Collections.Generic;
@@ -61,7 +62,7 @@ namespace Contextlib
/// Thrown when no comments are found for the provided user ID.
public async Task DeleteCommentForUser(int userId)
{
- var comments = await _context.comments.Where(x => x.IdUser == userId).ToListAsync();
+ var comments = await _context.comments.Include(c => c.User).Where(x => x.IdUser == userId).ToListAsync();
if (!comments.Any())
{
throw new KeyNotFoundException($"No comments found for the user ID: {userId}.");
@@ -73,13 +74,13 @@ namespace Contextlib
public async Task> GetAllComment()
{
- var comments = await _context.comments.ToListAsync();
+ var comments = await _context.comments.Include(c => c.User).ToListAsync();
return new PaginationResult(comments.Count, 0, comments.Count, comments);
}
public async Task GetCommentById(int id)
{
- var comment = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync();
+ var comment = await _context.comments.Include(c => c.User).Where(x => x.Id == id).FirstOrDefaultAsync();
if(comment == null)
{
throw new KeyNotFoundException($"No comments found with the given ID: {id}.");
@@ -89,7 +90,7 @@ namespace Contextlib
public async Task> GetCommentByQuote(int quoteId, int index, int pageSize)
{
- var comments = await _context.comments.Where(x => x.IdQuote == quoteId).ToListAsync();
+ var comments = await _context.comments.Include(c => c.User).Where(x => x.IdQuote == quoteId).ToListAsync();
if (!comments.Any())
{
throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}.");
@@ -111,7 +112,7 @@ namespace Contextlib
public async Task> GetCommentByUser(int userId, int index, int pageSize)
{
- var comments = await _context.comments.Where(x => x.IdUser == userId).ToListAsync();
+ var comments = await _context.comments.Include(c => c.User).Where(x => x.IdUser == userId).ToListAsync();
if (!comments.Any())
{
throw new KeyNotFoundException($"No comments found for the user ID: {userId}.");
@@ -154,6 +155,7 @@ namespace Contextlib
public async Task UpdateComment(int id, Commentary comment)
{
+ var modif = false;
var com = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync();
if (comment == null)
{
@@ -163,11 +165,29 @@ namespace Contextlib
{
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();
+ if (comment.Comment != null)
+ {
+ com.Comment = comment.Comment;
+ modif = true;
+ }
+ if(comment.DateCommentary != null){
+ com.DateCommentary = comment.DateCommentary;
+ modif = true;
+ }
+ if(comment.IdQuote != 0)
+ {
+ com.IdQuote = comment.IdQuote;
+ modif = true;
+ }
+ if (comment.IdUser != 0)
+ {
+ com.IdUser = comment.IdUser;
+ modif = true;
+ }
+ if (modif)
+ {
+ await _context.SaveChangesAsync();
+ }
}
}
}
diff --git a/WF_EF_Api/Contextlib/DbImagesManager.cs b/WF_EF_Api/Contextlib/DbImagesManager.cs
index 2ef9d7b..ad51785 100644
--- a/WF_EF_Api/Contextlib/DbImagesManager.cs
+++ b/WF_EF_Api/Contextlib/DbImagesManager.cs
@@ -85,6 +85,7 @@ namespace Contextlib
public async Task UpdateImage(int id, Images image)
{
+ var modif = false;
var img = await _context.images.Where(x => x.Id == id).FirstOrDefaultAsync();
if (image == null)
{
@@ -94,8 +95,15 @@ namespace Contextlib
{
throw new KeyNotFoundException($"No image found with the given ID: {id}.");
}
- img.ImgPath = image.ImgPath;
- await _context.SaveChangesAsync();
+ if (image.ImgPath != null)
+ {
+ img.ImgPath = image.ImgPath;
+ modif = true;
+ }
+ if (modif)
+ {
+ await _context.SaveChangesAsync();
+ }
}
}
}