Skip to content

Commit

Permalink
Add Creation and Update Dates (#48)
Browse files Browse the repository at this point in the history
* Fix a typo

* Add interfaces for dates

* Enhance `BaseCrudService`

Now `BaseCrudService` can assign creation and/or update dates to entities which have implemented `IHasCreationDate` and/or `IHasUpdateDate` interfaces.

* Add creation and update dates to 5 entities

Entities affected:
* Grade
* Classroom
* Course
* Group
* Semester

* Update view DTOs

* Update tests

* Update fake data generation
  • Loading branch information
romandykyi authored Nov 26, 2023
1 parent 179335f commit 612a7e4
Show file tree
Hide file tree
Showing 28 changed files with 1,124 additions and 32 deletions.
3 changes: 2 additions & 1 deletion Core/Dtos/University/ClassroomViewDto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
namespace EUniversity.Core.Dtos.University;

public record ClassroomViewDto(int Id, string Name);
public record ClassroomViewDto(int Id, string Name,
DateTimeOffset CreationDate, DateTimeOffset UpdateDate);
4 changes: 3 additions & 1 deletion Core/Dtos/University/CoursePreviewDto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace EUniversity.Core.Dtos.University;

public record CoursePreviewDto(int Id, string Name, SemesterMinimalViewDto? Semester);
public record CoursePreviewDto(int Id, string Name,
DateTimeOffset CreationDate, DateTimeOffset UpdateDate,
SemesterMinimalViewDto? Semester);
4 changes: 3 additions & 1 deletion Core/Dtos/University/CourseViewDto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace EUniversity.Core.Dtos.University;

public record CourseViewDto(int Id, string Name, string? Description, SemesterPreviewDto? Semester);
public record CourseViewDto(int Id, string Name, string? Description,
DateTimeOffset CreationDate, DateTimeOffset UpdateDate,
SemesterPreviewDto? Semester);
3 changes: 2 additions & 1 deletion Core/Dtos/University/Grades/GradeViewDto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
namespace EUniversity.Core.Dtos.University.Grades;

public record GradeViewDto(int Id, string Name, int Score);
public record GradeViewDto(int Id, string Name, int Score,
DateTimeOffset CreationDate, DateTimeOffset UpdateDate);
2 changes: 1 addition & 1 deletion Core/Dtos/University/GroupPreviewDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace EUniversity.Core.Dtos.University;

public record GroupPreviewDto(int Id, string Name, TeacherPreviewDto? Teacher, CoursePreviewDto Course);
public record GroupPreviewDto(int Id, string Name, DateTimeOffset CreationDate, DateTimeOffset UpdateDate, TeacherPreviewDto? Teacher, CoursePreviewDto Course);
1 change: 1 addition & 0 deletions Core/Dtos/University/GroupViewDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
namespace EUniversity.Core.Dtos.University;

public record GroupViewDto(int Id, string Name,
DateTimeOffset CreationDate, DateTimeOffset UpdateDate,
TeacherPreviewDto? Teacher, CoursePreviewDto Course,
IEnumerable<StudentPreviewDto> Students);
4 changes: 3 additions & 1 deletion Core/Dtos/University/SemesterPreviewDto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace EUniversity.Core.Dtos.University;

public record SemesterPreviewDto(int Id, string Name, DateTimeOffset DateFrom, DateTimeOffset DateTo);
public record SemesterPreviewDto(int Id, string Name,
DateTimeOffset CreationDate, DateTimeOffset UpdateDate,
DateTimeOffset DateFrom, DateTimeOffset DateTo);
1 change: 1 addition & 0 deletions Core/Dtos/University/SemesterViewDto.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace EUniversity.Core.Dtos.University;

public record SemesterViewDto(int Id, string Name,
DateTimeOffset CreationDate, DateTimeOffset UpdateDate,
DateTimeOffset DateFrom, DateTimeOffset DateTo,
IEnumerable<StudentSemesterViewDto> StudentEnrollments);
12 changes: 12 additions & 0 deletions Core/Models/IHasCreationDate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace EUniversity.Core.Models;

/// <summary>
/// An interface that defines an entity that has a creation date property.
/// </summary>
public interface IHasCreationDate
{
/// <summary>
/// Date when the entity was created.
/// </summary>
public DateTimeOffset CreationDate { get; set; }
}
2 changes: 1 addition & 1 deletion Core/Models/IHasName.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace EUniversity.Core.Models;

/// <summary>
/// An interface that defines an object that have a Name property.
/// An interface that defines an object that has a Name property.
/// </summary>
public interface IHasName
{
Expand Down
12 changes: 12 additions & 0 deletions Core/Models/IHasUpdateDate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace EUniversity.Core.Models;

/// <summary>
/// An interface that defines an entity that has an update(edit) date property.
/// </summary>
public interface IHasUpdateDate
{
/// <summary>
/// Date when the entity was last updated.
/// </summary>
public DateTimeOffset UpdateDate { get; set; }
}
11 changes: 10 additions & 1 deletion Core/Models/University/Classroom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Represents a classroom entity.
/// </summary>
public class Classroom : IEntity<int>, IHasName
public class Classroom : IEntity<int>, IHasName, IHasCreationDate, IHasUpdateDate
{
public const int MaxNameLength = 50;

Expand All @@ -14,4 +14,13 @@ public class Classroom : IEntity<int>, IHasName
/// </summary>
[StringLength(MaxNameLength)]
public string Name { get; set; } = null!;

/// <summary>
/// Date when the classroom was created.
/// </summary>
public DateTimeOffset CreationDate { get; set; }
/// <summary>
/// Date when the classroom was last updated.
/// </summary>
public DateTimeOffset UpdateDate { get; set; }
}
11 changes: 10 additions & 1 deletion Core/Models/University/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace EUniversity.Core.Models.University;
/// <summary>
/// Represents a course entity.
/// </summary>
public class Course : IEntity<int>, IHasName
public class Course : IEntity<int>, IHasName, IHasCreationDate, IHasUpdateDate
{
public const int MaxNameLength = 200;
public const int MaxDescriptionLength = 1000;
Expand All @@ -28,6 +28,15 @@ public class Course : IEntity<int>, IHasName
[StringLength(MaxDescriptionLength)]
public string? Description { get; set; }

/// <summary>
/// Date when the course was created.
/// </summary>
public DateTimeOffset CreationDate { get; set; }
/// <summary>
/// Date when the course was last updated.
/// </summary>
public DateTimeOffset UpdateDate { get; set; }

/// <summary>
/// Navigation property of the semester associated with this course(may be null).
/// </summary>
Expand Down
11 changes: 10 additions & 1 deletion Core/Models/University/Grades/Grade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Represents an available grade entity that can be assigned to students.
/// </summary>
public class Grade : IEntity<int>, IHasName
public class Grade : IEntity<int>, IHasName, IHasCreationDate, IHasUpdateDate
{
public const int MaxNameLength = 50;

Expand All @@ -19,6 +19,15 @@ public class Grade : IEntity<int>, IHasName
/// </summary>
public int Score { get; set; }

/// <summary>
/// Date when the grade was created.
/// </summary>
public DateTimeOffset CreationDate { get; set; }
/// <summary>
/// Date when the grade was last updated.
/// </summary>
public DateTimeOffset UpdateDate { get; set; }

/// <summary>
/// Navigation property:
/// Collection of <see cref="CourseGrades" />s related to this grade.
Expand Down
11 changes: 10 additions & 1 deletion Core/Models/University/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace EUniversity.Core.Models.University;
/// Represents a group entity,
/// which contains students with its own teacher and course.
/// </summary>
public class Group : IEntity<int>, IHasName
public class Group : IEntity<int>, IHasName, IHasCreationDate, IHasUpdateDate
{
public const int MaxNameLength = 50;

Expand All @@ -28,6 +28,15 @@ public class Group : IEntity<int>, IHasName
[ForeignKey(nameof(Teacher))]
public string? TeacherId { get; set; }

/// <summary>
/// Date when the group was created.
/// </summary>
public DateTimeOffset CreationDate { get; set; }
/// <summary>
/// Date when the group was last updated.
/// </summary>
public DateTimeOffset UpdateDate { get; set; }

/// <summary>
/// Navigation property to the course associated with this group.
/// </summary>
Expand Down
11 changes: 10 additions & 1 deletion Core/Models/University/Semester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// An entity that represents a semester.
/// </summary>
public class Semester : IEntity<int>, IHasName
public class Semester : IEntity<int>, IHasName, IHasCreationDate, IHasUpdateDate
{
public const int MaxNameLength = 100;

Expand All @@ -23,6 +23,15 @@ public class Semester : IEntity<int>, IHasName
/// </summary>
public DateTimeOffset DateTo { get; set; }

/// <summary>
/// Date when the semester was created.
/// </summary>
public DateTimeOffset CreationDate { get; set; }
/// <summary>
/// Date when the semester was last updated.
/// </summary>
public DateTimeOffset UpdateDate { get; set; }

/// <summary>
/// Navigation property to the students which are part of this semester.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions Infrastructure/Extensions/FakerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Bogus;
using EUniversity.Core.Models;

namespace EUniversity.Infrastructure.Extensions;

public static class FakerExtensions
{
public static Faker<T> RuleForDates<T>(this Faker<T> faker)
where T : class, IHasCreationDate, IHasUpdateDate
{
return faker
.RuleFor(x => x.CreationDate, f => f.Date.RecentOffset(180))
.RuleFor(x => x.UpdateDate,
(f, x) => f.Date.BetweenOffset(x.CreationDate, x.CreationDate + TimeSpan.FromDays(90)));
}
}
Loading

0 comments on commit 612a7e4

Please sign in to comment.