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.
72 lines
2.5 KiB
72 lines
2.5 KiB
using DbConnectionLibrairie;
|
|
using Entities;
|
|
using ManagerInterfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Model;
|
|
using OrderCriterias;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EntityManagers
|
|
{
|
|
public class ChapterEntityManager(MyDbContext dbContext) : IChapterManager<ChapterEntity>
|
|
{
|
|
MyDbContext dbContext = dbContext;
|
|
|
|
public Task<ChapterEntity> addChapter(ChapterEntity chapter)
|
|
{
|
|
dbContext.Chapters.Add(chapter);
|
|
dbContext.SaveChangesAsync();
|
|
return dbContext.Chapters.Where(a => a.Equals(chapter)).FirstAsync();
|
|
}
|
|
|
|
public Task<ChapterEntity?> getChapter(uint id)
|
|
{
|
|
return Task.FromResult<ChapterEntity?>(dbContext.Chapters.Where(c => c.Id == id).FirstOrDefault());
|
|
}
|
|
|
|
public Task<(int nbPages, IEnumerable<ChapterEntity>? chapters)> getChapters(int nb, int count, ChapterOrderCriteria orderCriteria = ChapterOrderCriteria.ById)
|
|
{
|
|
int nbEl = getNbElements();
|
|
if (nb > nbEl / count) return Task.FromResult<(int nbPages, IEnumerable<ChapterEntity>? chapters)>((nbEl / count, null));
|
|
var tmp = dbContext.Chapters;
|
|
switch (orderCriteria)
|
|
{
|
|
case ChapterOrderCriteria.ById:
|
|
tmp.OrderBy(a => a.Id);
|
|
break;
|
|
case ChapterOrderCriteria.ByName:
|
|
tmp.OrderBy(a => a.Name);
|
|
break;
|
|
}
|
|
return Task.FromResult<(int nbPages, IEnumerable<ChapterEntity>? chapters)>((nbEl / count, tmp.Skip((nb - 1) * count).Take(count)));
|
|
}
|
|
|
|
public int getNbElements()
|
|
{
|
|
return dbContext.Chapters.CountAsync().Result;
|
|
}
|
|
|
|
public Task<ChapterEntity?> removeChapter(ChapterEntity chapter)
|
|
{
|
|
var tmp = dbContext.Chapters.Where(a => a.Equals(chapter)).FirstOrDefaultAsync().Result;
|
|
if (tmp == null) return Task.FromResult<ChapterEntity?>(tmp);
|
|
dbContext.Chapters.Remove(tmp);
|
|
dbContext.SaveChanges();
|
|
return Task.FromResult<ChapterEntity?>(tmp);
|
|
}
|
|
|
|
public Task<ChapterEntity?> removeChapter(uint id)
|
|
{
|
|
var tmp = getChapter(id).Result;
|
|
if (tmp == null) return Task.FromResult<ChapterEntity?>(tmp);
|
|
dbContext.Chapters.Remove(tmp);
|
|
dbContext.SaveChanges();
|
|
return Task.FromResult<ChapterEntity?>(tmp);
|
|
}
|
|
}
|
|
}
|