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.
API_SQLuedo/API_SQLuedo/Shared/ParagraphDataService.cs

116 lines
3.7 KiB

using Entities;
using Microsoft.EntityFrameworkCore;
using Dto;
using DbContextLib;
using Model.OrderCriteria;
using Shared;
using Shared.Mapper;
namespace Shared;
public class ParagraphDataService : IParagraphDataService
{
private UserDbContext DbContext { get; set; }
public ParagraphDataService(UserDbContext context)
{
DbContext = context;
context.Database.EnsureCreated();
}
public IEnumerable<ParagraphDTO> GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria)
{
IQueryable<ParagraphEntity> query = DbContext.Paragraphs.Skip((page - 1) * number).Take(number);
switch (orderCriteria)
{
case ParagraphOrderCriteria.None:
break;
case ParagraphOrderCriteria.ByTitle:
query = query.OrderBy(s => s.Title);
break;
case ParagraphOrderCriteria.ByContent:
query = query.OrderBy(s => s.Content);
break;
case ParagraphOrderCriteria.ByInfo:
query = query.OrderBy(s => s.Info);
break;
case ParagraphOrderCriteria.ByQuery:
query = query.OrderBy(s => s.Query);
break;
case ParagraphOrderCriteria.ByComment:
query = query.OrderBy(s => s.Comment);
break;
default:
break;
}
var paragraphs = query.ToList();
return paragraphs.Select(s => s.FromEntityToDTO());
}
public ParagraphDTO GetParagraphById(int id)
{
var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Id == id);
if (paragraphEntity == null)
{
throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id));
}
return paragraphEntity.FromEntityToDTO();
}
public ParagraphDTO GetParagraphByTitle(string title)
{
var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Title == title);
if (paragraphEntity == null)
{
throw new ArgumentException("Impossible de trouver le paragraphe", nameof(title));
}
return paragraphEntity.FromEntityToDTO();
}
public bool DeleteParagraph(int id)
{
var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id);
if (paragraphEntity == null)
{
return false;
}
DbContext.Paragraphs.Remove(paragraphEntity);
DbContext.SaveChangesAsync();
return true;
}
public ParagraphDTO UpdateParagraph(int id, ParagraphDTO paragraphDTO)
{
var updatingParagraph = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id);
if (updatingParagraph == null)
{
throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id));
}
updatingParagraph.Title = paragraphDTO.Title;
updatingParagraph.Content = paragraphDTO.Content;
updatingParagraph.Info = paragraphDTO.Info;
updatingParagraph.Query = paragraphDTO.Query;
updatingParagraph.Comment = paragraphDTO.Comment;
DbContext.SaveChangesAsync();
return updatingParagraph.FromEntityToDTO();
}
public ParagraphDTO CreateParagraph(string title, string content, string info, string query, string comment, int lessonId)
{
var newParagraphEntity = new ParagraphDTO()
{
Title = title,
Content = content,
Info = info,
Query = query,
Comment = comment,
LessonId = lessonId
};
DbContext.Paragraphs.Add(newParagraphEntity.FromDTOToEntity());
DbContext.SaveChangesAsync();
return newParagraphEntity;
}
}