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

165 lines
5.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Shared;
namespace Contextlib
{
public class DbQuoteManager : IQuoteService<Quote>
{
private WTFContext _context;
private GenericRepository<Quote> _repo;
public DbQuoteManager(WTFContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
_repo = new GenericRepository<Quote>(_context);
}
public async Task AddQuote(Quote quote)
{
if (quote == null)
{
throw new ArgumentNullException(nameof(quote), "quote cannot be null.");
}
_repo.Insert(quote);
await _context.SaveChangesAsync();
}
public async Task<PaginationResult<Quote>> GetAllQuote()
{
List<Quote> quotes = _repo.GetItems(item=> item.IsValid,
0, _repo.Count(), []
).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 = _repo.GetItems(item => item.IsValid && item.Langage==(LangEnum)lang ,
index, pageSize, []
).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<Quote> GetDailyQuote(DateOnly date, int lang)
{
List<Quote> quotes = _repo.GetItems(item => item.IsValid && item.Langage == (LangEnum)lang,
0, _repo.Count(), []
).ToList();
Quote quote = _repo.GetById(date.DayNumber % quotes.Count()) ?? quotes.First();
return quote;
}
public async Task<PaginationResult<Quote>> GetFavorites(int index, int pageSize, int UserId)
{
List<Quote> quotes = _repo.GetItems(item => item.IsValid && item.Favorite.Where(p=>p.Id==UserId)!=null ,
index, pageSize, [nameof(Quote.Favorite)]
).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 = _repo.GetItems(item => !item.IsValid && item.Langage == (LangEnum)lang,
index, pageSize, []
).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<PaginationResult<Quote>> GetInvalidQuote(int index, int pageSize)
{
List<Quote> quotes = _repo.GetItems(item => !item.IsValid,
index, pageSize, []
).ToList();
return new PaginationResult<Quote>(quotes.Count, index, pageSize, quotes);
}
public async Task<int> GetLastQuoteId()
{
PaginationResult<Quote> users = await GetAllQuote();
int lastQuoteId = 0;
foreach (Quote quote in users.items)
{
if (quote.Id >= lastQuoteId)
{
lastQuoteId = quote.Id + 1;
}
}
return lastQuoteId;
}
public async Task<Quote> GetQuoteById(int id)
{
Quote? quote = _repo.GetById(id, item => item.Id == id, []);
if (quote == null)
{
throw new KeyNotFoundException($"Error : No quotes found with the ID: {id}.");
}
return quote;
}
public async Task<PaginationResult<Quote>> GetSomeQuote(int index, int pageSize)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Quote>> GetSuggestions(int index, int pageSize, int lang)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Quote>> GetValidQuote(int index, int pageSize)
{
throw new NotImplementedException();
}
public async Task RemoveQuote(int quoteId)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Quote>> SearchByCharacter(string character, int index, int pageSize, int lang)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Quote>> SearchByContent(string content, int index, int pageSize, int lang)
{
throw new NotImplementedException();
}
public async Task<PaginationResult<Quote>> SearchBySource(string source, int index, int pageSize, int lang)
{
throw new NotImplementedException();
}
public async Task UpdateQuote(int quoteId, Quote quote)
{
throw new NotImplementedException();
}
public async Task ValidateQuote(int quoteId, bool isValidate)
{
throw new NotImplementedException();
}
}
}