-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCoursesController.cs
More file actions
160 lines (140 loc) · 6.03 KB
/
CoursesController.cs
File metadata and controls
160 lines (140 loc) · 6.03 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
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using HwProj.APIGateway.API.Filters;
using HwProj.APIGateway.API.Models;
using HwProj.AuthService.Client;
using HwProj.CoursesService.Client;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.CoursesService.ViewModels;
using HwProj.Models.Roles;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace HwProj.APIGateway.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CoursesController : AggregationController
{
private readonly ICoursesServiceClient _coursesClient;
public CoursesController(ICoursesServiceClient coursesClient, IAuthServiceClient authServiceClient) : base(
authServiceClient)
{
_coursesClient = coursesClient;
}
[HttpGet]
[Authorize]
public async Task<CoursePreviewView[]> GetAllCourses()
{
var courses = await _coursesClient.GetAllCourses();
var result = await GetCoursePreviews(courses);
return result;
}
[CourseDataFilter]
[HttpGet("{courseId}")]
[ProducesResponseType(typeof(CourseViewModel), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetCourseData(long courseId)
{
var course = await _coursesClient.GetCourseById(courseId);
if (course == null) return NotFound();
var studentIds = course.CourseMates.Select(t => t.StudentId).ToArray();
var getStudentsTask = AuthServiceClient.GetAccountsData(studentIds);
var getMentorsTask = AuthServiceClient.GetAccountsData(course.MentorIds);
await Task.WhenAll(getStudentsTask, getMentorsTask);
var students = getStudentsTask.Result;
var acceptedStudents = new List<AccountDataDto>();
var newStudents = new List<AccountDataDto>();
for (var i = 0; i < students.Length; i++)
{
if (!(students[i] is { } student)) continue;
if (course.CourseMates[i].IsAccepted) acceptedStudents.Add(student);
else newStudents.Add(student);
}
var result = new CourseViewModel
{
Id = courseId,
Name = course.Name,
GroupName = course.GroupName,
Mentors = getMentorsTask.Result.Where(t => t != null).ToArray(),
AcceptedStudents = acceptedStudents.ToArray(),
NewStudents = newStudents.ToArray(),
Homeworks = course.Homeworks,
IsCompleted = course.IsCompleted,
IsOpen = course.IsOpen,
};
return Ok(result);
}
[HttpDelete("{courseId}")]
[Authorize(Roles = Roles.LecturerRole)]
public async Task<IActionResult> DeleteCourse(long courseId)
{
await _coursesClient.DeleteCourse(courseId);
return Ok();
}
[HttpPost("create")]
[Authorize(Roles = Roles.LecturerRole)]
[ProducesResponseType(typeof(long), (int)HttpStatusCode.OK)]
public async Task<IActionResult> CreateCourse(CreateCourseViewModel model)
{
var result = await _coursesClient.CreateCourse(model, UserId);
return Ok(result);
}
[HttpPost("update/{courseId}")]
[Authorize(Roles = Roles.LecturerRole)]
public async Task<IActionResult> UpdateCourse(UpdateCourseViewModel model, long courseId)
{
await _coursesClient.UpdateCourse(model, courseId);
return Ok();
}
[HttpPost("signInCourse/{courseId}")]
[Authorize(Roles = Roles.StudentRole)]
public async Task<IActionResult> SignInCourse(long courseId)
{
await _coursesClient.SignInCourse(courseId, UserId);
return Ok();
}
[HttpPost("acceptStudent/{courseId}/{studentId}")]
[Authorize(Roles = Roles.LecturerRole)]
public async Task<IActionResult> AcceptStudent(long courseId, string studentId)
{
await _coursesClient.AcceptStudent(courseId, studentId);
return Ok();
}
[HttpPost("rejectStudent/{courseId}/{studentId}")]
[Authorize(Roles = Roles.LecturerRole)]
public async Task<IActionResult> RejectStudent(long courseId, string studentId)
{
await _coursesClient.RejectStudent(courseId, studentId);
return Ok();
}
[HttpGet("userCourses")]
[Authorize]
public async Task<CoursePreviewView[]> GetAllUserCourses()
{
var userCourses = await _coursesClient.GetAllUserCourses();
var result = await GetCoursePreviews(userCourses);
return result;
}
[HttpGet("acceptLecturer/{courseId}/{lecturerEmail}")]
[Authorize(Roles = Roles.LecturerRole)]
public async Task<IActionResult> AcceptLecturer(long courseId, string lecturerEmail)
{
var lecturer = await AuthServiceClient.GetAccountDataByEmail(lecturerEmail);
if (lecturer == null) return NotFound("Преподаватель с такой почтой не найден");
if (lecturer.Role != Roles.LecturerRole) return BadRequest("Пользователь не является преподавателем");
var result = await _coursesClient.AcceptLecturer(courseId, lecturerEmail, lecturer.UserId);
return result.Succeeded
? Ok(result) as IActionResult
: BadRequest(result.Errors);
}
[HttpGet("getLecturersAvailableForCourse/{courseId}")]
[Authorize(Roles = Roles.LecturerRole)]
[ProducesResponseType(typeof(AccountDataDto[]), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetLecturersAvailableForCourse(long courseId)
{
var result = await _coursesClient.GetLecturersAvailableForCourse(courseId);
return Ok(result.Value);
}
}
}