@@ -20,16 +20,17 @@ public class TasksController : Controller
20
20
{
21
21
private readonly ITasksService _tasksService ;
22
22
private readonly IHomeworksService _homeworksService ;
23
- private readonly ITaskQuestionsRepository _questionsRepository ;
23
+ private readonly ITaskQuestionsService _taskQuestionsService ;
24
24
private readonly ICoursesService _coursesService ;
25
25
26
26
public TasksController ( ITasksService tasksService , ICoursesService coursesService ,
27
- IHomeworksService homeworksService , ITaskQuestionsRepository questionsRepository )
27
+ IHomeworksService homeworksService , ITaskQuestionsRepository questionsRepository ,
28
+ ITaskQuestionsService taskQuestionsService )
28
29
{
29
30
_tasksService = tasksService ;
30
31
_coursesService = coursesService ;
31
32
_homeworksService = homeworksService ;
32
- _questionsRepository = questionsRepository ;
33
+ _taskQuestionsService = taskQuestionsService ;
33
34
}
34
35
35
36
[ HttpGet ( "get/{taskId}" ) ]
@@ -114,7 +115,7 @@ public async Task<IActionResult> AddQuestionForTask([FromBody] AddTaskQuestionDt
114
115
if ( ! await _coursesService . HasStudent ( task . Homework . CourseId , studentId ) )
115
116
return Forbid ( ) ;
116
117
117
- await _questionsRepository . AddAsync ( new TaskQuestion
118
+ await _taskQuestionsService . AddQuestionAsync ( new TaskQuestion
118
119
{
119
120
TaskId = task . Id ,
120
121
StudentId = studentId ,
@@ -137,10 +138,11 @@ public async Task<IActionResult> GetQuestionsForTask(long taskId)
137
138
if ( ! isLecturer && ! isStudent )
138
139
return Forbid ( ) ;
139
140
140
- var questions = _questionsRepository . FindAll ( x => x . TaskId == taskId ) ;
141
- questions = isLecturer ? questions : questions . Where ( x => ! x . IsPrivate || x . StudentId == userId ) ;
141
+ var questions = isLecturer
142
+ ? await _taskQuestionsService . GetQuestionsForLecturerAsync ( taskId )
143
+ : await _taskQuestionsService . GetStudentQuestionsAsync ( taskId , userId ) ;
142
144
143
- var result = ( await questions . ToListAsync ( ) ) . Select ( x => new GetTaskQuestionDto
145
+ var result = questions . Select ( x => new GetTaskQuestionDto
144
146
{
145
147
Id = x . Id ,
146
148
StudentId = x . StudentId ,
@@ -153,15 +155,15 @@ public async Task<IActionResult> GetQuestionsForTask(long taskId)
153
155
}
154
156
155
157
[ HttpPost ( "addAnswer" ) ]
156
- public async Task < IActionResult > GetQuestionsForTask ( AddAnswerForQuestionDto answer )
158
+ public async Task < IActionResult > AddAnswerForQuestion ( AddAnswerForQuestionDto answer )
157
159
{
158
160
if ( string . IsNullOrEmpty ( answer . Answer ) )
159
161
return BadRequest ( "Текст ответа пуст" ) ;
160
162
161
163
var lecturerId = Request . GetUserIdFromHeader ( ) ;
162
164
if ( lecturerId == null ) return NotFound ( ) ;
163
165
164
- var question = await _questionsRepository . FindAsync ( x => x . Id == answer . QuestionId ) ;
166
+ var question = await _taskQuestionsService . GetQuestionAsync ( answer . QuestionId ) ;
165
167
if ( question == null ) return NotFound ( ) ;
166
168
167
169
var task = await _tasksService . GetTaskAsync ( question . TaskId ) ;
@@ -171,11 +173,7 @@ public async Task<IActionResult> GetQuestionsForTask(AddAnswerForQuestionDto ans
171
173
var isLecturer = ( await _coursesService . GetCourseLecturers ( courseId ) ) . Contains ( lecturerId ) ;
172
174
if ( ! isLecturer ) return Forbid ( ) ;
173
175
174
- await _questionsRepository . UpdateAsync ( question . Id , x => new TaskQuestion
175
- {
176
- LecturerId = lecturerId ,
177
- Answer = answer . Answer
178
- } ) ;
176
+ await _taskQuestionsService . AddAnswerAsync ( question . Id , lecturerId , answer . Answer ) ;
179
177
return Ok ( ) ;
180
178
}
181
179
}
0 commit comments