|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
using DbContextLib;
|
|
|
|
|
using Entities;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Model.OrderCriteria;
|
|
|
|
|
using Shared;
|
|
|
|
@ -18,6 +19,14 @@ public class LessonDataService : ILessonService<LessonEntity>
|
|
|
|
|
|
|
|
|
|
public IEnumerable<LessonEntity> GetLessons(int page, int number, LessonOrderCriteria orderCriteria)
|
|
|
|
|
{
|
|
|
|
|
if (page <= 0)
|
|
|
|
|
{
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
|
|
|
|
if (number <= 0)
|
|
|
|
|
{
|
|
|
|
|
number = 10;
|
|
|
|
|
}
|
|
|
|
|
IQueryable<LessonEntity> query = DbContext.Lessons.Skip((page - 1) * number).Take(number);
|
|
|
|
|
switch (orderCriteria)
|
|
|
|
|
{
|
|
|
|
@ -64,45 +73,64 @@ public class LessonDataService : ILessonService<LessonEntity>
|
|
|
|
|
|
|
|
|
|
public bool DeleteLesson(int id)
|
|
|
|
|
{
|
|
|
|
|
var lessonEntity = DbContext.Lessons.FirstOrDefault(l => l.Id == id);
|
|
|
|
|
if (lessonEntity == null)
|
|
|
|
|
using (UserDbContext context = new UserDbContext(new DbContextOptions<UserDbContext>()))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
var lessonEntity = context.Lessons.FirstOrDefault(l => l.Id == id);
|
|
|
|
|
if (lessonEntity == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DbContext.Lessons.Remove(lessonEntity);
|
|
|
|
|
DbContext.SaveChangesAsync();
|
|
|
|
|
return true;
|
|
|
|
|
context.Lessons.Remove(lessonEntity);
|
|
|
|
|
context.SaveChangesAsync();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LessonEntity UpdateLesson(int id, LessonEntity lesson)
|
|
|
|
|
{
|
|
|
|
|
var updatingLesson = DbContext.Lessons.FirstOrDefault(l => l.Id == id);
|
|
|
|
|
if (updatingLesson == null)
|
|
|
|
|
using (UserDbContext context = new UserDbContext(new DbContextOptions<UserDbContext>()))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Impossible de trouver la leçon", nameof(id));
|
|
|
|
|
}
|
|
|
|
|
var updatingLesson = context.Lessons.FirstOrDefault(l => l.Id == id);
|
|
|
|
|
if (updatingLesson == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Impossible de trouver la leçon", nameof(id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var pptt in typeof(LessonEntity).GetProperties()
|
|
|
|
|
.Where(p => p.CanWrite && p.Name != nameof(LessonEntity.Id)))
|
|
|
|
|
{
|
|
|
|
|
pptt.SetValue(updatingLesson, pptt.GetValue(lesson));
|
|
|
|
|
}
|
|
|
|
|
foreach (var pptt in typeof(LessonEntity).GetProperties()
|
|
|
|
|
.Where(p => p.CanWrite && p.Name != nameof(LessonEntity.Id)))
|
|
|
|
|
{
|
|
|
|
|
pptt.SetValue(updatingLesson, pptt.GetValue(lesson));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DbContext.SaveChangesAsync();
|
|
|
|
|
return updatingLesson;
|
|
|
|
|
context.SaveChangesAsync();
|
|
|
|
|
return updatingLesson;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LessonEntity CreateLesson(string title, string lastPublisher, DateOnly lastEdit)
|
|
|
|
|
public LessonEntity CreateLesson(int id, string title, string lastPublisher, DateOnly lastEdit)
|
|
|
|
|
{
|
|
|
|
|
var newLessonEntity = new LessonEntity()
|
|
|
|
|
using (UserDbContext context = new UserDbContext(new DbContextOptions<UserDbContext>()))
|
|
|
|
|
{
|
|
|
|
|
Title = title,
|
|
|
|
|
LastPublisher = lastPublisher,
|
|
|
|
|
LastEdit = lastEdit,
|
|
|
|
|
};
|
|
|
|
|
DbContext.Lessons.Add(newLessonEntity);
|
|
|
|
|
DbContext.SaveChangesAsync();
|
|
|
|
|
return newLessonEntity;
|
|
|
|
|
DateTime date = DateTime.Now;
|
|
|
|
|
var newLessonEntity = new LessonEntity()
|
|
|
|
|
{
|
|
|
|
|
Title = title,
|
|
|
|
|
LastPublisher = lastPublisher,
|
|
|
|
|
LastEdit = lastEdit,
|
|
|
|
|
};
|
|
|
|
|
var lesson = context.Lessons.FirstOrDefault(l => l.Id == id);
|
|
|
|
|
if (lesson == null && id > 0)
|
|
|
|
|
{
|
|
|
|
|
newLessonEntity.Id = id;
|
|
|
|
|
context.Lessons.Add(newLessonEntity);
|
|
|
|
|
context.SaveChangesAsync();
|
|
|
|
|
throw new ArgumentException(
|
|
|
|
|
$"Erreur, l'ID {id} est déjà utilisé pour une autre leçon, un id par a été attribué.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Lessons.Add(newLessonEntity);
|
|
|
|
|
context.SaveChangesAsync();
|
|
|
|
|
return newLessonEntity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|