Modification du service de Paragraph

pull/37/head
Clement CHIEU 1 year ago
parent b4722228bf
commit 4fe9d58275

@ -13,7 +13,7 @@ namespace API.Controllers
[ApiController]
public class LessonsController : Controller
{
private ILessonService<LessonDTO> _lessonDataService;
private readonly ILessonService<LessonDTO> _lessonDataService;
private readonly ILogger<LessonsController> _logger;

@ -13,7 +13,7 @@ namespace API.Controllers
[ApiController]
public class ParagraphsController : Controller
{
private IParagraphService<ParagraphDTO> _paragraphDataService;
private readonly IParagraphService<ParagraphDTO> _paragraphDataService;
private readonly ILogger<ParagraphsController> _logger;

@ -13,7 +13,7 @@ namespace API.Controllers
[ApiController]
public class SuccessesController : Controller
{
private ISuccessService<SuccessDTO> _successDataService;
private readonly ISuccessService<SuccessDTO> _successDataService;
private readonly ILogger<SuccessesController> _logger;

@ -6,9 +6,7 @@ using DbDataManager.Service;
using Dto;
using Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Shared;
@ -27,8 +25,8 @@ builder.Services.AddScoped<IUserService<UserDTO>, Shared.UserDataService>();
builder.Services.AddScoped<IInquiryService<InquiryEntity>, InquiryDataService>();
builder.Services.AddScoped<IInquiryService<InquiryDTO>, InquiryDataServiceApi>();
builder.Services.AddScoped<IParagraphService<ParagraphEntity>, DbDataManager.Service.ParagraphDataService>();
builder.Services.AddScoped<IParagraphService<ParagraphDTO>, Shared.ParagraphDataService>();
builder.Services.AddScoped<IParagraphService<ParagraphEntity>, ParagraphDataService>();
builder.Services.AddScoped<IParagraphService<ParagraphDTO>, ParagraphDataServiceApi>();
builder.Services.AddScoped<ISuccessService<SuccessEntity>, DbDataManager.Service.SuccessDataService>();
builder.Services.AddScoped<ISuccessService<SuccessDTO>, Shared.SuccessDataService>();

@ -1,26 +1,28 @@
using Entities;
using Dto;
using Entities;
using Model.OrderCriteria;
using Shared;
using Shared.Mapper;
namespace API.Service;
public class ParagraphDataServiceApi(IParagraphService<ParagraphEntity> paragraphService) : IParagraphService<ParagraphEntity>
public class ParagraphDataServiceApi(IParagraphService<ParagraphEntity> paragraphService) : IParagraphService<ParagraphDTO>
{
public IEnumerable<ParagraphEntity> GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria)
public IEnumerable<ParagraphDTO> GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria)
{
var paragraphsEntities = paragraphService.GetParagraphs(page, number, orderCriteria);
return paragraphsEntities.Select(e => e).ToList();
return paragraphsEntities.Select(e => e.FromEntityToDTO()).ToList();
}
public ParagraphEntity GetParagraphById(int id) => paragraphService.GetParagraphById(id);
public ParagraphDTO GetParagraphById(int id) => paragraphService.GetParagraphById(id).FromEntityToDTO();
public ParagraphEntity GetParagraphByTitle(string title) => paragraphService.GetParagraphByTitle(title);
public ParagraphDTO GetParagraphByTitle(string title) => paragraphService.GetParagraphByTitle(title).FromEntityToDTO();
public bool DeleteParagraph(int id) => paragraphService.DeleteParagraph(id);
public ParagraphEntity UpdateParagraph(int id, ParagraphEntity paragraph) =>
paragraphService.UpdateParagraph(id, paragraph);
public ParagraphDTO UpdateParagraph(int id, ParagraphDTO paragraph) =>
paragraphService.UpdateParagraph(id, paragraph.FromDTOToEntity()).FromEntityToDTO();
public ParagraphEntity CreateParagraph(string title, string content, string info, string query, string comment, int lessonId) =>
paragraphService.CreateParagraph(title, content, info, query, comment, lessonId);
public ParagraphDTO CreateParagraph(string title, string content, string info, string query, string comment, int lessonId) =>
paragraphService.CreateParagraph(title, content, info, query, comment, lessonId).FromEntityToDTO();
}

@ -16,6 +16,36 @@ public class ParagraphDataService : IParagraphService<ParagraphEntity>
context.Database.EnsureCreated();
}
public IEnumerable<ParagraphEntity> 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);
}
public ParagraphEntity GetParagraphById(int id)
{
var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Id == id);
@ -38,15 +68,9 @@ public class ParagraphDataService : IParagraphService<ParagraphEntity>
return paragraphEntity;
}
public IEnumerable<ParagraphEntity> GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria)
{
return DbContext.Paragraphs.Skip((page - 1) * number).Take(number).ToList()
.Select(u => u);
}
public bool DeleteParagraph(int id)
{
var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Id == id);
var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id);
if (paragraphEntity == null)
{
return false;
@ -57,27 +81,25 @@ public class ParagraphDataService : IParagraphService<ParagraphEntity>
return true;
}
public ParagraphEntity UpdateParagraph(int id, ParagraphEntity paragraph)
public ParagraphEntity UpdateParagraph(int id, ParagraphEntity paragraphDTO)
{
var updatingParagraph = DbContext.Paragraphs.FirstOrDefault(u => u.Id == id);
var updatingParagraph = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id);
if (updatingParagraph == null)
{
throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id));
}
updatingParagraph.Title = paragraph.Title;
updatingParagraph.Content = paragraph.Content;
updatingParagraph.Info = paragraph.Info;
updatingParagraph.Query = paragraph.Query;
updatingParagraph.Comment = paragraph.Comment;
updatingParagraph.LessonId = paragraph.LessonId;
// Permet d'indiquer en Db que l'entité a été modifiée.
DbContext.Entry(updatingParagraph).State = EntityState.Modified;
updatingParagraph.Title = paragraphDTO.Title;
updatingParagraph.Content = paragraphDTO.Content;
updatingParagraph.Info = paragraphDTO.Info;
updatingParagraph.Query = paragraphDTO.Query;
updatingParagraph.Comment = paragraphDTO.Comment;
DbContext.SaveChangesAsync();
return updatingParagraph;
}
public ParagraphEntity CreateParagraph(string title, string content, string info, string query, string comment, int lessonId)
public ParagraphEntity CreateParagraph(string title, string content, string info, string query, string comment,
int lessonId)
{
var newParagraphEntity = new ParagraphEntity()
{

@ -1,116 +0,0 @@
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;
}
}

@ -10,8 +10,8 @@ using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Model.OrderCriteria;
using ParagraphDataService = Shared.ParagraphDataService;
using Service_LessonDataService = DbDataManager.Service.LessonDataService;
using Service_ParagraphDataService = DbDataManager.Service.ParagraphDataService;
using SuccessDataService = Shared.SuccessDataService;
using UserDataService = Shared.UserDataService;
@ -33,7 +33,7 @@ using (var context = new UserDbContext(options))
var userController = new UsersController(userLogger, new UserDataService(context));
var inquiryController =
new InquiriesController(new InquiryDataServiceApi(new InquiryDataService(context)), inquiryLogger);
var paragraphController = new ParagraphsController(new ParagraphDataService(context), paragraphLogger);
var paragraphController = new ParagraphsController(new ParagraphDataServiceApi(new Service_ParagraphDataService(context)), paragraphLogger);
var lessonController =
new LessonsController(new LessonDataServiceApi(new Service_LessonDataService(context)), lessonLogger);
var successController = new SuccessesController(new SuccessDataService(context), successLogger);

Loading…
Cancel
Save