-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCoursesController.cs
More file actions
270 lines (232 loc) · 10.2 KB
/
CoursesController.cs
File metadata and controls
270 lines (232 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using HwProj.CoursesService.API.Filters;
using HwProj.CoursesService.API.Models;
using HwProj.CoursesService.API.Services;
using HwProj.Models.CoursesService.ViewModels;
using HwProj.Utils.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Net;
using HwProj.AuthService.Client;
using HwProj.CoursesService.API.Repositories;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.CoursesService.DTO;
using Microsoft.EntityFrameworkCore;
using HwProj.CoursesService.API.Domains;
using HwProj.Models.CoursesService;
using HwProj.Models.Roles;
namespace HwProj.CoursesService.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class CoursesController : Controller
{
private readonly ICoursesService _coursesService;
private readonly ICourseFilterService _courseFilterService;
private readonly IHomeworksRepository _homeworksRepository;
private readonly IMapper _mapper;
public CoursesController(ICoursesService coursesService,
IHomeworksRepository homeworksRepository,
IMapper mapper,
ICourseFilterService courseFilterService)
{
_coursesService = coursesService;
_homeworksRepository = homeworksRepository;
_mapper = mapper;
_courseFilterService = courseFilterService;
}
[HttpGet]
public async Task<CoursePreview[]> GetAll()
{
var coursesFromDb = await _coursesService.GetAllAsync();
var courses = coursesFromDb.Select(c => c.ToCoursePreview()).ToArray();
return courses;
}
[CourseDataFilter]
[HttpGet("{courseId}")]
public async Task<IActionResult> Get(long courseId)
{
var userId = Request.GetUserIdFromHeader();
var course = await _coursesService.GetAsync(courseId, userId);
if (course == null) return NotFound();
return Ok(course);
}
[HttpGet("getForMentor/{courseId}/{mentorId}")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> GetForMentor(long courseId, string mentorId)
{
var course = await _coursesService.GetAsync(courseId, mentorId);
if (course == null) return NotFound();
return Ok(course);
}
[HttpGet("getAllData/{courseId}/")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> GetAllData(long courseId)
{
var course = await _coursesService.GetAsync(courseId);
if (course == null) return NotFound();
return Ok(course);
}
[CourseDataFilter]
[HttpGet("getByTask/{taskId}")]
public async Task<IActionResult> GetByTask(long taskId)
{
var userId = Request.GetUserIdFromHeader();
var course = await _coursesService.GetByTaskAsync(taskId, userId);
if (course == null) return NotFound();
return Ok(course);
}
[HttpPost("create")]
public async Task<IActionResult> AddCourse([FromBody] CreateCourseViewModel courseViewModel)
{
var mentorId = Request.GetUserIdFromHeader();
CourseDTO? baseCourse = null;
if (courseViewModel.BaseCourseId != null)
{
baseCourse = await _coursesService.GetForEditingAsync((long)courseViewModel.BaseCourseId);
if (baseCourse == null) return NotFound();
if (!baseCourse.MentorIds.Contains(mentorId)) return Forbid();
}
var courseId = await _coursesService.AddAsync(courseViewModel, baseCourse, mentorId);
return Ok(courseId);
}
[HttpDelete("{courseId}")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> DeleteCourse(long courseId)
{
await _coursesService.DeleteAsync(courseId);
return Ok();
}
[HttpPost("update/{courseId}")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> UpdateCourse(long courseId, [FromBody] UpdateCourseViewModel courseViewModel)
{
await _coursesService.UpdateAsync(courseId, courseViewModel);
return Ok();
}
[HttpPost("signInCourse/{courseId}")]
public async Task<IActionResult> SignInCourse(long courseId, [FromQuery] string studentId)
{
return await _coursesService.AddStudentAsync(courseId, studentId)
? Ok() as IActionResult
: NotFound();
}
[HttpPost("acceptStudent/{courseId}")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> AcceptStudent(long courseId, [FromQuery] string studentId)
{
return await _coursesService.AcceptCourseMateAsync(courseId, studentId)
? Ok() as IActionResult
: NotFound();
}
[HttpPost("updateCharacteristics")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> UpdateStudentCharacteristics([FromQuery] long courseId,
[FromQuery] string studentId,
[FromBody] StudentCharacteristicsDto characteristics)
{
return await _coursesService.UpdateStudentCharacteristics(courseId, studentId, characteristics)
? Ok() as IActionResult
: NotFound();
}
[HttpPost("rejectStudent/{courseId}")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> RejectStudent(long courseId, [FromQuery] string studentId)
{
return await _coursesService.RejectCourseMateAsync(courseId, studentId)
? Ok() as IActionResult
: NotFound();
}
[CourseDataFilter]
[HttpGet("userCourses")]
public async Task<CourseDTO[]> GetUserCourses(string role)
{
var userId = Request.GetUserIdFromHeader();
var courses = await _coursesService.GetUserCoursesAsync(userId, role);
return courses;
}
[HttpGet("acceptLecturer/{courseId}")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> AcceptLecturer(long courseId, [FromQuery] string lecturerEmail,
[FromQuery] string lecturerId)
{
await _coursesService.AcceptLecturerAsync(courseId, lecturerEmail, lecturerId);
return Ok();
}
[HttpGet("getLecturersAvailableForCourse/{courseId}")]
[ProducesResponseType(typeof(AccountDataDto[]), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetLecturersAvailableForCourse(long courseId)
{
var mentorId = Request.GetMentorId();
var result = await _coursesService.GetLecturersAvailableForCourse(courseId, mentorId);
return result == null
? NotFound() as IActionResult
: Ok(result);
}
[HttpGet("getCourseLecturers/{courseId}")]
[ProducesResponseType(typeof(string[]), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetCourseLecturersIds(long courseId)
{
var result = await _coursesService.GetCourseLecturers(courseId);
return Ok(result);
}
//TODO: optimize
[HttpGet("taskDeadlines")]
public async Task<TaskDeadlineDto[]> GetUserDeadlines()
{
var userId = Request.GetUserIdFromHeader();
var courses = await _coursesService.GetUserCoursesAsync(userId, Roles.StudentRole);
var currentDate = DateTime.UtcNow;
//TODO: Move to service
var result = courses
.SelectMany(course => course.Homeworks
.SelectMany(x => x.Tasks)
.Where(t =>
(t.HasDeadline ?? false)
&& t.PublicationDate <= currentDate
&& (t.DeadlineDate >= currentDate || !(t.IsDeadlineStrict ?? true))
&& !(t.Tags.Contains(HomeworkTags.Test) && t.DeadlineDate <= currentDate))
.Select(task => new TaskDeadlineDto
{
TaskId = task.Id,
CourseId = course.Id,
HomeworkId = task.HomeworkId,
TaskTitle = task.Title,
Tags = task.Tags,
CourseTitle = course.Name + " / " + course.GroupName,
PublicationDate = task.PublicationDate ?? DateTime.MinValue,
MaxRating = task.MaxRating,
DeadlineDate = task.DeadlineDate!.Value
}))
.OrderBy(t => t.DeadlineDate)
.ToArray();
return result;
}
[HttpGet("getAllTagsForCourse/{courseId}")]
[ProducesResponseType(typeof(string[]), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetAllTagsForCourse(long courseId)
{
var homeworks = await _homeworksRepository
.FindAll(t => t.CourseId == courseId)
.ToListAsync();
var result = homeworks
.SelectMany(hw => hw.Tags?.Split(';') ?? Array.Empty<string>())
.Where(t => !string.IsNullOrEmpty(t))
.ToArray();
var defaultTags = new[] { HomeworkTags.Test, HomeworkTags.BonusTask, HomeworkTags.GroupWork };
result = result.Concat(defaultTags).Distinct().ToArray();
return Ok(result);
}
[HttpGet("getMentorsToStudents/{courseId}")]
[ProducesResponseType(typeof(MentorToAssignedStudentsDTO), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetMentorsToAssignedStudents(long courseId)
{
var mentorIds = await _coursesService.GetCourseLecturers(courseId);
var mentorsToAssignedStudents = await _courseFilterService.GetAssignedStudentsIds(courseId, mentorIds);
return Ok(mentorsToAssignedStudents);
}
}
}