// TEST: files for solutions // done public async Task <OperationDetailDTO <TaskDTO> > GetTaskByIDAsync(int id) { var detail = new OperationDetailDTO <TaskDTO>(); var resultTaskDTO = new TaskDTO(); var resSolutionDTOList = new List <SolutionDTO>(); var resStudentDTOList = new List <StudentDTO>(); try { var entity = await _context.TaskModels .Include(t => t.Group) .Include(t => t.TaskStudentItems) .Include(t => t.Solutions) .Include(t => t.Subject) .Include(t => t.Type) .Include(t => t.File) .Include(t => t.Teacher) .FirstOrDefaultAsync(t => t.Id == id); if (entity == null) { detail.ErrorMessages.Add("Задача не найдена."); return(detail); } foreach (var ts in entity.TaskStudentItems) { var studentEntuty = await _context.Students.FindAsync(ts.StudentId); resStudentDTOList.Add(StudentDTO.Map(studentEntuty)); } // [files] foreach (var solution in entity.Solutions) { var solutionEntity = await _context.Solutions .Include(s => s.File) .Where(s => s == solution) .FirstOrDefaultAsync(); resSolutionDTOList.Add(SolutionDTO.Map(solutionEntity)); } resultTaskDTO = TaskDTO.Map(entity); _taskService.GetCurrentTimePercentage(ref resultTaskDTO); resultTaskDTO.Solutions = resSolutionDTOList; resultTaskDTO.Students = resStudentDTOList; detail.Data = resultTaskDTO; detail.Succeeded = true; return(detail); } catch (Exception e) { detail.ErrorMessages.Add(_serverErrorMessage + e.Message); return(detail); } }
public async Task <OperationDetailDTO <TaskDTO> > GetTaskByIDAsync(int id) { var detail = new OperationDetailDTO <TaskDTO>(); var resultTaskDTO = new TaskDTO(); SolutionDTO resSolutionDTO = null; Solution solutionEntity; try { var currentUserEntity = await GetUserFromClaimsAsync(); var studentEntity = await _context.Students .Include(s => s.User) .Include(s => s.Solutions) .Where(s => s.UserId == currentUserEntity.Id) .FirstOrDefaultAsync(); var taskEntity = await _context.TaskModels .Include(t => t.Group) .Include(t => t.Solutions) .Include(t => t.Subject) .Include(t => t.Type) .Include(t => t.File) .Include(t => t.Teacher) .Include(t => t.TaskStudentItems) .FirstOrDefaultAsync(t => t.Id == id); if (taskEntity == null) { detail.ErrorMessages.Add("Задача не найдена."); return(detail); } resultTaskDTO = TaskDTO.Map(taskEntity); _taskService.GetCurrentTimePercentage(ref resultTaskDTO); solutionEntity = await _context.Solutions .Include(s => s.Student) .Include(s => s.File) .Where(s => s.TaskId == id) .Where(s => s.StudentId == studentEntity.Id) .FirstOrDefaultAsync(); if (solutionEntity != null) { resSolutionDTO = SolutionDTO.Map(solutionEntity); } resultTaskDTO.Solution = resSolutionDTO; resultTaskDTO.Solutions = null; detail.Data = resultTaskDTO; detail.Succeeded = true; return(detail); } catch (Exception e) { detail.ErrorMessages.Add(_serverErrorMessage + e.Message); return(detail); } }
public async Task <OperationDetailDTO <List <TaskDTO> > > GetTasksFromDBAsync(FilterDTO[] filters = null) { var detail = new OperationDetailDTO <List <TaskDTO> >(); var resultList = new List <TaskDTO>(); try { var currentUserEntity = await GetUserFromClaimsAsync(); var studentEntity = await _context.Students .Include(s => s.User) .Where(s => s.UserId == currentUserEntity.Id) .FirstOrDefaultAsync(); var tasks = from t in _context.TaskModels .Include(t => t.Teacher) .Include(t => t.Subject) .Include(t => t.Type) .Include(t => t.Solutions) .Where(t => (t.TaskStudentItems.FirstOrDefault(x => x.StudentId == studentEntity.Id) != null)) select t; tasks.OrderBy(t => t.BeginDate); if (filters != null) { foreach (var filter in filters) { switch (filter.Name) { case "subjectId": { var value = Convert.ToInt32(filter.Value); if (value > 0) { tasks = tasks.Where(t => t.SubjectId == value); } break; } case "typeId": { var value = Convert.ToInt32(filter.Value); if (value > 0) { tasks = tasks.Where(t => t.TypeId == value); } break; } case "searchString": { var value = filter.Value; if (!String.IsNullOrEmpty(value)) { tasks = tasks.Where(t => t.Name.ToUpper().Contains(value) || t.ContentText.ToUpper().Contains(value)); } break; } case "isOpen": { var value = filter.Value; if (!String.IsNullOrEmpty(value)) { if (value == "true") { tasks = tasks .Where(t => t.IsOpen); } if (value == "false") { tasks = tasks .Where(t => !t.IsOpen); } } break; } } } } foreach (var entity in tasks) { var solution = new Solution(); SolutionDTO currentSolution = null; if (entity.Solutions.Count > 0) { if ((solution = entity.Solutions.Where(s => (s.StudentId == studentEntity.Id)).FirstOrDefault()) != null) { currentSolution = SolutionDTO.Map(solution); } } entity.Solutions = null; var taskDTO = TaskDTO.Map(entity); taskDTO.Solution = currentSolution; resultList.Add(taskDTO); } detail.Succeeded = true; detail.Data = resultList; return(detail); } catch (Exception e) { detail.Succeeded = false; detail.ErrorMessages.Add(_serverErrorMessage + e.Message); return(detail); } }
// test // сразу формировать DTO public async Task <OperationDetailDTO <List <SubjectDTO> > > GetMainDataAsync() { var detail = new OperationDetailDTO <List <SubjectDTO> >(); var resSubjectDTOList = new List <SubjectDTO>(); try { var currentUserEntity = await GetUserFromClaimsAsync(); var currentTeacher = await _context.Teachers .Include(t => t.User) .Include(t => t.Department) .ThenInclude(d => d.Faculty) .Where(t => t.UserId == currentUserEntity.Id) .FirstOrDefaultAsync(); // все задачи текущего преподавателя IQueryable <TaskModel> teacherTaskQueryList = from t in _context.TaskModels .Include(t => t.Teacher) .Include(t => t.Group) .ThenInclude(g => g.Tasks) .Include(t => t.Group) .ThenInclude(g => g.Students) .Include(t => t.Subject) .Include(t => t.Type) .Include(t => t.Solutions) .Include(t => t.TaskStudentItems) .Where(t => t.TeacherId == currentTeacher.Id) select t; // все предметы, по которым есть задачи IQueryable <Subject> subjectQueryList_ = from s in _context.Subjects .Include(s => s.Tasks) .Where(s => s.Tasks.Count > 0) select s; // формируем список сущностей предметов var resSubjectEntityList = new List <Subject>(); var resGroupEntityList = new List <Group>(); // из всех задач препода получить список предметов по которым у препода есть задачи foreach (var task in teacherTaskQueryList) { SubjectDTO currentSubjectDTO; GroupDTO currentGroupDTO; var newStudentDTO = new StudentDTO(); if ((currentSubjectDTO = resSubjectDTOList.FirstOrDefault(s => s.Id == task.SubjectId)) != null) { if ((currentGroupDTO = currentSubjectDTO.Groups.FirstOrDefault(g => g.Id == task.GroupId)) != null) { if (currentGroupDTO.Students != null) { foreach (var student in currentGroupDTO.Students) { // get every student solution for current task var ts = new TaskStudentItem(); var solution = new Solution(); if ((ts = task.TaskStudentItems.FirstOrDefault(ts => (ts.StudentId == student.Id) && (ts.TaskId == task.Id))) != null) { var taskDTO = TaskDTO.Map(task); var solEnt = task.Solutions.Where(s => s.StudentId == student.Id).FirstOrDefault(); if (solEnt != null) { taskDTO.Solution = SolutionDTO.Map(solEnt); } student.Tasks.Add(taskDTO); } } } } else { currentGroupDTO = GroupDTO.Map(task.Group); // currentSubjectDTO.Groups.Add(currentGroupDTO); if (currentGroupDTO.Students != null) { foreach (var student in currentGroupDTO.Students) { var ts = new TaskStudentItem(); var solution = new Solution(); if ((ts = task.TaskStudentItems.FirstOrDefault(ts => (ts.StudentId == student.Id) && (ts.TaskId == task.Id))) != null) { var taskDTO = TaskDTO.Map(task); var solEnt = task.Solutions.Where(s => s.StudentId == student.Id).FirstOrDefault(); if (solEnt != null) { taskDTO.Solution = SolutionDTO.Map(solEnt); } student.Tasks.Add(taskDTO); } } } } } else { currentGroupDTO = GroupDTO.Map(task.Group); currentSubjectDTO = SubjectDTO.Map(task.Subject); // наполняем студентов группы заданиями и решениями foreach (var student in currentGroupDTO.Students) { var ts = new TaskStudentItem(); if ((ts = task.TaskStudentItems.FirstOrDefault(ts => (ts.StudentId == student.Id) && (ts.TaskId == task.Id))) != null) { var taskDTO = TaskDTO.Map(task); var solEnt = task.Solutions.Where(s => s.StudentId == student.Id).FirstOrDefault(); if (solEnt != null) { taskDTO.Solution = SolutionDTO.Map(solEnt); } student.Tasks.Add(taskDTO); } } currentSubjectDTO.Groups.Add(currentGroupDTO); resSubjectDTOList.Add(currentSubjectDTO); } } detail.Succeeded = true; detail.Data = resSubjectDTOList; return(new OperationDetailDTO <List <SubjectDTO> > { Data = resSubjectDTOList, Succeeded = true }); } catch (Exception e) { detail.Succeeded = false; detail.ErrorMessages.Add(_serverErrorMessage + e.Message); return(detail); } }