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.
222 lines
8.9 KiB
222 lines
8.9 KiB
using Entity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Update;
|
|
using Shared;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Contextlib
|
|
{
|
|
public class DbCommentaryManager : ICommentaryService<Commentary>
|
|
{
|
|
private WTFContext _context;
|
|
private GenericRepository<Commentary> _repo;
|
|
|
|
public DbCommentaryManager(WTFContext context)
|
|
{
|
|
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
|
|
_repo = new GenericRepository<Commentary>(context);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a new comment to the database.
|
|
/// </summary>
|
|
/// <param name="comment">The comment to add.</param>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
/// <exception cref="ArgumentNullException">Thrown when the comment is null.</exception>
|
|
public async Task AddComment(Commentary comment, int idQuote)
|
|
{
|
|
if (comment == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(comment), "Comment cannot be null.");
|
|
}
|
|
|
|
var quote = await _context.quotes
|
|
.Include(q => q.Commentarys) // collection des commentaires est chargée
|
|
.FirstOrDefaultAsync(q => q.Id == idQuote);
|
|
|
|
var dbU = new DbUsersManager(_context);
|
|
var User = await dbU.GetUserByUsername(comment.User.UserName);
|
|
if (User == null)
|
|
{
|
|
throw new ArgumentException("Quote not exist", nameof(comment.User.UserName));
|
|
}
|
|
|
|
if (quote == null)
|
|
{
|
|
throw new ArgumentException("Quote not exist", nameof(idQuote));
|
|
}
|
|
|
|
comment.User = User;
|
|
comment.IdUser = User.Id;
|
|
|
|
// Lien entre le commentaire et la citation
|
|
comment.Quote = quote;
|
|
comment.IdQuote = idQuote;
|
|
|
|
// Ajout commentaire à la collection des commentaires de la citation
|
|
//_repo.Insert(comment);
|
|
_context.Add(comment);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes all comments associated with a specific quote ID from the database.
|
|
/// </summary>
|
|
/// <param name="quoteId">The ID of the quote whose comments need to be deleted.</param>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided quote ID.</exception>
|
|
public async Task DeleteCommentaryForQuote(int quoteId)
|
|
{
|
|
var comments = await _context.comments.Where(x => x.IdQuote == quoteId).ToListAsync();
|
|
if (!comments.Any())
|
|
{
|
|
throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}.");
|
|
}
|
|
|
|
_context.comments.RemoveRange(comments);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes all comments associated with a specific user ID from the database.
|
|
/// </summary>
|
|
/// <param name="userId">The ID of the user whose comments need to be deleted.</param>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided user ID.</exception>
|
|
public async Task DeleteCommentaryForUser(int userId)
|
|
{
|
|
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}.");
|
|
}
|
|
|
|
_context.comments.RemoveRange(comments);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<PaginationResult<Commentary>> GetAllCommentary()
|
|
{
|
|
var comments = await _context.comments.Include(c => c.User).Include(c=>c.User.Images).ToListAsync();
|
|
return new PaginationResult<Commentary>(comments.Count, 0, comments.Count, comments);
|
|
}
|
|
|
|
public async Task<Commentary> GetCommentaryById(int id)
|
|
{
|
|
var comment = await _context.comments.Include(c => c.User).Include(c => c.User.Images).Where(x => x.Id == id).FirstOrDefaultAsync();
|
|
if(comment == null)
|
|
{
|
|
throw new KeyNotFoundException($"No comments found with the given ID: {id}.");
|
|
}
|
|
return comment;
|
|
}
|
|
|
|
public async Task<PaginationResult<Commentary>> GetCommentaryByQuote(int quoteId, int index, int pageSize)
|
|
{
|
|
var comments = await _context.comments.Include(c => c.User).Include(c => c.User.Images).Where(x => x.IdQuote == quoteId).ToListAsync();
|
|
if (!comments.Any())
|
|
{
|
|
throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}.");
|
|
}
|
|
if ((index * pageSize + pageSize) > comments.Count)
|
|
{
|
|
if (pageSize > comments.Count)
|
|
{
|
|
return new PaginationResult<Commentary>(comments.Count(), index, pageSize, comments);
|
|
}
|
|
else
|
|
{
|
|
return new PaginationResult<Commentary>(pageSize, index, pageSize, comments.Skip(index * pageSize - (((index * pageSize) + pageSize) - comments.Count)).Take(pageSize).ToList());
|
|
}
|
|
|
|
}
|
|
return new PaginationResult<Commentary>(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList());
|
|
}
|
|
|
|
public async Task<PaginationResult<Commentary>> GetCommentaryByUser(int userId, int index, int pageSize)
|
|
{
|
|
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}.");
|
|
}
|
|
if ((index * pageSize + pageSize) > comments.Count)
|
|
{
|
|
if (pageSize > comments.Count)
|
|
{
|
|
return new PaginationResult<Commentary>(comments.Count(), index, pageSize, comments);
|
|
}
|
|
else
|
|
{
|
|
return new PaginationResult<Commentary>(pageSize, index, pageSize, comments.Skip(index * pageSize - (((index * pageSize) + pageSize) - comments.Count)).Take(pageSize).ToList());
|
|
}
|
|
|
|
}
|
|
return new PaginationResult<Commentary>(comments.Count, index, pageSize, comments.Skip(index * pageSize).Take(pageSize).ToList());
|
|
}
|
|
|
|
public async Task<int> LastCommentaryId()
|
|
{
|
|
var last = await _context.comments.OrderByDescending(x => x.Id).FirstOrDefaultAsync();
|
|
if(last == null)
|
|
{
|
|
return 0;
|
|
}
|
|
return last.Id;
|
|
}
|
|
|
|
public async Task RemoveCommentary(int id)
|
|
{
|
|
|
|
Commentary? commentary = await GetCommentaryById(id);
|
|
if (commentary == null)
|
|
{
|
|
throw new KeyNotFoundException($"Error : No comment found with the ID: {id}.");
|
|
}
|
|
_repo.Delete(commentary);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateCommentary(int id, Commentary comment)
|
|
{
|
|
var modif = false;
|
|
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}.");
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|