From b22e31d3523a6ada3f7f07e68438087b6cfb18c8 Mon Sep 17 00:00:00 2001 From: Nestisse Date: Sat, 16 Mar 2024 10:31:56 +0100 Subject: [PATCH] Modification du constructeur de paragrapheEntity --- .../EntityFramework/ContentLessonEntity.cs | 26 -------- .../EntityFramework/ParagraphEntity.cs | 24 ------- API_SQLuedo/Shared/Mapper/ParagraphMapper.cs | 27 ++++++-- API_SQLuedo/Shared/ParagraphDataService.cs | 10 +-- .../ConcreteContentLessonEntity.cs | 21 ------- .../EntitiesTests/TestContentLessonEntity.cs | 55 ---------------- .../EntitiesTests/TestParagraphEntity.cs | 62 ++++++++++++------- 7 files changed, 65 insertions(+), 160 deletions(-) delete mode 100644 API_SQLuedo/TestEF/EntitiesTests/ConcreteContentLessonEntity.cs delete mode 100644 API_SQLuedo/TestEF/EntitiesTests/TestContentLessonEntity.cs diff --git a/API_SQLuedo/EntityFramework/ContentLessonEntity.cs b/API_SQLuedo/EntityFramework/ContentLessonEntity.cs index ee81947..7717d35 100644 --- a/API_SQLuedo/EntityFramework/ContentLessonEntity.cs +++ b/API_SQLuedo/EntityFramework/ContentLessonEntity.cs @@ -10,30 +10,4 @@ public abstract class ContentLessonEntity public string ContentTitle { get; set; } [ForeignKey(nameof(Lesson))] public int LessonId { get; set; } public LessonEntity Lesson { get; set; } = null!; - - protected ContentLessonEntity() - { - } - - protected ContentLessonEntity(int id, string contentContent, string contentTitle) - { - Id = id; - ContentContent = contentContent; - ContentTitle = contentTitle; - } - - - protected ContentLessonEntity(int id, int lessonId, string contentContent, string contentTitle) - { - Id = id; - LessonId = lessonId; - ContentContent = contentContent; - ContentTitle = contentTitle; - } - - protected ContentLessonEntity(string contentContent, string contentTitle) - { - ContentContent = contentContent; - ContentTitle = contentTitle; - } } \ No newline at end of file diff --git a/API_SQLuedo/EntityFramework/ParagraphEntity.cs b/API_SQLuedo/EntityFramework/ParagraphEntity.cs index b7ab14e..6b4a8c2 100644 --- a/API_SQLuedo/EntityFramework/ParagraphEntity.cs +++ b/API_SQLuedo/EntityFramework/ParagraphEntity.cs @@ -14,28 +14,4 @@ public class ParagraphEntity : ContentLessonEntity public string Info { get; set; } public string Query { get; set; } public string Comment { get; set; } - - public ParagraphEntity() - { - } - - public ParagraphEntity(int id, string title, string content, string info, string query, - string comment, int idLesson) : base(id, idLesson, content, title) - { - Title = title; - Content = content; - Info = info; - Query = query; - Comment = comment; - } - - public ParagraphEntity(string title, string content, string info, string query, string comment) : base(content, - title) - { - Title = title; - Content = content; - Info = info; - Query = query; - Comment = comment; - } } \ No newline at end of file diff --git a/API_SQLuedo/Shared/Mapper/ParagraphMapper.cs b/API_SQLuedo/Shared/Mapper/ParagraphMapper.cs index 3d029e5..4d76665 100644 --- a/API_SQLuedo/Shared/Mapper/ParagraphMapper.cs +++ b/API_SQLuedo/Shared/Mapper/ParagraphMapper.cs @@ -16,7 +16,7 @@ public static class ParagraphMapper return new Paragraph(model.ContentTitle, model.ContentContent, model.Info, model.Query, model.Comment); } - public static ParagraphDTO FromEntityToDTO(this ParagraphEntity model) + public static ParagraphDTO FromEntityToDto(this ParagraphEntity model) { return new ParagraphDTO(model.Id, model.ContentTitle, model.ContentContent, model.Info, model.Query, model.Comment, model.LessonId); @@ -28,15 +28,30 @@ public static class ParagraphMapper model.Comment, model.LessonId); } - public static ParagraphEntity FromDTOToEntity(this ParagraphDTO dto) + public static ParagraphEntity FromDtoToEntity(this ParagraphDTO dto) { - return new ParagraphEntity(dto.Id, dto.ContentTitle, dto.ContentContent, dto.Info, dto.Query, dto.Comment, - dto.LessonId); + return new ParagraphEntity + { + Id = dto.Id, + ContentTitle = dto.ContentTitle, + ContentContent = dto.ContentContent, + Info = dto.Info, + Query = dto.Query, + Comment = dto.Comment, + LessonId = dto.LessonId + }; } public static ParagraphEntity FromModelToEntity(this Paragraph model) { - return new ParagraphEntity(model.Id, model.ContentTitle, model.ContentContent, model.Info, model.Query, - model.Comment, model.LessonId); + return new ParagraphEntity + { + Id = model.Id, + ContentTitle = model.ContentTitle, + ContentContent = model.ContentContent, + Info = model.Info, + Query = model.Query, + Comment = model.Comment, LessonId = model.LessonId + }; } } \ No newline at end of file diff --git a/API_SQLuedo/Shared/ParagraphDataService.cs b/API_SQLuedo/Shared/ParagraphDataService.cs index 7954f78..bfa3bb0 100644 --- a/API_SQLuedo/Shared/ParagraphDataService.cs +++ b/API_SQLuedo/Shared/ParagraphDataService.cs @@ -43,7 +43,7 @@ public class ParagraphDataService : IParagraphDataService break; } var paragraphs = query.ToList(); - return paragraphs.Select(s => s.FromEntityToDTO()); + return paragraphs.Select(s => s.FromEntityToDto()); } public ParagraphDTO GetParagraphById(int id) @@ -54,7 +54,7 @@ public class ParagraphDataService : IParagraphDataService throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id)); } - return paragraphEntity.FromEntityToDTO(); + return paragraphEntity.FromEntityToDto(); } public ParagraphDTO GetParagraphByTitle(string title) @@ -65,7 +65,7 @@ public class ParagraphDataService : IParagraphDataService throw new ArgumentException("Impossible de trouver le paragraphe", nameof(title)); } - return paragraphEntity.FromEntityToDTO(); + return paragraphEntity.FromEntityToDto(); } public bool DeleteParagraph(int id) @@ -95,7 +95,7 @@ public class ParagraphDataService : IParagraphDataService updatingParagraph.Query = paragraphDTO.Query; updatingParagraph.Comment = paragraphDTO.Comment; DbContext.SaveChangesAsync(); - return updatingParagraph.FromEntityToDTO(); + return updatingParagraph.FromEntityToDto(); } public ParagraphDTO CreateParagraph(string title, string content, string info, string query, string comment, int lessonId) @@ -109,7 +109,7 @@ public class ParagraphDataService : IParagraphDataService Comment = comment, LessonId = lessonId }; - DbContext.Paragraphs.Add(newParagraphEntity.FromDTOToEntity()); + DbContext.Paragraphs.Add(newParagraphEntity.FromDtoToEntity()); DbContext.SaveChangesAsync(); return newParagraphEntity; } diff --git a/API_SQLuedo/TestEF/EntitiesTests/ConcreteContentLessonEntity.cs b/API_SQLuedo/TestEF/EntitiesTests/ConcreteContentLessonEntity.cs deleted file mode 100644 index 15789eb..0000000 --- a/API_SQLuedo/TestEF/EntitiesTests/ConcreteContentLessonEntity.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Entities; - -namespace TestEF.EntitiesTests; - -public class ConcreteContentLessonEntity : ContentLessonEntity -{ - public ConcreteContentLessonEntity(int id, string contentContent, string contentTitle) - : base(id, contentContent, contentTitle) - { - } - - public ConcreteContentLessonEntity(int id, int lessonId, string contentContent, string contentTitle) - : base(id, lessonId, contentContent, contentTitle) - { - } - - public ConcreteContentLessonEntity(string contentContent, string contentTitle) - : base(contentContent, contentTitle) - { - } -} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/EntitiesTests/TestContentLessonEntity.cs b/API_SQLuedo/TestEF/EntitiesTests/TestContentLessonEntity.cs deleted file mode 100644 index 6d2bd7e..0000000 --- a/API_SQLuedo/TestEF/EntitiesTests/TestContentLessonEntity.cs +++ /dev/null @@ -1,55 +0,0 @@ -namespace TestEF.EntitiesTests; - -public class TestContentLessonEntity -{ - [Fact] - public void Constructor_ShouldSetProperties_WhenCalledWithTwoParameters() - { - // Arrange - const string contentContent = "Content"; - const string contentTitle = "Title"; - - // Act - var concreteContentLessonEntity = new ConcreteContentLessonEntity(contentContent, contentTitle); - - // Assert - Assert.Equal(contentContent, concreteContentLessonEntity.ContentContent); - Assert.Equal(contentTitle, concreteContentLessonEntity.ContentTitle); - } - - [Fact] - public void Constructor_ShouldSetProperties_WhenCalledWithThreeParameters() - { - // Arrange - const int id = 1; - const string contentContent = "Content"; - const string contentTitle = "Title"; - - // Act - var concreteContentLessonEntity = new ConcreteContentLessonEntity(id, contentContent, contentTitle); - - // Assert - Assert.Equal(id, concreteContentLessonEntity.Id); - Assert.Equal(contentContent, concreteContentLessonEntity.ContentContent); - Assert.Equal(contentTitle, concreteContentLessonEntity.ContentTitle); - } - - [Fact] - public void Constructor_ShouldSetProperties_WhenCalledWithFourParameters() - { - // Arrange - const int id = 1; - const int lessonId = 2; - const string contentContent = "Content"; - const string contentTitle = "Title"; - - // Act - var concreteContentLessonEntity = new ConcreteContentLessonEntity(id, lessonId, contentContent, contentTitle); - - // Assert - Assert.Equal(id, concreteContentLessonEntity.Id); - Assert.Equal(lessonId, concreteContentLessonEntity.LessonId); - Assert.Equal(contentContent, concreteContentLessonEntity.ContentContent); - Assert.Equal(contentTitle, concreteContentLessonEntity.ContentTitle); - } -} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/EntitiesTests/TestParagraphEntity.cs b/API_SQLuedo/TestEF/EntitiesTests/TestParagraphEntity.cs index 1f2228d..6853fc1 100644 --- a/API_SQLuedo/TestEF/EntitiesTests/TestParagraphEntity.cs +++ b/API_SQLuedo/TestEF/EntitiesTests/TestParagraphEntity.cs @@ -1,20 +1,20 @@ using Entities; -namespace TestEF; +namespace TestEF.EntitiesTests; public class TestParagraphEntity { - private const int _id = 42; - private const string _title = "Title"; - private const string _info = "Info"; - private const string _content = "Content"; - private const string _query = "Query"; - private const string _comment = "Comment"; + private const int Id = 42; + private const string Title = "Title"; + private const string Info = "Info"; + private const string Content = "Content"; + private const string Query = "Query"; + private const string Comment = "Comment"; [Fact] public void TestDefaultConstructor() { - ParagraphEntity paragraph = new ParagraphEntity(); + var paragraph = new ParagraphEntity(); Assert.Equal(0, paragraph.Id); Assert.Null(paragraph.Title); Assert.Null(paragraph.Info); @@ -22,29 +22,45 @@ public class TestParagraphEntity Assert.Null(paragraph.Query); Assert.Null(paragraph.Comment); } - + [Fact] public void TestConstructorWithoutId() { - ParagraphEntity paragraph = new ParagraphEntity(_title,_content,_info,_query,_comment); + var paragraph = new ParagraphEntity + { + Title = Title, + Content = Content, + Info = Info, + Query = Query, + Comment = Comment + }; Assert.Equal(0, paragraph.Id); - Assert.Equal(_title,paragraph.Title); - Assert.Equal(_info,paragraph.Info); - Assert.Equal(_content,paragraph.Content); - Assert.Equal(_query,paragraph.Query); - Assert.Equal(_comment,paragraph.Comment); + Assert.Equal(Title, paragraph.Title); + Assert.Equal(Info, paragraph.Info); + Assert.Equal(Content, paragraph.Content); + Assert.Equal(Query, paragraph.Query); + Assert.Equal(Comment, paragraph.Comment); } - + [Fact] public void TestConstructorWithAllAttributes() { - ParagraphEntity paragraph = new ParagraphEntity(_id,_title,_content,_info,_query,_comment,10); - Assert.Equal(_id, paragraph.Id); - Assert.Equal(_title,paragraph.Title); - Assert.Equal(_info,paragraph.Info); - Assert.Equal(_content,paragraph.Content); - Assert.Equal(_query,paragraph.Query); - Assert.Equal(_comment,paragraph.Comment); + var paragraph = new ParagraphEntity + { + Id = Id, + Title = Title, + Content = Content, + Info = Info, + Query = Query, + Comment = Comment, + LessonId = 10 + }; + Assert.Equal(Id, paragraph.Id); + Assert.Equal(Title, paragraph.Title); + Assert.Equal(Info, paragraph.Info); + Assert.Equal(Content, paragraph.Content); + Assert.Equal(Query, paragraph.Query); + Assert.Equal(Comment, paragraph.Comment); Assert.Equal(10, paragraph.LessonId); } } \ No newline at end of file