Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

refactor: decompose division #65

Merged
merged 14 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Source/SeaInk.Core/APIs/IUniversitySystemApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SeaInk.Core.Entities;
using System.Collections.Generic;
using SeaInk.Core.Entities;

namespace SeaInk.Core.APIs
{
Expand All @@ -12,6 +13,7 @@ public interface IUniversitySystemApi
Subject GetSubject(int id);
StudentAssignmentProgress GetStudentAssignmentProgress(int studentId, int assignmentId);
Division GetDivision(int mentorId, int subjectId);
List<StudyGroupSubject> GetStudyGroupSubjects(int mentorId, int subjectId);

void SaveUser(User user);
void SaveStudent(Student student);
Expand Down
4 changes: 1 addition & 3 deletions Source/SeaInk.Core/Entities/Division.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public class Division: IEntity

public string Title { get; set; }
public string SpreadsheetId { get; set; }
public virtual Mentor Mentor { get; set; }
public virtual Subject Subject { get; set; }
public virtual List<StudyGroup> Groups { get; set; } = new();
public virtual List<StudyGroupSubject> StudyGroupSubjects { get; set; } = new();
}
}
4 changes: 2 additions & 2 deletions Source/SeaInk.Core/Entities/Mentor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace SeaInk.Core.Entities
{
public class Mentor: User
public class Mentor : User
ronimizy marked this conversation as resolved.
Show resolved Hide resolved
{
public virtual List<Division> Divisions { get; set; } = new();
public virtual List<StudyGroupSubject> StudyGroupSubjects { get; set; } = new();
}
}
13 changes: 13 additions & 0 deletions Source/SeaInk.Core/Entities/StudyGroupSubject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace SeaInk.Core.Entities
{
public class StudyGroupSubject : IEntity
{
public int Id { get; set; }

public virtual StudyGroup StudyGroup { get; set; }
public virtual Subject Subject { get; set; }
public virtual List<Mentor> Mentors { get; set; }
}
}
10 changes: 2 additions & 8 deletions Source/SeaInk.Endpoints.Shared/Dto/DivisionDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ public record DivisionDto(
int Id,
string Title,
string SpreadsheetId,
int MentorId,
string MentorName,
SubjectDto Subject,
IReadOnlyList<StudyGroupDto> Groups);
IReadOnlyList<StudyGroupSubjectDto> StudyGroupSubjects);

public static class DivisionExtension
{
Expand All @@ -20,10 +17,7 @@ public static DivisionDto ToDto(this Division division)
return new DivisionDto(division.Id,
division.Title,
division.SpreadsheetId,
division.Mentor.Id,
division.Mentor.FullName,
division.Subject.ToDto(),
division.Groups.Select(g => g.ToDto()).ToList());
division.StudyGroupSubjects.Select(s => s.ToDto()).ToList());
}
}
}
12 changes: 2 additions & 10 deletions Source/SeaInk.Endpoints.Shared/Dto/MentorDto.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using MoreLinq;
using SeaInk.Core.Entities;

namespace SeaInk.Endpoints.Shared.Dto
Expand All @@ -12,8 +11,7 @@ public record MentorDto(
string LastName,
string MiddleName,
string FullName,
IReadOnlyList<SubjectDto> Subjects,
IReadOnlyList<DivisionDto> Divisions);
List<StudyGroupSubjectDto> StudyGroupSubjects);

public static class MentorExtension
{
Expand All @@ -25,13 +23,7 @@ public static MentorDto ToDto(this Mentor mentor)
mentor.LastName,
mentor.MiddleName,
mentor.FullName,
mentor.Divisions
.Select(d => d.Subject.ToDto())
.DistinctBy(s => s.Id)
.ToList(),
mentor.Divisions
.Select(d => d.ToDto())
.ToList());
mentor.StudyGroupSubjects.Select(s => s.ToDto()).ToList());
}
}
}
24 changes: 24 additions & 0 deletions Source/SeaInk.Endpoints.Shared/Dto/StudyGroupSubjectDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Linq;
using SeaInk.Core.Entities;

namespace SeaInk.Endpoints.Shared.Dto
{
public record StudyGroupSubjectDto(
int Id,
StudyGroupDto StudyGroup,
SubjectDto Subject,
List<int> MentorIds);

public static class StudyGroupSubjectExtension
{
public static StudyGroupSubjectDto ToDto(this StudyGroupSubject studyGroupSubject)
{
return new StudyGroupSubjectDto(
studyGroupSubject.Id,
studyGroupSubject.StudyGroup.ToDto(),
studyGroupSubject.Subject.ToDto(),
studyGroupSubject.Mentors.Select(m => m.Id).ToList());
}
}
}
16 changes: 9 additions & 7 deletions Source/SeaInk.Endpoints/Client/Pages/Groups/GroupsPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public partial class GroupsPage
private int _selectedSubjectId;

private IReadOnlyList<DivisionDto> _divisions;
private int _selectedDivisionId;

private IReadOnlyList<StudyGroupDto> _groups;
private string _selectedGroupId;
Expand All @@ -26,7 +25,8 @@ protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_currentMentor = await Client.CurrentAsync();
_subjects = _currentMentor.Subjects.ToList();
_divisions = (await Client.DivisionsAsync(_currentMentor.Id)).ToList();
_subjects = _currentMentor.StudyGroupSubjects.Select(sgs => sgs.Subject).ToList();

if (_subjects.Count != 0)
OnSelectedSubjectChanged(_subjects[0].Id);
Expand All @@ -35,18 +35,20 @@ protected override async Task OnInitializedAsync()
private void OnSelectedSubjectChanged(int subjectId)
{
_selectedSubjectId = subjectId;
_divisions = _currentMentor.Divisions.Where(d => d.Subject.Id == _selectedSubjectId).ToList();

if (_divisions.Count != 0)
OnSelectedDivisionChanged(_divisions[0].Id);
}

private void OnSelectedDivisionChanged(int divisionId)
{
_selectedDivisionId = divisionId;
_groups = _currentMentor.Divisions
.Single(d => d.Subject.Id == _selectedSubjectId && d.Id == _selectedDivisionId)
.Groups
_groups = _divisions
.Where(d => d.Id == divisionId)
.SelectMany(d => d.StudyGroupSubjects)
.Where(sgs => sgs.Subject.Id == _selectedSubjectId)
.Select(sgs => sgs.StudyGroup)
.GroupBy(g => g.Id)
.Select(gg => gg.First())
.ToList();

if (_groups.Count == 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,84 @@ public async System.Threading.Tasks.Task<MentorDto> MentorsAsync(int mentorId, S
}
}

/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public System.Threading.Tasks.Task<System.Collections.Generic.ICollection<DivisionDto>> DivisionsAsync(int mentorId)
{
return DivisionsAsync(mentorId, System.Threading.CancellationToken.None);
}

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public async System.Threading.Tasks.Task<System.Collections.Generic.ICollection<DivisionDto>> DivisionsAsync(int mentorId, System.Threading.CancellationToken cancellationToken)
{
if (mentorId == null)
throw new System.ArgumentNullException("mentorId");

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append("mentors/{mentorId}/divisions");
urlBuilder_.Replace("{mentorId}", System.Uri.EscapeDataString(ConvertToString(mentorId, System.Globalization.CultureInfo.InvariantCulture)));

var client_ = _httpClient;
var disposeClient_ = false;
try
{
using (var request_ = new System.Net.Http.HttpRequestMessage())
{
request_.Method = new System.Net.Http.HttpMethod("GET");
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("text/plain"));

PrepareRequest(client_, request_, urlBuilder_);

var url_ = urlBuilder_.ToString();
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);

PrepareRequest(client_, request_, url_);

var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
var disposeResponse_ = true;
try
{
var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}

ProcessResponse(client_, response_);

var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<System.Collections.Generic.ICollection<DivisionDto>>(response_, headers_, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
}
return objectResponse_.Object;
}
else
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
}
}
finally
{
if (disposeResponse_)
response_.Dispose();
}
}
}
finally
{
if (disposeClient_)
client_.Dispose();
}
}

protected struct ObjectResponseResult<T>
{
public ObjectResponseResult(T responseObject, string responseText)
Expand Down Expand Up @@ -384,17 +462,8 @@ public partial class DivisionDto
[System.Text.Json.Serialization.JsonPropertyName("spreadsheetId")]
public string SpreadsheetId { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("mentorId")]
public int MentorId { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("mentorName")]
public string MentorName { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("subject")]
public SubjectDto Subject { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("groups")]
public System.Collections.Generic.ICollection<StudyGroupDto> Groups { get; set; }
[System.Text.Json.Serialization.JsonPropertyName("studyGroupSubjects")]
public System.Collections.Generic.ICollection<StudyGroupSubjectDto> StudyGroupSubjects { get; set; }


}
Expand All @@ -420,11 +489,8 @@ public partial class MentorDto
[System.Text.Json.Serialization.JsonPropertyName("fullName")]
public string FullName { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("subjects")]
public System.Collections.Generic.ICollection<SubjectDto> Subjects { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("divisions")]
public System.Collections.Generic.ICollection<DivisionDto> Divisions { get; set; }
[System.Text.Json.Serialization.JsonPropertyName("studyGroupSubjects")]
public System.Collections.Generic.ICollection<StudyGroupSubjectDto> StudyGroupSubjects { get; set; }


}
Expand Down Expand Up @@ -499,6 +565,24 @@ public partial class StudyGroupDto
public System.Collections.Generic.ICollection<StudentDto> Students { get; set; }


}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.4.4.0 (Newtonsoft.Json v11.0.0.0)")]
public partial class StudyGroupSubjectDto
{
[System.Text.Json.Serialization.JsonPropertyName("id")]
public int Id { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("studyGroup")]
public StudyGroupDto StudyGroup { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("subject")]
public SubjectDto Subject { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("mentorIds")]
public System.Collections.Generic.ICollection<int> MentorIds { get; set; }


}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.4.4.0 (Newtonsoft.Json v11.0.0.0)")]
Expand Down
19 changes: 17 additions & 2 deletions Source/SeaInk.Endpoints/Server/Controllers/MentorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public MentorController(DatabaseContext databaseContext)
public ActionResult<MentorDto> GetCurrentMentor()
{
Mentor mentor = _databaseContext.Mentors
.MaxBy(m => m.Divisions.Count)
.MaxBy(m => m.StudyGroupSubjects.Count)
.First();
return Ok(mentor.ToDto());
}
Expand All @@ -45,13 +45,28 @@ public ActionResult<List<SubjectDto>> GetSubjects(int mentorId)
if (mentor is null)
return NotFound();

List<SubjectDto> subjects = mentor.Divisions
List<SubjectDto> subjects = mentor.StudyGroupSubjects
.Select(d => d.Subject)
.DistinctBy(s => s.Id)
.Select(s => s.ToDto())
.ToList();

return Ok(subjects);
}

[HttpGet("{mentorId:int}/divisions")]
public ActionResult<List<DivisionDto>> GetDivisions(int mentorId)
{
Mentor mentor = _databaseContext.Mentors.Find(mentorId);
if (mentor is null)
return NotFound();

List<DivisionDto> divisions = _databaseContext.Divisions
.Where(d => d.StudyGroupSubjects.Any(sgs => sgs.Mentors.Any(m => m.Id == mentorId)))
.Select(d => d.ToDto())
.ToList();

return Ok(divisions);
}
}
}
Loading