Réglage de tous les problèmes conus pour les paragraphes + merge de la branche debugLesson
continuous-integration/drone/push Build is passing Details

pull/45/head
Johnny RATTON 1 year ago
commit 69d344e947

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authorization;
using System.Net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Dto;
using Model.OrderCriteria;
@ -94,18 +95,33 @@ namespace API.Controllers
[HttpPost]
[ProducesResponseType(typeof(LessonDto), 201)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 500)]
public IActionResult CreateLesson([FromBody] LessonDto dto)
{
if (dto.Title == null || dto.LastPublisher == null)
{
return BadRequest();
}
try
{
var createdLesson = _lessonDataService.CreateLesson(dto.Id, dto.Title, dto.LastPublisher, dto.LastEdit ?? DateOnly.FromDateTime(DateTime.Now));
if (createdLesson != null)
{
_logger.LogInformation(
"[INFORMATION] Une leçon a été créé : title - {title}, lastPublisher - {publisher}, lastEdit - {lastEdit}",
dto.Title, dto.LastPublisher, dto.LastEdit);
return Created(nameof(GetLessons),
_lessonDataService.CreateLesson(dto.Title, dto.LastPublisher, dto.LastEdit));
return Created(nameof(GetLessons), createdLesson);
}
else
{
return StatusCode(500);
}
}
catch (ArgumentException e)
{
_logger.LogError("[ERREUR] " + e.Message);
return Created();
}
}
[HttpPut("lesson/{id:int}")]
@ -126,7 +142,6 @@ namespace API.Controllers
id);
return BadRequest();
}
if (LessonDto != null)
{
_logger.LogInformation("[INFORMATION] La mise à jour de la leçon avec l'id {id} a été effectuée", id);

@ -97,7 +97,7 @@ namespace API.Controllers
[ProducesResponseType(typeof(string), 400)]
public IActionResult CreateParagraph([FromBody] ParagraphDto dto)
{
if (dto.Title == null || dto.Content == null || dto.Info == null || dto.Query == null ||
if (dto.ContentTitle == null || dto.ContentContent == null || dto.Title == null || dto.Content == null || dto.Info == null || dto.Query == null ||
dto.Comment == null)
{
return BadRequest();
@ -107,7 +107,7 @@ namespace API.Controllers
"[INFORMATION] Un paragraphe a été créé : title - {title}, content - {content}, info - {info}, query - {query}, comment - {comment}",
dto.Title, dto.Content, dto.Info, dto.Query, dto.Comment);
return Created(nameof(GetParagraphs),
_paragraphDataService.CreateParagraph(dto.Title, dto.Content, dto.Info, dto.Query, dto.Comment,
_paragraphDataService.CreateParagraph(dto.ContentTitle, dto.ContentContent,dto.Title, dto.Content, dto.Info, dto.Query, dto.Comment,
dto.LessonId));
}

@ -38,11 +38,11 @@ namespace API.Controllers
var nbUser = _successDataService.GetSuccesses(page, number, orderCriteria).ToList().Count;
if (nbUser == 0)
{
_logger.LogError("[ERREUR] Aucun utilisateur trouvé.");
_logger.LogError("[ERREUR] Aucun Succès trouvé.");
return StatusCode(204);
}
_logger.LogInformation("[INFORMATION] {nb} Utilisateur(s) trouvé(s)", nbUser);
_logger.LogInformation("[INFORMATION] {nb} Succès(s) trouvé(s)", nbUser);
return Ok(_successDataService.GetSuccesses(page, number, orderCriteria));
}

@ -37,6 +37,7 @@ builder.Services.AddScoped<ILessonService<LessonDto>, LessonDataServiceApi>();
builder.Services.AddDbContext<DbContext, UserDbContext>();
builder.Services.AddDbContext<WebAPIDbContext>(options => options.UseInMemoryDatabase("appDb"));
builder.Services.AddIdentityApiEndpoints<IdentityUser>().AddEntityFrameworkStores<WebAPIDbContext>();
builder.Services.AddAuthorization();
builder.Services.AddApiVersioning(o =>
{
@ -81,6 +82,36 @@ builder.Services.AddSwaggerGen(option =>
var app = builder.Build();
// Création de l'utilisateur admin pour la base de données Identity
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var userManager = services.GetRequiredService<UserManager<IdentityUser>>();
try
{
var user = new IdentityUser { UserName = "admin@example.com", Email = "admin@example.com" };
var result = await userManager.CreateAsync(user, "Mdp_1234");
if (result.Succeeded)
{
Console.WriteLine("Utilisateur admin créé avec succès.");
}
else
{
foreach (var error in result.Errors)
{
Console.WriteLine($"Erreur lors de la création de l'utilisateur : {error.Description}");
}
}
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Une erreur s'est produite lors de la création de l'utilisateur.");
}
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{

@ -26,6 +26,6 @@ public class LessonDataServiceApi(ILessonService<LessonEntity> lessonService) :
public LessonDto UpdateLesson(int id, LessonEntity lesson) =>
lessonService.UpdateLesson(id, lesson).FromEntityToDto();
public LessonDto CreateLesson(string title, string lastPublisher, DateOnly lastEdit) =>
lessonService.CreateLesson(title, lastPublisher, lastEdit).FromEntityToDto();
public LessonDto CreateLesson(int id, string title, string lastPublisher, DateOnly lastEdit) =>
lessonService.CreateLesson(id, title, lastPublisher, lastEdit).FromEntityToDto();
}

@ -25,7 +25,6 @@ public class ParagraphDataServiceApi(IParagraphService<ParagraphEntity> paragrap
public ParagraphDto UpdateParagraph(int id, ParagraphDto paragraph) =>
paragraphService.UpdateParagraph(id, paragraph.FromDtoToEntity()).FromEntityToDto();
public ParagraphDto CreateParagraph(string title, string content, string info, string query, string comment,
int lessonId) =>
paragraphService.CreateParagraph(title, content, info, query, comment, lessonId).FromEntityToDto();
public ParagraphDto CreateParagraph(string contentTitle, string contentContent,string title, string content, string info, string query, string comment,
int lessonId) => paragraphService.CreateParagraph(contentTitle, contentContent, title, content, info, query, comment, lessonId).FromEntityToDto();
}

@ -18,6 +18,14 @@ public class InquiryDataService : IInquiryService<InquiryEntity>
public IEnumerable<InquiryEntity> GetInquiries(int page, int number, InquiryOrderCriteria orderCriteria)
{
if (page <= 0)
{
page = 1;
}
if (number <= 0)
{
number = 10;
}
IQueryable<InquiryEntity> query = DbContext.Inquiries.Skip((page - 1) * number).Take(number);
switch (orderCriteria)
{

@ -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,20 +73,25 @@ public class LessonDataService : ILessonService<LessonEntity>
public bool DeleteLesson(int id)
{
var lessonEntity = DbContext.Lessons.FirstOrDefault(l => l.Id == id);
using (UserDbContext context = new UserDbContext(new DbContextOptions<UserDbContext>()))
{
var lessonEntity = context.Lessons.FirstOrDefault(l => l.Id == id);
if (lessonEntity == null)
{
return false;
}
DbContext.Lessons.Remove(lessonEntity);
DbContext.SaveChangesAsync();
context.Lessons.Remove(lessonEntity);
context.SaveChangesAsync();
return true;
}
}
public LessonEntity UpdateLesson(int id, LessonEntity lesson)
{
var updatingLesson = DbContext.Lessons.FirstOrDefault(l => l.Id == id);
using (UserDbContext context = new UserDbContext(new DbContextOptions<UserDbContext>()))
{
var updatingLesson = context.Lessons.FirstOrDefault(l => l.Id == id);
if (updatingLesson == null)
{
throw new ArgumentException("Impossible de trouver la leçon", nameof(id));
@ -89,20 +103,34 @@ public class LessonDataService : ILessonService<LessonEntity>
pptt.SetValue(updatingLesson, pptt.GetValue(lesson));
}
DbContext.SaveChangesAsync();
context.SaveChangesAsync();
return updatingLesson;
}
public LessonEntity CreateLesson(string title, string lastPublisher, DateOnly lastEdit)
}
public LessonEntity CreateLesson(int id, string title, string lastPublisher, DateOnly lastEdit)
{
using (UserDbContext context = new UserDbContext(new DbContextOptions<UserDbContext>()))
{
DateTime date = DateTime.Now;
var newLessonEntity = new LessonEntity()
{
Title = title,
LastPublisher = lastPublisher,
LastEdit = lastEdit,
};
DbContext.Lessons.Add(newLessonEntity);
DbContext.SaveChangesAsync();
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;
}
}
}

@ -18,6 +18,14 @@ public class ParagraphDataService : IParagraphService<ParagraphEntity>
public IEnumerable<ParagraphEntity> GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria)
{
if (page <= 0)
{
page = 1;
}
if (number <= 0)
{
number = 10;
}
IQueryable<ParagraphEntity> query = DbContext.Paragraphs.Skip((page - 1) * number).Take(number);
switch (orderCriteria)
{
@ -94,16 +102,25 @@ public class ParagraphDataService : IParagraphService<ParagraphEntity>
{
pptt.SetValue(updatingParagraph, pptt.GetValue(paragraph));
}
/*updatingParagraph.ContentTitle = paragraph.ContentTitle;
updatingParagraph.ContentContent = paragraph.ContentContent;
updatingParagraph.Title = paragraph.Title;
updatingParagraph.Content = paragraph.Content;
updatingParagraph.Info = paragraph.Info;
updatingParagraph.Query = paragraph.Query;
updatingParagraph.Comment = paragraph.Comment;
updatingParagraph.LessonId = paragraph.LessonId;*/
DbContext.SaveChangesAsync();
return updatingParagraph;
}
public ParagraphEntity CreateParagraph(string title, string content, string info, string query, string comment,
public ParagraphEntity CreateParagraph(string contentTitle, string contentContent, string title, string content, string info, string query, string comment,
int lessonId)
{
var newParagraphEntity = new ParagraphEntity()
{
ContentContent = contentContent,
ContentTitle = contentTitle,
Title = title,
Content = content,
Info = info,

@ -18,6 +18,14 @@ public class SuccessDataService : ISuccessService<SuccessEntity>
public IEnumerable<SuccessEntity> GetSuccesses(int page, int number, SuccessOrderCriteria orderCriteria)
{
if (page <= 0)
{
page = 1;
}
if (number <= 0)
{
number = 10;
}
IQueryable<SuccessEntity> query = DbContext.Successes.Skip((page - 1) * number).Take(number);
switch (orderCriteria)
{

@ -39,6 +39,14 @@ public class UserDataService : IUserService<UserEntity>
public IEnumerable<UserEntity> GetUsers(int page, int number, UserOrderCriteria orderCriteria)
{
if (page <= 0)
{
page = 1;
}
if (number <= 0)
{
number = 10;
}
IQueryable<UserEntity> query = DbContext.Users.Skip((page - 1) * number).Take(number);
switch (orderCriteria)
{

@ -1,13 +1,21 @@
namespace Dto;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
public abstract class ContentLessonDto
namespace Dto;
[DataContract]
public class ContentLessonDto
{
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "contentContent")]
public string ContentContent { get; set; }
[DataMember(Name = "contentTitle")]
public string ContentTitle { get; set; }
[DataMember(Name = "lessonId")]
public int LessonId { get; set; }
protected ContentLessonDto(int id, string contentContent, string contentTitle, int lessonId)
public ContentLessonDto(int id, string contentContent, string contentTitle, int lessonId)
{
Id = id;
ContentContent = contentContent;
@ -22,7 +30,7 @@ public abstract class ContentLessonDto
LessonId = lessonId;
}
protected ContentLessonDto()
public ContentLessonDto()
{
}
}

@ -6,4 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\..\..\.nuget\packages\newtonsoft.json\13.0.1\lib\netstandard2.0\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

@ -1,13 +1,20 @@
namespace Dto;
using System.Runtime.Serialization;
namespace Dto;
[DataContract]
public class InquiryDto : IEquatable<InquiryDto>
{
public int Id { get; }
[DataMember]
public int Id { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public bool IsUser { get; set; }
public InquiryDto()

@ -1,17 +1,25 @@
namespace Dto;
using System.Net;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
namespace Dto;
[DataContract]
public class LessonDto : IEquatable<LessonDto>
{
public int Id { get; }
public string Title { get; set; }
public string LastPublisher { get; set; }
public DateOnly LastEdit { get; set; }
[DataMember]
public int Id { get; set; }
[DataMember]
public string? Title { get; set; }
[DataMember]
public string? LastPublisher { get; set; }
[DataMember]
public DateOnly? LastEdit { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ICollection<ContentLessonDto> Content { get; set; } = new List<ContentLessonDto>();
public LessonDto()
{
}
public LessonDto(int id, string title, string lastPublisher, DateOnly lastEdit)
{
Id = id;
@ -20,6 +28,14 @@ public class LessonDto : IEquatable<LessonDto>
LastEdit = lastEdit;
}
public LessonDto(int id, string title, string lastPublisher, DateOnly? lastEdit, ICollection<ContentLessonDto> content)
{
Id = id;
Title = title;
LastPublisher = lastPublisher;
LastEdit = lastEdit;
Content = content;
}
public LessonDto(string title, string lastPublisher, DateOnly lastEdit)
{
Title = title;

@ -1,13 +1,31 @@
namespace Dto;
using System.Runtime.Serialization;
namespace Dto;
[DataContract]
public class ParagraphDto : ContentLessonDto, IEquatable<ParagraphDto>
{
[DataMember(Name = "title")]
public string Title { get; set; }
[DataMember(Name = "content")]
public string Content { get; set; }
[DataMember(Name = "info")]
public string Info { get; set; }
[DataMember(Name = "query")]
public string Query { get; set; }
[DataMember(Name = "comment")]
public string Comment { get; set; }
public ParagraphDto(string contentTitle, string contentContent, string title, string content, string info, string query, string comment, int lessonId) :
base(contentContent,
contentTitle, lessonId)
{
Title = title;
Content = content;
Info = info;
Query = query;
Comment = comment;
}
public ParagraphDto(string title, string content, string info, string query, string comment, int lessonId) :
base(content,
title, lessonId)

@ -9,5 +9,5 @@ public interface ILessonService<TLesson>
public TLesson GetLessonByTitle(string title);
public bool DeleteLesson(int id);
public TLesson UpdateLesson(int id, TLesson lesson);
public TLesson CreateLesson(string title, string lastPublisher, DateOnly lastEdit);
public TLesson CreateLesson(int id, string title, string lastPublisher, DateOnly lastEdit);
}

@ -10,7 +10,7 @@ namespace Shared
public bool DeleteParagraph(int id);
public TParagraph UpdateParagraph(int id, TParagraph paragraph);
public TParagraph CreateParagraph(string title, string content, string info, string query, string comment,
public TParagraph CreateParagraph(string contentTitle, string contentContent, string title, string content, string info, string query, string comment,
int lessonId);
}
}

@ -0,0 +1,13 @@
using Dto;
using Entities;
using Model;
namespace Shared.Mapper;
public static class ContentLessonMapper
{
public static ContentLessonDto FromEntityToDto(this ContentLessonEntity entity)
{
return new ContentLessonDto(entity.Id, entity.ContentContent, entity.ContentTitle, entity.LessonId);
}
}

@ -12,6 +12,11 @@ public static class LessonMapper
}
public static LessonDto FromEntityToDto(this LessonEntity model)
{
return new LessonDto(model.Id, model.Title, model.LastPublisher, model.LastEdit, model.Content.Select(c => c.FromEntityToDto()).ToList());
}
public static LessonDto FromEntityToDTOPost(this LessonEntity model)
{
return new LessonDto(model.Id, model.Title, model.LastPublisher, model.LastEdit);
}
@ -26,21 +31,22 @@ public static class LessonMapper
LastEdit = model.LastEdit
};
}
public static LessonEntity FromDtoToEntity(this LessonDto dto)
{
DateTime date = DateTime.Now;
return new LessonEntity
{
Id = dto.Id,
Title = dto.Title,
LastPublisher = dto.LastPublisher,
LastEdit = dto.LastEdit
LastEdit = dto.LastEdit ?? new DateOnly(date.Year,date.Month,date.Day)
};
}
public static Lesson FromDtoToModel(this LessonDto dto)
{
return new Lesson(dto.Id, dto.Title, dto.LastPublisher, dto.LastEdit);
DateTime date = DateTime.Now;
return new Lesson(dto.Id, dto.Title, dto.LastPublisher, dto.LastEdit ?? new DateOnly(date.Year,date.Month,date.Day));
}
public static Lesson FromEntityToModel(this LessonEntity entity)

@ -35,6 +35,8 @@ public static class ParagraphMapper
Id = dto.Id,
ContentTitle = dto.ContentTitle,
ContentContent = dto.ContentContent,
Title = dto.Title,
Content = dto.Content,
Info = dto.Info,
Query = dto.Query,
Comment = dto.Comment,

@ -179,7 +179,7 @@ public class LessonUnitTest
[Fact]
public void CreateLessonSuccess()
{
_lessonService.Setup(x => x.CreateLesson("Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)))
_lessonService.Setup(x => x.CreateLesson(96,"Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)))
.Returns(new LessonDto("Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)));
var lessonsController = new LessonsController(_lessonService.Object, new NullLogger<LessonsController>());
@ -200,7 +200,7 @@ public class LessonUnitTest
[Fact]
public void CreateLessonFail()
{
_lessonService.Setup(x => x.CreateLesson("Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)))
_lessonService.Setup(x => x.CreateLesson(42,"Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)))
.Returns(new LessonDto("Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)));
var lessonsController = new LessonsController(_lessonService.Object, new NullLogger<LessonsController>());

@ -193,21 +193,22 @@ public class ParagraphsUnitTest
[Fact]
public void CreateParagraphSuccess()
{
_paragraphService.Setup(x => x.CreateParagraph("Le nouveau titre", "Le nouveau content", "Les infos",
_paragraphService.Setup(x => x.CreateParagraph("Le nouveau titre", "Le nouveau content", "Title","Le nouveau content","Les infos",
"La requête requêtante", "Commentaires", 2))
.Returns(new ParagraphDto("Le nouveau titre", "Le nouveau content", "Les infos", "La requête requêtante",
.Returns(new ParagraphDto("Le nouveau titre", "Le nouveau content","Title", "Le nouveau content", "Les infos", "La requête requêtante",
"Commentaires", 2));
var paragraphsController =
new ParagraphsController(_paragraphService.Object, new NullLogger<ParagraphsController>());
var paragraphsResult = paragraphsController.CreateParagraph(new ParagraphDto("Le nouveau titre",
var paragraphsResult = paragraphsController.CreateParagraph(new ParagraphDto("Le nouveau titre", "Le nouveau content","Title",
"Le nouveau content", "Les infos", "La requête requêtante", "Commentaires", 2));
if (paragraphsResult is CreatedResult createdObjectResult)
{
ParagraphDto valeur = createdObjectResult.Value as ParagraphDto;
Assert.NotNull(valeur);
Assert.Equal("Le nouveau titre", valeur.Title);
Assert.Equal("Title", valeur.Title);
Assert.Equal("Le nouveau titre", valeur.ContentTitle);
Assert.Equal("Le nouveau content", valeur.ContentContent);
Assert.Equal("Le nouveau content", valeur.Content);
Assert.Equal("Les infos", valeur.Info);
Assert.Equal("La requête requêtante", valeur.Query);
@ -219,8 +220,8 @@ public class ParagraphsUnitTest
[Fact]
public void CreateParagraphFail()
{
_paragraphService.Setup(x => x.CreateParagraph("Le nouveau titre", "Le nouveau content", "Les infos",
"La requête requêtante", "Commentaires", 2))
_paragraphService.Setup(x => x.CreateParagraph("Le nouveau titre", "Le nouveau content", "Title","Les infos",
"La requête requêtante", "Commentaires", "Comment", 2))
.Returns(new ParagraphDto("Le nouveau titre", "Le nouveau content", "Les infos", "La requête requêtante",
"Commentaires", 2));
var paragraphsController =

Loading…
Cancel
Save