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.
WF-PmAPI/WF_EF_Api/Contextlib/DbQuoteManager.cs

299 lines
12 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Shared;
namespace Contextlib
{
public class DbQuoteManager : IQuoteService<Quote>
{
private WTFContext _context;
private GenericRepository<Quote> _repo;
private DbCharacterManager _dbC;
private DbSourceManager _dbS;
private DbImagesManager _dbI;
public DbQuoteManager(WTFContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
_repo = new GenericRepository<Quote>(_context);
_dbS = new DbSourceManager(_context);
_dbC = new DbCharacterManager(_context);
_dbI = new DbImagesManager(_context);
}
public async Task<Quote> AddQuote(Quote quote)
{
if (quote == null)
{
throw new ArgumentNullException(nameof(quote), "quote cannot be null.");
}
//Character
var c = await _dbC.GetCharByName(quote.Character.Name);
if (c != null)
{
quote.IdCharacter = c.Id;
quote.Character = c;
}
//Image
var i = await _dbI.GetImageByPath(quote.Character.Images.ImgPath);
if (i != null)
{
quote.Character.IdImage = i.Id;
quote.Character.Images = i;
}
//Source
var s = await _dbS.GetSourceByTitle(quote.Source.Title);
if (s != null)
{
quote.IdSource = s.Id;
quote.Source = s;
}
_repo.Insert(quote);
await _context.SaveChangesAsync();
return quote;
}
public async Task<PaginationResult<Quote>> GetAllQuote()
{
List<Quote> quotes = _context.quotes.Where(item => item.IsValid)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.ToList();
return new PaginationResult<Quote>(quotes.Count, 0, quotes.Count, quotes);
}
public async Task<PaginationResult<Quote>> GetAllQuoteLang(int index, int pageSize, int lang)
{
List<Quote> quotes = await _context.quotes.Where(item => item.IsValid && item.Langage == (LangEnum)lang)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.Skip(index * pageSize).Take(pageSize).ToListAsync();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<Quote?> GetDailyQuote(DateOnly date, int lang)
{
List<Quote> quotes = await _context.quotes.Where(item => item.IsValid && item.Langage == (LangEnum)lang)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite)
.ToListAsync();
if (quotes.Count() == 0) return null;
Quote quote = quotes[date.DayNumber % quotes.Count()];
return quote;
}
public async Task<PaginationResult<Quote>> GetFavorites(int index, int pageSize, int UserId)
{
List<Quote> quotes = _context.quotes.Where(item => item.IsValid && item.Favorite.Where(p => p.Id == UserId) != null)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite)
.Skip(index * pageSize).Take(pageSize).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<PaginationResult<Quote>> GetInvalidQuote(int index, int pageSize, int lang)
{
List<Quote> quotes = _context.quotes.Where(item => !item.IsValid && item.Langage == (LangEnum)lang)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.Skip(index * pageSize).Take(pageSize).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<PaginationResult<Quote>> GetInvalidQuote(int index, int pageSize)
{
List<Quote> quotes = _context.quotes.Where(item => !item.IsValid)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.Skip(index * pageSize).Take(pageSize).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<int> GetLastQuoteId()
{
PaginationResult<Quote> quotes = await GetAllQuote();
int lastQuoteId = 0;
foreach (Quote quote in quotes.items)
{
if (quote.Id >= lastQuoteId)
{
lastQuoteId = quote.Id + 1;
}
}
return lastQuoteId;
}
public async Task<Quote?> GetQuoteById(int id)
{
Quote? quote = _context.quotes.Where(item => item.Id == id)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.FirstOrDefault();
return quote;
}
public async Task<PaginationResult<Quote>> GetSomeQuote(int index, int pageSize)
{
List<Quote> quotes = _context.quotes.Where(item => item.IsValid)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.Skip(index * pageSize).Take(pageSize).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<PaginationResult<Quote>> GetSuggestions(int index, int pageSize, int lang) // A changer Suggestion Random
{
List<Quote> quotes = _context.quotes.Where(item => item.IsValid && item.Langage == (LangEnum)lang)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
//.OrderBy(q=> new Random() )
.Skip(index * pageSize).Take(pageSize).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<PaginationResult<Quote>> GetValidQuote(int index, int pageSize)
{
List<Quote> quotes = _context.quotes.Where(item => item.IsValid)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.Skip(index * pageSize).Take(pageSize).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<PaginationResult<Quote>> SearchByCharacter(string character, int index, int pageSize, int lang)
{
List<Quote> quotes = _context.quotes.Where(item => item.IsValid && item.Langage==(LangEnum)lang && (item.Character.Name).Contains(character))
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.Skip(index * pageSize).Take(pageSize).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<PaginationResult<Quote>> SearchByContent(string content, int index, int pageSize, int lang)
{
List<Quote> quotes = _context.quotes.Where(item => item.IsValid && item.Langage == (LangEnum)lang && (item.Content).Contains(content))
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.Skip(index * pageSize).Take(pageSize).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<PaginationResult<Quote>> SearchBySource(string source, int index, int pageSize, int lang)
{
List<Quote> quotes = _context.quotes.Where(item => item.IsValid && item.Langage == (LangEnum)lang && (item.Source.Title).Contains(source))
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images)
.Skip(index * pageSize).Take(pageSize).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task RemoveQuote(int quoteId)
{
var quote = _repo.GetById(quoteId);
if (quote == null) throw new KeyNotFoundException();
_repo.Delete( quote );
await _context.SaveChangesAsync();
}
public async Task UpdateQuote(int quoteId, Quote quote)
{
//Character
var c = await _dbC.GetCharByName(quote.Character.Name);
if (c != null)
{
quote.IdCharacter = c.Id;
quote.Character = c;
}
//Image
var i = await _dbI.GetImageByPath(quote.Character.Images.ImgPath);
if (c != null)
{
quote.Character.IdImage = i.Id;
quote.Character.Images = i;
}
//Source
var s = await _dbS.GetSourceByTitle(quote.Source.Title);
if (c != null)
{
quote.IdSource = s.Id;
quote.Source = s;
}
Quote? q = _repo.GetById(quoteId);
if (q != null)
{
bool change = false;
if (quote.IdSource != 0)
{
q.IdSource = quote.IdSource;
change = true;
}
if (quote.IdCharacter != 0)
{
q.IdCharacter = quote.IdCharacter;
change = true;
}
if (quote.IdSource != 0)
{
q.IdSource = quote.IdSource;
change = true;
}
if (quote.IdUsersPropose !=0)
{
q.IdUsersPropose = quote.IdUsersPropose;
change = true;
}
if (quote.Content != null || quote.Content == "")
{
q.Content = quote.Content;
change = true;
}
if (quote.IsValid != q.IsValid)
{
q.IsValid = quote.IsValid;
change = true;
}
if (quote.Likes != q.Likes)
{
q.Likes = quote.Likes;
change = true;
}
if (quote.Langage != q.Langage)
{
q.Langage = quote.Langage;
change = true;
}
_repo.Update(q);
if (change) _context.SaveChanges();
}
return ;
}
public async Task ValidateQuote(int quoteId, bool isValidate)
{
Quote? q = _repo.GetById(quoteId);
if (q != null)
{
q.IsValid = isValidate;
_repo.Update(q);
_context.SaveChanges();
}
return;
}
}
}