/Gradebook.Data/Repositories/TaskRepository.cs
C# | 102 lines | 81 code | 21 blank | 0 comment | 22 complexity | 5650f61dffc98d3e248283585153dc06 MD5 | raw file
- using Gradebook.Contracts.Repositories;
- using Gradebook.Model;
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
-
- namespace Gradebook.Data.Repositories
- {
- public class TaskRepository : Repository<Task>, ITaskRepository
- {
- public TaskRepository(DbContext dbContext) : base(dbContext) { }
-
- public override Task Read(int id)
- {
- if (!Contains(id))
- throw new ArgumentException("id");
-
- return DbSet.Include(x => x.Assessment).Include(x => x.Type).Single(x => x.Id == id);
- }
-
- public IEnumerable<Task> GetByAssessment(int assessmentId)
- {
- if (!DbContext.Set<Task>().Any(x => x.Id == assessmentId))
- throw new ArgumentException("assessmentId");
-
- return DbSet.Include(x => x.Assessment).Where(x => x.AssessmentId == assessmentId);
- }
-
- public IEnumerable<Task> GetByStudent(int studentId)
- {
- if (!DbContext.Set<Student>().Any(x => x.Id == studentId))
- throw new ArgumentException("studentId");
-
- var student = DbContext.Set<Student>()
- .Include(x => x.ClassCodes.Select(y => y.Code).Select(y => y.Code).Select(y => y.Tasks))
- .Include(x => x.ClassCodes.Select(y => y.Code).Select(y => y.Code).Select(y => y.Subject).Select(y => y.TaskTypes))
- .Single(x => x.Id == studentId);
-
- return student.ClassCodes
- .Select(x => x.Code)
- .Select(x => x.Code)
- .SelectMany(x => x.Tasks);
- }
-
- public IEnumerable<Task> GetByCode(int codeId)
- {
- if (!DbContext.Set<ClassCode>().Any(x => x.Id == codeId))
- throw new ArgumentException("codeId");
-
- return GetAll().Where(x => x.CodeId == codeId);
- }
-
- public IEnumerable<Task> GetByTerm(int termId)
- {
- if (!DbContext.Set<Term>().Any(x => x.Id == termId))
- throw new ArgumentException("termId");
-
- return DbSet
- .Include(x => x.Code)
- .Include(x => x.Assessment)
- .Include(x => x.Type)
- .Where(x => x.Code.TeacherClassCodes.Any(y => y.TermId == termId));
- }
-
- public IEnumerable<Task> GetLinked(int taskId)
- {
- if (!Contains(taskId))
- throw new ArgumentException("taskId");
-
- var task = DbSet.Include(x => x.Assessment).Single(x => x.Id == taskId);
-
- if (!task.AssessmentId.HasValue) return new Task[0];
-
- return task.Assessment.Tasks;
- }
-
- public int GetStatus(int taskId)
- {
- if (!Contains(taskId))
- throw new ArgumentException("taskId");
-
- var task = DbSet
- .Include(x => x.Grades)
- .Include(x => x.Code.TeacherClassCodes.Select(o => o.StudentClassCodes))
- .Single(x => x.Id == taskId);
-
- var studentCount = task.Code.TeacherClassCodes
- .Where(x => x.ClassCodeId == task.CodeId)
- .SelectMany(x => x.StudentClassCodes)
- .Count();
- if (studentCount == 0) return 0;
-
- return task.Grades.Count / studentCount;
- }
-
- public override IQueryable<Task> GetAll()
- {
- return DbSet.Include(x => x.Assessment).Include(x => x.Type);
- }
- }
- }