Modification du constructeur de paragrapheEntity

pull/40/head
Clement CHIEU 1 year ago
parent 210093f565
commit b22e31d352

@ -10,30 +10,4 @@ public abstract class ContentLessonEntity
public string ContentTitle { get; set; } public string ContentTitle { get; set; }
[ForeignKey(nameof(Lesson))] public int LessonId { get; set; } [ForeignKey(nameof(Lesson))] public int LessonId { get; set; }
public LessonEntity Lesson { get; set; } = null!; 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;
}
} }

@ -14,28 +14,4 @@ public class ParagraphEntity : ContentLessonEntity
public string Info { get; set; } public string Info { get; set; }
public string Query { get; set; } public string Query { get; set; }
public string Comment { 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;
}
} }

@ -16,7 +16,7 @@ public static class ParagraphMapper
return new Paragraph(model.ContentTitle, model.ContentContent, model.Info, model.Query, model.Comment); 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, return new ParagraphDTO(model.Id, model.ContentTitle, model.ContentContent, model.Info, model.Query,
model.Comment, model.LessonId); model.Comment, model.LessonId);
@ -28,15 +28,30 @@ public static class ParagraphMapper
model.Comment, model.LessonId); 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, return new ParagraphEntity
dto.LessonId); {
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) public static ParagraphEntity FromModelToEntity(this Paragraph model)
{ {
return new ParagraphEntity(model.Id, model.ContentTitle, model.ContentContent, model.Info, model.Query, return new ParagraphEntity
model.Comment, model.LessonId); {
Id = model.Id,
ContentTitle = model.ContentTitle,
ContentContent = model.ContentContent,
Info = model.Info,
Query = model.Query,
Comment = model.Comment, LessonId = model.LessonId
};
} }
} }

@ -43,7 +43,7 @@ public class ParagraphDataService : IParagraphDataService
break; break;
} }
var paragraphs = query.ToList(); var paragraphs = query.ToList();
return paragraphs.Select(s => s.FromEntityToDTO()); return paragraphs.Select(s => s.FromEntityToDto());
} }
public ParagraphDTO GetParagraphById(int id) public ParagraphDTO GetParagraphById(int id)
@ -54,7 +54,7 @@ public class ParagraphDataService : IParagraphDataService
throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id)); throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id));
} }
return paragraphEntity.FromEntityToDTO(); return paragraphEntity.FromEntityToDto();
} }
public ParagraphDTO GetParagraphByTitle(string title) public ParagraphDTO GetParagraphByTitle(string title)
@ -65,7 +65,7 @@ public class ParagraphDataService : IParagraphDataService
throw new ArgumentException("Impossible de trouver le paragraphe", nameof(title)); throw new ArgumentException("Impossible de trouver le paragraphe", nameof(title));
} }
return paragraphEntity.FromEntityToDTO(); return paragraphEntity.FromEntityToDto();
} }
public bool DeleteParagraph(int id) public bool DeleteParagraph(int id)
@ -95,7 +95,7 @@ public class ParagraphDataService : IParagraphDataService
updatingParagraph.Query = paragraphDTO.Query; updatingParagraph.Query = paragraphDTO.Query;
updatingParagraph.Comment = paragraphDTO.Comment; updatingParagraph.Comment = paragraphDTO.Comment;
DbContext.SaveChangesAsync(); DbContext.SaveChangesAsync();
return updatingParagraph.FromEntityToDTO(); return updatingParagraph.FromEntityToDto();
} }
public ParagraphDTO CreateParagraph(string title, string content, string info, string query, string comment, int lessonId) 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, Comment = comment,
LessonId = lessonId LessonId = lessonId
}; };
DbContext.Paragraphs.Add(newParagraphEntity.FromDTOToEntity()); DbContext.Paragraphs.Add(newParagraphEntity.FromDtoToEntity());
DbContext.SaveChangesAsync(); DbContext.SaveChangesAsync();
return newParagraphEntity; return newParagraphEntity;
} }

@ -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)
{
}
}

@ -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);
}
}

@ -1,20 +1,20 @@
using Entities; using Entities;
namespace TestEF; namespace TestEF.EntitiesTests;
public class TestParagraphEntity public class TestParagraphEntity
{ {
private const int _id = 42; private const int Id = 42;
private const string _title = "Title"; private const string Title = "Title";
private const string _info = "Info"; private const string Info = "Info";
private const string _content = "Content"; private const string Content = "Content";
private const string _query = "Query"; private const string Query = "Query";
private const string _comment = "Comment"; private const string Comment = "Comment";
[Fact] [Fact]
public void TestDefaultConstructor() public void TestDefaultConstructor()
{ {
ParagraphEntity paragraph = new ParagraphEntity(); var paragraph = new ParagraphEntity();
Assert.Equal(0, paragraph.Id); Assert.Equal(0, paragraph.Id);
Assert.Null(paragraph.Title); Assert.Null(paragraph.Title);
Assert.Null(paragraph.Info); Assert.Null(paragraph.Info);
@ -26,25 +26,41 @@ public class TestParagraphEntity
[Fact] [Fact]
public void TestConstructorWithoutId() 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(0, paragraph.Id);
Assert.Equal(_title,paragraph.Title); Assert.Equal(Title, paragraph.Title);
Assert.Equal(_info,paragraph.Info); Assert.Equal(Info, paragraph.Info);
Assert.Equal(_content,paragraph.Content); Assert.Equal(Content, paragraph.Content);
Assert.Equal(_query,paragraph.Query); Assert.Equal(Query, paragraph.Query);
Assert.Equal(_comment,paragraph.Comment); Assert.Equal(Comment, paragraph.Comment);
} }
[Fact] [Fact]
public void TestConstructorWithAllAttributes() public void TestConstructorWithAllAttributes()
{ {
ParagraphEntity paragraph = new ParagraphEntity(_id,_title,_content,_info,_query,_comment,10); var paragraph = new ParagraphEntity
Assert.Equal(_id, paragraph.Id); {
Assert.Equal(_title,paragraph.Title); Id = Id,
Assert.Equal(_info,paragraph.Info); Title = Title,
Assert.Equal(_content,paragraph.Content); Content = Content,
Assert.Equal(_query,paragraph.Query); Info = Info,
Assert.Equal(_comment,paragraph.Comment); 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); Assert.Equal(10, paragraph.LessonId);
} }
} }
Loading…
Cancel
Save