Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #531 from kysect/refactor/parameterized-entity-id
Browse files Browse the repository at this point in the history
Refactor/parameterized entity
  • Loading branch information
ronimizy authored Jan 4, 2023
2 parents 0ceade2 + 37eb3ab commit b77cc41
Show file tree
Hide file tree
Showing 49 changed files with 118 additions and 89 deletions.
5 changes: 4 additions & 1 deletion Source/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,7 @@ dotnet_style_allow_statement_immediately_after_block_experimental = true:silent
csharp_style_prefer_utf8_string_literals = true:suggestion

# ReSharper
resharper_csharp_empty_block_style = together_same_line
resharper_csharp_empty_block_style = together_same_line

csharp_wrap_arguments_style=chop_if_long
csharp_wrap_chained_method_calls=chop_if_long
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public async Task<Submission> CreateAsync(Guid userId, Guid assignmentId, Cancel
.CountAsync(cancellationToken);

return new GithubSubmission(
Guid.NewGuid(),
count + 1,
student,
groupAssignment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Task<string> GetSubjectCourseTableId(Guid subjectCourseId, CancellationTo
string spreadsheetId = await _spreadsheetManagementService
.CreateSpreadsheetAsync(subjectCourse.Title, cancellationToken);

spreadsheetAssociation = new GoogleTableSubjectCourseAssociation(subjectCourse, spreadsheetId);
spreadsheetAssociation = new GoogleTableSubjectCourseAssociation(Guid.NewGuid(), subjectCourse, spreadsheetId);
_context.SubjectCourseAssociations.Add(spreadsheetAssociation);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ public AddGithubSubjectCourseAssociationHandler(IShreksDatabaseContext context)

public async Task<Response> Handle(Command request, CancellationToken cancellationToken)
{
SubjectCourse subjectCourse =
await _context.SubjectCourses.GetByIdAsync(request.SubjectCourseId, cancellationToken);
var githubSubjectCourseAssociation =
new GithubSubjectCourseAssociation(subjectCourse, request.Organization, request.TemplateRepository);
SubjectCourse subjectCourse = await _context.SubjectCourses
.GetByIdAsync(request.SubjectCourseId, cancellationToken);

var githubSubjectCourseAssociation = new GithubSubjectCourseAssociation(
Guid.NewGuid(),
subjectCourse,
request.Organization,
request.TemplateRepository);

_context.SubjectCourseAssociations.Add(githubSubjectCourseAssociation);
await _context.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<Unit> Handle(Command request, CancellationToken cancellationTo

Student student = await _context.Students.GetByIdAsync(request.UserId, cancellationToken);

var association = new GithubUserAssociation(student.User, request.GithubUsername);
var association = new GithubUserAssociation(Guid.NewGuid(), student.User, request.GithubUsername);
_context.UserAssociations.Add(association);
_context.Users.Update(student.User);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ private async Task CreateMentorFromAdmin(
if (!isGithubUserExists)
throw new DomainInvalidOperationException($"Github user with username {adminUsername} does not exist");

var adminUser = new User(adminUsername, adminUsername, adminUsername);
var githubUserAssociation = new GithubUserAssociation(adminUser, adminUsername);
var adminUser = new User(Guid.NewGuid(), adminUsername, adminUsername, adminUsername);
var githubUserAssociation = new GithubUserAssociation(Guid.NewGuid(), adminUser, adminUsername);

_shreksDatabaseContext.Users.Add(adminUser);
_shreksDatabaseContext.UserAssociations.Add(githubUserAssociation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<Response> Handle(Command request, CancellationToken cancellati
if (group is null)
throw EntityNotFoundException.For<StudentGroup>(request.GroupId);

var user = new User(request.FirstName, request.MiddleName, request.LastName);
var user = new User(Guid.NewGuid(), request.FirstName, request.MiddleName, request.LastName);
var student = new Student(user, group);

_context.Students.Add(student);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public async Task<Response> Handle(Command request, CancellationToken cancellati
await _context.SubjectCourses.GetByIdAsync(request.SubjectCourseId, cancellationToken);

var assignment = new Assignment(
Guid.NewGuid(),
request.Title,
request.ShortName,
request.Order,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CreateStudyGroupHandler(IShreksDatabaseContext context, IMapper mapper)

public async Task<Response> Handle(Command request, CancellationToken cancellationToken)
{
var studentGroup = new StudentGroup(request.Name);
var studentGroup = new StudentGroup(Guid.NewGuid(), request.Name);
_context.StudentGroups.Add(studentGroup);
await _context.SaveChangesAsync(cancellationToken);
return new Response(_mapper.Map<StudyGroupDto>(studentGroup));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<Response> Handle(Command request, CancellationToken cancellati
{
Subject subject = await _context.Subjects.GetByIdAsync(request.SubjectId, cancellationToken);
var subjectCourse =
new SubjectCourse(subject, request.Title, (SubmissionStateWorkflowType)request.WorkflowType);
new SubjectCourse(Guid.NewGuid(), subject, request.Title, (SubmissionStateWorkflowType)request.WorkflowType);
_context.SubjectCourses.Add(subjectCourse);
await _context.SaveChangesAsync(cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CreateSubjectHandler(IShreksDatabaseContext context, IMapper mapper)

public async Task<Response> Handle(Command request, CancellationToken cancellationToken)
{
var subject = new Subject(request.Title);
var subject = new Subject(Guid.NewGuid(), request.Title);
_context.Subjects.Add(subject);
await _context.SaveChangesAsync(cancellationToken);
return new Response(_mapper.Map<SubjectDto>(subject));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<Response> Handle(Command request, CancellationToken cancellati
throw new DomainInvalidOperationException(message);
}

var association = new GithubUserAssociation(user, request.GithubUsername);
var association = new GithubUserAssociation(Guid.NewGuid(), user, request.GithubUsername);
user.AddAssociation(association);

await _context.UserAssociations.AddAsync(association, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task<Unit> Handle(Command request, CancellationToken cancellationTo

if (association is null)
{
association = new IsuUserAssociation(user, request.UniversityId);
association = new IsuUserAssociation(Guid.NewGuid(), user, request.UniversityId);
_context.UserAssociations.Add(association);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ private void AddGithubUserAssociations(DeveloperEnvironmentSeedingRequest reques
{
SubjectCourse subjectCourse = _subjectCourseGenerator.GeneratedEntities[0];
_context.SubjectCourses.Attach(subjectCourse);
var githubSubjectCourseAssociation =
new GithubSubjectCourseAssociation(subjectCourse, request.Organization, request.TemplateRepository);

var githubSubjectCourseAssociation = new GithubSubjectCourseAssociation(
Guid.NewGuid(),
subjectCourse,
request.Organization,
request.TemplateRepository);

_context.SubjectCourseAssociations.Add(githubSubjectCourseAssociation);
}

Expand All @@ -58,7 +63,7 @@ private void AddUsers(DeveloperEnvironmentSeedingRequest request)
{
User user = users[index];
string login = request.Users[index];
_context.UserAssociations.Add(new GithubUserAssociation(user, login));
_context.UserAssociations.Add(new GithubUserAssociation(Guid.NewGuid(), user, login));
}
}
}
2 changes: 1 addition & 1 deletion Source/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<AnalysisLevel>latest</AnalysisLevel>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1591;SA1633;SA1101;SA1503;SA1309;SA1601;SA1201;SA1502;SA1127;SA1128</NoWarn>
<NoWarn>1591;SA1633;SA1101;SA1503;SA1309;SA1601;SA1201;SA1502;SA1127;SA1128;SA1600;SA1611;SA1604;SA1629;SA1602</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers">
Expand Down
3 changes: 2 additions & 1 deletion Source/Domain/Kysect.Shreks.Core/Study/Assignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ public partial class Assignment : IEntity<Guid>
private readonly HashSet<GroupAssignment> _groupAssignments;

public Assignment(
Guid id,
string title,
string shortName,
int order,
Points minPoints,
Points maxPoints,
SubjectCourse subjectCourse)
: this(Guid.NewGuid())
: this(id)
{
ArgumentNullException.ThrowIfNull(title);

Expand Down
3 changes: 1 addition & 2 deletions Source/Domain/Kysect.Shreks.Core/Study/StudentGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ public partial class StudentGroup : IEntity<Guid>
{
private readonly HashSet<Student> _students;

public StudentGroup(string name)
: this(Guid.NewGuid())
public StudentGroup(Guid id, string name) : this(id)
{
ArgumentNullException.ThrowIfNull(name);

Expand Down
3 changes: 1 addition & 2 deletions Source/Domain/Kysect.Shreks.Core/Study/Subject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ public partial class Subject : IEntity<Guid>
{
private readonly HashSet<SubjectCourse> _courses;

public Subject(string title)
: this(Guid.NewGuid())
public Subject(Guid id, string title) : this(id)
{
ArgumentNullException.ThrowIfNull(title);

Expand Down
4 changes: 2 additions & 2 deletions Source/Domain/Kysect.Shreks.Core/Study/SubjectCourse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public partial class SubjectCourse : IEntity<Guid>
private readonly HashSet<SubjectCourseGroup> _groups;
private readonly HashSet<Mentor> _mentors;

public SubjectCourse(Subject subject, string title, SubmissionStateWorkflowType? workflowType)
: this(Guid.NewGuid())
public SubjectCourse(Guid id, Subject subject, string title, SubmissionStateWorkflowType? workflowType)
: this(id)
{
Subject = subject;
Title = title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ namespace Kysect.Shreks.Core.SubjectCourseAssociations;
public partial class GithubSubjectCourseAssociation : SubjectCourseAssociation
{
public GithubSubjectCourseAssociation(
Guid id,
SubjectCourse subjectCourse,
string githubOrganizationName,
string templateRepositoryName)
: base(subjectCourse)
: base(id)
{
GithubOrganizationName = githubOrganizationName;
TemplateRepositoryName = templateRepositoryName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace Kysect.Shreks.Core.SubjectCourseAssociations;

public partial class GoogleTableSubjectCourseAssociation : SubjectCourseAssociation
{
public GoogleTableSubjectCourseAssociation(SubjectCourse subjectCourse, string spreadsheetId)
: base(subjectCourse)
public GoogleTableSubjectCourseAssociation(Guid id, SubjectCourse subjectCourse, string spreadsheetId) : base(id)
{
SpreadsheetId = spreadsheetId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace Kysect.Shreks.Core.SubjectCourseAssociations;

public abstract partial class SubjectCourseAssociation : IEntity<Guid>
{
protected SubjectCourseAssociation(SubjectCourse subjectCourse)
: this(Guid.NewGuid())
protected SubjectCourseAssociation(Guid id, SubjectCourse subjectCourse) : this(id)
{
SubjectCourse = subjectCourse;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ namespace Kysect.Shreks.Core.SubmissionAssociations;
public partial class GithubSubmissionAssociation : SubmissionAssociation
{
public GithubSubmissionAssociation(
Guid id,
GithubSubmission submission,
string organization,
string repository,
long prNumber)
: base(submission)
: base(id)
{
Repository = repository;
PrNumber = prNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace Kysect.Shreks.Core.SubmissionAssociations;

public abstract partial class SubmissionAssociation : IEntity<Guid>
{
protected SubmissionAssociation(Submission submission)
: this(Guid.NewGuid())
protected SubmissionAssociation(Guid id, Submission submission) : this(id)
{
Submission = submission;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Kysect.Shreks.Core.Submissions;
public partial class GithubSubmission : Submission
{
public GithubSubmission(
Guid id,
int code,
Student student,
GroupAssignment groupAssignment,
Expand All @@ -16,9 +17,10 @@ public GithubSubmission(
string organization,
string repository,
long prNumber)
: base(code, student, groupAssignment, submissionDate, payload)
: base(id, code, student, groupAssignment, submissionDate, payload)
{
var association = new GithubSubmissionAssociation(
Guid.NewGuid(),
this,
organization,
repository,
Expand Down
3 changes: 2 additions & 1 deletion Source/Domain/Kysect.Shreks.Core/Submissions/Submission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ public abstract partial class Submission : IEntity<Guid>
private readonly HashSet<SubmissionAssociation> _associations;

protected Submission(
Guid id,
int code,
Student student,
GroupAssignment groupAssignment,
SpbDateTime submissionDate,
string payload)
: this(Guid.NewGuid())
: this(id)
{
Code = code;
SubmissionDate = submissionDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace Kysect.Shreks.Core.UserAssociations;

public partial class GithubUserAssociation : UserAssociation
{
public GithubUserAssociation(User user, string githubUsername)
: base(user)
public GithubUserAssociation(Guid id, User user, string githubUsername) : base(id, user)
{
GithubUsername = githubUsername;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace Kysect.Shreks.Core.UserAssociations;

public partial class IsuUserAssociation : UserAssociation
{
public IsuUserAssociation(User user, int universityId)
: base(user)
public IsuUserAssociation(Guid id, User user, int universityId) : base(id, user)
{
UniversityId = universityId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace Kysect.Shreks.Core.UserAssociations;

public abstract partial class UserAssociation : IEntity<Guid>
{
protected UserAssociation(User user)
: this(Guid.NewGuid())
protected UserAssociation(Guid id, User user) : this(id)
{
User = user;
user.AddAssociation(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public static class StudentExtensions
{
public static GithubUserAssociation AddGithubAssociation(this Student student, string githubUsername)
{
return new GithubUserAssociation(student.User, githubUsername);
return new GithubUserAssociation(Guid.NewGuid(), student.User, githubUsername);
}
}
3 changes: 1 addition & 2 deletions Source/Domain/Kysect.Shreks.Core/Users/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ public partial class User : IEntity<Guid>
{
private readonly HashSet<UserAssociation> _associations;

public User(string firstName, string middleName, string lastName)
: this(Guid.NewGuid())
public User(Guid id, string firstName, string middleName, string lastName) : this(id)
{
ArgumentNullException.ThrowIfNull(firstName);
ArgumentNullException.ThrowIfNull(middleName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ public interface ISheetManagementService
{
/// <summary>
/// Clears all values and formatting of a sheet
/// if it exists and creates it otherwise
/// if it exists and creates it otherwise.
/// </summary>
/// <returns>Sheet id</returns>
/// <returns>Sheet id.</returns>
Task<int> CreateOrClearSheetAsync(string spreadsheetId, string sheetTitle, CancellationToken token);

/// <returns>Sheet id</returns>
/// <returns>Sheet id.</returns>
Task<int> CreateSheetAsync(string spreadsheetId, string sheetTitle, CancellationToken token);

Task<bool> CheckIfExists(string spreadsheetId, string sheetTitle, CancellationToken token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected override Assignment Generate(int index)
int assignmentOrder = index + 1;

var assignment = new Assignment(
_faker.Random.Guid(),
_faker.Commerce.Product(),
$"lab-{assignmentOrder}",
assignmentOrder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public StudentGroupGenerator(EntityGeneratorOptions<StudentGroup> options, Faker
protected override StudentGroup Generate(int index)
{
int groupNumber = _faker.Random.Int(MinGroupNumber, MaxGroupNumber);
return new StudentGroup($"M{groupNumber}");
return new StudentGroup(_faker.Random.Guid(), $"M{groupNumber}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected override SubjectCourse Generate(int index)

const SubmissionStateWorkflowType reviewType = SubmissionStateWorkflowType.ReviewWithDefense;

var subjectCourse = new SubjectCourse(subject, subjectCourseName, reviewType);
var subjectCourse = new SubjectCourse(_faker.Random.Guid(), subject, subjectCourseName, reviewType);

IEnumerable<User> users = _faker.Random
.ListItems(_userGenerator.GeneratedEntities.ToList(), 2)
Expand Down
Loading

0 comments on commit b77cc41

Please sign in to comment.