From 7ff1bacb96bfc5ea9a30f688383ede4c6984e1ef Mon Sep 17 00:00:00 2001 From: Kevin MONDEJAR Date: Fri, 14 Mar 2025 11:22:26 +0100 Subject: [PATCH] =?UTF-8?q?Fin=20ajout=20donn=C3=A9es=20stuber=20+=20d?= =?UTF-8?q?=C3=A9but=20DbManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WF_EF_Api/Contextlib/DbCharacterManager.cs | 154 ++++++++++++++++++ WF_EF_Api/StubbedContextLib/StubWTFContext.cs | 12 +- 2 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 WF_EF_Api/Contextlib/DbCharacterManager.cs diff --git a/WF_EF_Api/Contextlib/DbCharacterManager.cs b/WF_EF_Api/Contextlib/DbCharacterManager.cs new file mode 100644 index 0000000..65f19e9 --- /dev/null +++ b/WF_EF_Api/Contextlib/DbCharacterManager.cs @@ -0,0 +1,154 @@ +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 DbCharacterManager : ICharacterService + { + 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."); + } + + /// + /// Adds a new character to the database. + /// + /// The character to be added to the database. + /// A task that represents the asynchronous operation. + /// Thrown when the provided character is null. + public async Task AddCharacter(Character character) + { + if (character == null) + { + throw new ArgumentNullException(nameof(character), "Character cannot be null."); + } + + await _context.AddAsync(character); + await _context.SaveChangesAsync(); + } + + /// + /// Retrieves all characters from the database and returns them in a paginated format. + /// + /// 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(); + + 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 + )); + } + + /// + /// Retrieves a character by its ID from the database. + /// + /// The unique identifier of the character to retrieve. + /// A task that represents the asynchronous operation, with a as its result. + /// Thrown when no character is found with the given ID. + public async Task GetCharById(int id) + { + Character? character = await _context.characters + .FirstOrDefaultAsync(x => x.Id == id); + + if (character == null) + { + throw new KeyNotFoundException($"Error : No character found with the ID: {id}."); + } + + return character; + } + + /// + /// Retrieves a character by its name from the database. + /// + /// The name of the character to retrieve. + /// A task that represents the asynchronous operation, with a as its result. + /// 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)); + + if (character == null) + { + throw new KeyNotFoundException($"Error : No character found with the name: {name}."); + } + + return character; + } + + /// + /// Retrieves the highest character ID from the database. + /// + /// The highest character ID in the database. 0 if there is no character in the database + public async Task GetLastCharId() + { + int id = await _context.characters + .OrderByDescending(x => x.Id) + .Select(x => x.Id) + .FirstOrDefaultAsync(); + return id; + } + + /// + /// Removes a character from the database by its ID. + /// + /// The ID of the character to remove. + /// A task representing the asynchronous operation. + /// Thrown when no character is found with the given ID. + public async Task RemoveCharacter(int id) + { + Character? character = await _context.characters.FirstOrDefaultAsync(x => x.Id == id); + if (character == null) + { + throw new KeyNotFoundException($"Error : Unable to delete, no character found with the ID: {id}."); + } + + _context.characters.Remove(character); + await _context.SaveChangesAsync(); + } + + /// + /// Updates a character's details by its ID. + /// + /// The ID of the character to update. + /// The updated character information. + /// A task representing the asynchronous operation. + /// /// Thrown when the provided character is null. + /// Thrown when no character is found with the given ID. + public async Task UpdateCharacter(int id, Character character) + { + if (character == null) + { + throw new ArgumentNullException(nameof(character), "The updated character data cannot be null."); + } + + 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(); + } + } +} diff --git a/WF_EF_Api/StubbedContextLib/StubWTFContext.cs b/WF_EF_Api/StubbedContextLib/StubWTFContext.cs index 8ef24d8..8fda1c6 100644 --- a/WF_EF_Api/StubbedContextLib/StubWTFContext.cs +++ b/WF_EF_Api/StubbedContextLib/StubWTFContext.cs @@ -100,9 +100,17 @@ namespace StubbedContextLib modelBuilder.Entity().HasData( new Quote() { Id = 1, Content = "Je n'y crois pas. Je n'y crois pas. Ce n'est pas possible", IdCharacter = 1, IdSource = 1, IdUsersPropose = 1 , IsValid = true, Langage = LangEnum.vf, Likes = 11025}, - new Quote() { Id = 1, Content = "There is always hope", IdCharacter = 2, IdSource = 2, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vo, Likes = 11025 }, - new Quote() { Id = 1, Content = "A red sun rises. Blood has been spilled this night.", IdCharacter = 3, IdSource = 2, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vo, Likes = 11025 } + new Quote() { Id = 2, Content = "There is always hope", IdCharacter = 2, IdSource = 2, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vo, Likes = 11025 }, + new Quote() { Id = 3, Content = "A red sun rises. Blood has been spilled this night.", IdCharacter = 3, IdSource = 2, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vo, Likes = 11025 }, + new Quote() { Id = 4, Content = "I wish the Ring had never come to me.I wish none of this had happened.", IdCharacter = 4, IdSource = 2, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vo, Likes = 11025 }, + new Quote() { Id = 5, Content = "Dobby is a free elf!", IdCharacter = 5 , IdSource = 4 , IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vo, Likes = 11025 }, + new Quote() { Id = 6, Content = "Winter is comming", IdCharacter = 6 , IdSource = 3, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vo, Likes = 11025 }, + new Quote() { Id = 7, Content = "Je suis la dernière Targaryen. Je suis la reine des dragons", IdCharacter = 7 , IdSource = 3, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vf, Likes = 11025 }, + new Quote() { Id = 8, Content = "Je ne suis pas prêt à affronter ça. C'est trop pour moi.", IdCharacter = 8 , IdSource = 5, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vf, Likes = 11025 }, + new Quote() { Id = 9, Content = "Aidez-moi, Obi-Wan Kenobi, vous êtes mon seul espoir.", IdCharacter = 9 , IdSource = 5, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vf, Likes = 11025 }, + 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 },