You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
6.6 KiB
155 lines
6.6 KiB
using Entity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Shared;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Contextlib
|
|
{
|
|
public class DbCharacterManager : ICharacterService<Character>
|
|
{
|
|
private WTFContext _context;
|
|
private GenericRepository<Character> _repo;
|
|
private DbImagesManager _dbI;
|
|
|
|
public DbCharacterManager(WTFContext context)
|
|
{
|
|
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
|
|
_repo = new GenericRepository<Character>(_context);
|
|
_dbI = new DbImagesManager(context);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a new character to the database.
|
|
/// </summary>
|
|
/// <param name="character">The character to be added to the database.</param>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
/// <exception cref="ArgumentNullException">Thrown when the provided character is null.</exception>
|
|
public async Task AddCharacter(Character character)
|
|
{
|
|
if (character == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(character), "character cannot be null.");
|
|
}
|
|
var image = await _dbI.GetImageByPath(character.Images.ImgPath);
|
|
if (image != null)
|
|
{
|
|
character.IdImage = image.Id;
|
|
character.Images = image;
|
|
}
|
|
_repo.Insert(character);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves all characters from the database and returns them in a paginated format.
|
|
/// </summary>
|
|
/// <returns>A task representing the asynchronous operation, with a <see cref="PaginationResult{Character}"/> as its result containing the full list of characters and pagination information.</returns>
|
|
public async Task<PaginationResult<Character>> GetAll()
|
|
{
|
|
List<Character> charLst = _repo.GetItems(0, _repo.Count(), [nameof(Character.Images)]).ToList();
|
|
return new PaginationResult<Character>(charLst.Count, 0, charLst.Count, charLst);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves a character by its ID from the database.
|
|
/// </summary>
|
|
/// <param name="id">The unique identifier of the character to retrieve.</param>
|
|
/// <returns>A task that represents the asynchronous operation, with a <see cref="Character"/> as its result.</returns>
|
|
/// <exception cref="KeyNotFoundException">Thrown when no character is found with the given ID.</exception>
|
|
public async Task<Character> GetCharById(int id)
|
|
{
|
|
Character? character = _repo.GetById(id, item => item.Id == id, nameof(Character.Images));
|
|
if (character == null)
|
|
{
|
|
throw new KeyNotFoundException($"Error : No character found with the ID: {id}.");
|
|
}
|
|
return character;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves a character by its name from the database.
|
|
/// </summary>
|
|
/// <param name="name">The name of the character to retrieve.</param>
|
|
/// <returns>A task that represents the asynchronous operation, with a <see cref="Character"/> as its result.</returns>
|
|
/// <exception cref="KeyNotFoundException">Thrown when no character is found with the given name.</exception>
|
|
public async Task<Character?> GetCharByName(string name)
|
|
{
|
|
var character = _repo.GetItems(item => item.Name == name,0,1, [nameof(Character.Images)]).FirstOrDefault();
|
|
|
|
return character;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves the highest character ID from the database.
|
|
/// </summary>
|
|
/// <returns>The highest character ID in the database. 0 if there is no character in the database</returns>
|
|
public async Task<int> GetLastCharId()
|
|
{
|
|
PaginationResult<Character> characters = await GetAll();
|
|
int lastCharId = 0;
|
|
foreach (Character character in characters.items)
|
|
{
|
|
if (character.Id >= lastCharId)
|
|
{
|
|
lastCharId = character.Id + 1;
|
|
}
|
|
}
|
|
return lastCharId;
|
|
}
|
|
|
|
public async Task<PaginationResult<Character>> GetSomeChar(int page, int count)
|
|
{
|
|
var charLst = _repo.GetItems(page, count, [nameof(Character.Images)]).ToList();
|
|
return new PaginationResult<Character>(charLst.Count, 0, charLst.Count, charLst);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a character from the database by its ID.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the character to remove.</param>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
/// <exception cref="KeyNotFoundException">Thrown when no character is found with the given ID.</exception>
|
|
public async Task RemoveCharacter(int id)
|
|
{
|
|
_repo.Delete(id);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a character's details by its ID.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the character to update.</param>
|
|
/// <param name="character">The updated character information.</param>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
/// /// <exception cref="ArgumentNullException">Thrown when the provided character is null.</exception>
|
|
/// <exception cref="KeyNotFoundException">Thrown when no character is found with the given ID.</exception>
|
|
public async Task UpdateCharacter(int id, Character character)
|
|
{
|
|
Character? charac = _repo.GetById(id);
|
|
if (charac != null)
|
|
{
|
|
bool change = false;
|
|
var image = await _dbI.GetImageByPath(character.Images.ImgPath);
|
|
if (image != null)
|
|
{
|
|
charac.IdImage = image.Id;
|
|
charac.Images = image;
|
|
change = true;
|
|
}
|
|
if (character.Name != null)
|
|
{
|
|
charac.Name = character.Name;
|
|
change = true;
|
|
}
|
|
_repo.Update(charac);
|
|
if (change) _context.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|