Skip to content

Commit d98abfe

Browse files
authored
Рефакторинг функциональности вопросов по задачам (#586)
* [backend] refactor: create TaskQuestionsRepository * [backend] refactor: create TaskQuestionsService and ITaskQuestionsService; refactor TasksController (remove repository calls from service)
1 parent b519da3 commit d98abfe

File tree

5 files changed

+84
-22
lines changed

5 files changed

+84
-22
lines changed

HwProj.CoursesService/HwProj.CoursesService.API/Controllers/TasksController.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ public class TasksController : Controller
2020
{
2121
private readonly ITasksService _tasksService;
2222
private readonly IHomeworksService _homeworksService;
23-
private readonly ITaskQuestionsRepository _questionsRepository;
23+
private readonly ITaskQuestionsService _taskQuestionsService;
2424
private readonly ICoursesService _coursesService;
2525

2626
public TasksController(ITasksService tasksService, ICoursesService coursesService,
27-
IHomeworksService homeworksService, ITaskQuestionsRepository questionsRepository)
27+
IHomeworksService homeworksService, ITaskQuestionsRepository questionsRepository,
28+
ITaskQuestionsService taskQuestionsService)
2829
{
2930
_tasksService = tasksService;
3031
_coursesService = coursesService;
3132
_homeworksService = homeworksService;
32-
_questionsRepository = questionsRepository;
33+
_taskQuestionsService = taskQuestionsService;
3334
}
3435

3536
[HttpGet("get/{taskId}")]
@@ -114,7 +115,7 @@ public async Task<IActionResult> AddQuestionForTask([FromBody] AddTaskQuestionDt
114115
if (!await _coursesService.HasStudent(task.Homework.CourseId, studentId))
115116
return Forbid();
116117

117-
await _questionsRepository.AddAsync(new TaskQuestion
118+
await _taskQuestionsService.AddQuestionAsync(new TaskQuestion
118119
{
119120
TaskId = task.Id,
120121
StudentId = studentId,
@@ -137,10 +138,11 @@ public async Task<IActionResult> GetQuestionsForTask(long taskId)
137138
if (!isLecturer && !isStudent)
138139
return Forbid();
139140

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);
142144

143-
var result = (await questions.ToListAsync()).Select(x => new GetTaskQuestionDto
145+
var result = questions.Select(x => new GetTaskQuestionDto
144146
{
145147
Id = x.Id,
146148
StudentId = x.StudentId,
@@ -153,15 +155,15 @@ public async Task<IActionResult> GetQuestionsForTask(long taskId)
153155
}
154156

155157
[HttpPost("addAnswer")]
156-
public async Task<IActionResult> GetQuestionsForTask(AddAnswerForQuestionDto answer)
158+
public async Task<IActionResult> AddAnswerForQuestion(AddAnswerForQuestionDto answer)
157159
{
158160
if (string.IsNullOrEmpty(answer.Answer))
159161
return BadRequest("Текст ответа пуст");
160162

161163
var lecturerId = Request.GetUserIdFromHeader();
162164
if (lecturerId == null) return NotFound();
163165

164-
var question = await _questionsRepository.FindAsync(x => x.Id == answer.QuestionId);
166+
var question = await _taskQuestionsService.GetQuestionAsync(answer.QuestionId);
165167
if (question == null) return NotFound();
166168

167169
var task = await _tasksService.GetTaskAsync(question.TaskId);
@@ -171,11 +173,7 @@ public async Task<IActionResult> GetQuestionsForTask(AddAnswerForQuestionDto ans
171173
var isLecturer = (await _coursesService.GetCourseLecturers(courseId)).Contains(lecturerId);
172174
if (!isLecturer) return Forbid();
173175

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);
179177
return Ok();
180178
}
181179
}

HwProj.CoursesService/HwProj.CoursesService.API/Repositories/ITaskQuestionsRepository.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,4 @@ namespace HwProj.CoursesService.API.Repositories
66
public interface ITaskQuestionsRepository : ICrudRepository<TaskQuestion, long>
77
{
88
}
9-
10-
public class TaskQuestionsRepository : CrudRepository<TaskQuestion, long>, ITaskQuestionsRepository
11-
{
12-
public TaskQuestionsRepository(CourseContext context)
13-
: base(context)
14-
{
15-
}
16-
}
179
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using HwProj.CoursesService.API.Models;
2+
using HwProj.Repositories;
3+
4+
namespace HwProj.CoursesService.API.Repositories
5+
{
6+
public class TaskQuestionsRepository : CrudRepository<TaskQuestion, long>, ITaskQuestionsRepository
7+
{
8+
public TaskQuestionsRepository(CourseContext context)
9+
: base(context)
10+
{
11+
}
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using HwProj.CoursesService.API.Models;
4+
5+
namespace HwProj.CoursesService.API.Services
6+
{
7+
public interface ITaskQuestionsService
8+
{
9+
Task<long> AddQuestionAsync(TaskQuestion taskQuestion);
10+
Task<TaskQuestion> GetQuestionAsync(long taskQuestionId);
11+
Task<List<TaskQuestion>> GetQuestionsForLecturerAsync(long taskId);
12+
Task<List<TaskQuestion>> GetStudentQuestionsAsync(long taskId, string studentId);
13+
Task AddAnswerAsync(long questionId, string lecturerId, string answer);
14+
}
15+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using HwProj.CoursesService.API.Models;
5+
using HwProj.CoursesService.API.Repositories;
6+
using Microsoft.EntityFrameworkCore;
7+
8+
namespace HwProj.CoursesService.API.Services
9+
{
10+
public class TaskQuestionsService : ITaskQuestionsService
11+
{
12+
private readonly ITaskQuestionsRepository _taskQuestionsRepository;
13+
14+
public TaskQuestionsService(ITaskQuestionsRepository taskQuestionsRepository)
15+
{
16+
_taskQuestionsRepository = taskQuestionsRepository;
17+
}
18+
19+
public async Task<long> AddQuestionAsync(TaskQuestion taskQuestion)
20+
=> await _taskQuestionsRepository.AddAsync(taskQuestion);
21+
22+
public async Task<TaskQuestion> GetQuestionAsync(long taskQuestionId)
23+
=> await _taskQuestionsRepository.FindAsync(x => x.Id == taskQuestionId);
24+
25+
public async Task<List<TaskQuestion>> GetQuestionsForLecturerAsync(long taskId)
26+
=> await GetALlTaskQuestions(taskId).ToListAsync();
27+
28+
public Task<List<TaskQuestion>> GetStudentQuestionsAsync(long taskId, string studentId)
29+
{
30+
var allQuestions = GetALlTaskQuestions(taskId);
31+
return allQuestions.Where(x => !x.IsPrivate || x.StudentId == studentId).ToListAsync();
32+
}
33+
34+
public async Task AddAnswerAsync(long questionId, string lecturerId, string answer)
35+
=> await _taskQuestionsRepository.UpdateAsync(questionId, x => new TaskQuestion
36+
{
37+
LecturerId = lecturerId,
38+
Answer = answer
39+
});
40+
41+
private IQueryable<TaskQuestion> GetALlTaskQuestions(long taskId)
42+
=> _taskQuestionsRepository.FindAll(x => x.TaskId == taskId);
43+
}
44+
}

0 commit comments

Comments
 (0)