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(); } } }