PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Gradebook.Data/Repositories/GradeRepository.cs

https://bitbucket.org/academium/gradebook
C# | 192 lines | 154 code | 38 blank | 0 comment | 68 complexity | d8c5a7659fc0971fe6b6a588572bb40d MD5 | raw file
  1. using System.Collections.Generic;
  2. using Gradebook.Contracts.Repositories;
  3. using Gradebook.Model;
  4. using System;
  5. using System.Data.Entity;
  6. using System.Linq;
  7. namespace Gradebook.Data.Repositories
  8. {
  9. public class GradeRepository : RepositoryBase<Grade>, IGradeRepository
  10. {
  11. public GradeRepository(DbContext dbContext) : base(dbContext) { }
  12. public int GetAverageByTask(int taskId)
  13. {
  14. if (!DbContext.Set<Task>().Any(x => x.Id == taskId))
  15. throw new ArgumentException("taskId");
  16. if (!DbSet.Any(x => x.TaskId == taskId)) return 0;
  17. return (int)Math.Round(DbSet.Where(x => x.TaskId == taskId).Average(x => x.Value));
  18. }
  19. public int GetRate(int taskId, int studentId)
  20. {
  21. if (!DbContext.Set<Task>().Any(x => x.Id == taskId))
  22. throw new ArgumentException("taskId");
  23. if (!DbContext.Set<Student>().Any(x => x.Id == studentId))
  24. throw new ArgumentException("studentId");
  25. if (!Contains(taskId, studentId))
  26. throw new ArgumentException();
  27. var total = DbContext.Set<Task>().Single(x => x.Id == taskId).TotalMarks;
  28. var grade = DbSet.Single(x => x.TaskId == taskId && x.StudentId == studentId).Value;
  29. var rank = (int)Math.Round((double)grade * 100 / total);
  30. return rank;
  31. }
  32. public int GetValue(int taskId, int studentId)
  33. {
  34. if (!Contains(taskId, studentId)) return 0;
  35. return Read(taskId, studentId).Value;
  36. }
  37. public int GetRank(int taskId, int studentId)
  38. {
  39. if (!DbContext.Set<Task>().Any(x => x.Id == taskId))
  40. throw new ArgumentException("taskId");
  41. if (!DbContext.Set<Student>().Any(x => x.Id == studentId))
  42. throw new ArgumentException("studentId");
  43. if (!Contains(taskId, studentId))
  44. throw new ArgumentException();
  45. var grade = DbSet.Single(x => x.TaskId == taskId && x.StudentId == studentId);
  46. return DbSet.Where(x => x.TaskId == taskId)
  47. .OrderBy(x => x.Value)
  48. .ToList()
  49. .IndexOf(grade) + 1;
  50. }
  51. public IEnumerable<Grade> GetByTask(int taskId)
  52. {
  53. if (!DbContext.Set<Task>().Any(x => x.Id == taskId))
  54. throw new ArgumentException("taskId");
  55. return DbSet.Include(x => x.Student).Where(x => x.TaskId == taskId);
  56. }
  57. public IEnumerable<Grade> GetAllByTask(int taskId)
  58. {
  59. if (!DbContext.Set<Task>().Any(x => x.Id == taskId))
  60. throw new ArgumentException("taskId");
  61. var students = DbContext.Set<Task>().Include(x => x.Code).Include(x => x.Code.TeacherClassCodes)
  62. .Where(x => x.Id == taskId)
  63. .Select(x => x.Code)
  64. .SelectMany(x => x.TeacherClassCodes)
  65. .SelectMany(x => x.StudentClassCodes)
  66. .Select(x => x.Student)
  67. .ToList();
  68. foreach (var student in students)
  69. {
  70. if (Contains(taskId, student.Id))
  71. {
  72. yield return DbSet.Include(x => x.Task.Type).Include(x => x.Task.Code.TeacherClassCodes)
  73. .Single(x => x.TaskId == taskId && x.StudentId == student.Id);
  74. }
  75. else
  76. {
  77. yield return new Grade
  78. {
  79. TaskId = taskId,
  80. StudentId = student.Id,
  81. Student = student,
  82. Task = DbContext.Set<Task>().Include(x => x.Type).Include(x => x.Code.TeacherClassCodes).Single(x => x.Id == taskId)
  83. };
  84. }
  85. }
  86. }
  87. public IEnumerable<Grade> GetByClassCode(int codeId, int termId = 0)
  88. {
  89. if (!DbContext.Set<Class>().Any(x => x.Id == codeId))
  90. throw new ArgumentException("codeId");
  91. var grades = DbSet.Include(x => x.Student)
  92. .Include(x => x.Task.Type)
  93. .Include(x => x.Task.Code.TeacherClassCodes)
  94. .Where(x => x.Task.CodeId == codeId);
  95. if (termId != 0)
  96. {
  97. grades = grades.Where(x => x.Task.Code.TeacherClassCodes.Any(y => y.TermId == termId));
  98. }
  99. return grades;
  100. }
  101. public IEnumerable<Grade> GetAllByClassCode(int codeId, int termId = 0, int typeId = 0)
  102. {
  103. if (!DbContext.Set<Class>().Any(x => x.Id == codeId))
  104. throw new ArgumentException("codeId");
  105. var tasks = DbContext.Set<Task>().Include(x => x.Code.TeacherClassCodes).Where(x => x.CodeId == codeId);
  106. if (termId != 0)
  107. {
  108. tasks = tasks.Where(x => x.Code.TeacherClassCodes.Any(y => y.TermId == termId));
  109. }
  110. if (typeId != 0)
  111. {
  112. tasks = tasks.Where(x => x.TypeId == typeId);
  113. }
  114. var grades = new List<Grade>();
  115. foreach (var id in tasks.Select(x => x.Id).ToList())
  116. {
  117. grades.AddRange(GetAllByTask(id));
  118. }
  119. return grades;
  120. }
  121. public IEnumerable<Grade> GetAllByStudent(int studentId, int classId = 0)
  122. {
  123. return classId == 0
  124. ? DbSet.Include(x => x.Task.Type).Include(x => x.Student).Where(x => x.StudentId == studentId)
  125. : GetAllByClassCode(classId).Where(x => x.StudentId == studentId);
  126. }
  127. public Grade Create(int taskId, int studentId, int value, string comments = null)
  128. {
  129. if (Contains(taskId, studentId))
  130. throw new ArgumentException();
  131. if (!DbContext.Set<Task>().Any(x => x.Id == taskId))
  132. throw new ArgumentException("taskId");
  133. if (!DbContext.Set<Student>().Any(x => x.Id == taskId))
  134. throw new ArgumentException("studentId");
  135. if (DbContext.Set<Task>().Single(x => x.Id == taskId).TotalMarks < value || value < 0)
  136. throw new ArgumentOutOfRangeException("value");
  137. return DbSet.Add(new Grade { TaskId = taskId, StudentId = studentId, Value = value, Comments = comments });
  138. }
  139. public Grade Read(int taskId, int studentId)
  140. {
  141. if (!Contains(taskId, studentId))
  142. throw new ArgumentException();
  143. return DbSet.Single(x => x.TaskId == taskId && x.StudentId == studentId);
  144. }
  145. public bool Contains(int taskId, int studentId)
  146. {
  147. return DbSet.Any(x => x.TaskId == taskId && x.StudentId == studentId);
  148. }
  149. public override IQueryable<Grade> GetAll()
  150. {
  151. return DbSet.Include(x => x.Student);
  152. }
  153. }
  154. }