|
14 | 14 | using Microsoft.AspNetCore.Authorization; |
15 | 15 | using Microsoft.AspNetCore.Mvc; |
16 | 16 |
|
17 | | -namespace HwProj.APIGateway.API.Controllers |
18 | | -{ |
19 | | - [Route("api/[controller]")] |
20 | | - [ApiController] |
21 | | - public class AccountController : AggregationController |
22 | | - { |
23 | | - private readonly ICoursesServiceClient _coursesClient; |
24 | | - private readonly ISolutionsServiceClient _solutionsServiceClient; |
| 17 | +namespace HwProj.APIGateway.API.Controllers; |
25 | 18 |
|
26 | | - public AccountController( |
27 | | - IAuthServiceClient authClient, |
28 | | - ICoursesServiceClient coursesClient, |
29 | | - ISolutionsServiceClient solutionsServiceClient) : base(authClient) |
30 | | - { |
31 | | - _coursesClient = coursesClient; |
32 | | - _solutionsServiceClient = solutionsServiceClient; |
33 | | - } |
| 19 | +[Route("api/[controller]")] |
| 20 | +[ApiController] |
| 21 | +public class AccountController : AggregationController |
| 22 | +{ |
| 23 | + private readonly ICoursesServiceClient _coursesClient; |
| 24 | + private readonly ISolutionsServiceClient _solutionsServiceClient; |
34 | 25 |
|
35 | | - [HttpGet("getUserData/{userId}")] |
36 | | - [ProducesResponseType(typeof(AccountDataDto), (int)HttpStatusCode.OK)] |
37 | | - public async Task<IActionResult> GetUserDataById(string userId) |
38 | | - { |
39 | | - var result = await AuthServiceClient.GetAccountData(userId); |
40 | | - return result == null |
41 | | - ? NotFound() as IActionResult |
42 | | - : Ok(result); |
43 | | - } |
| 26 | + public AccountController( |
| 27 | + IAuthServiceClient authClient, |
| 28 | + ICoursesServiceClient coursesClient, |
| 29 | + ISolutionsServiceClient solutionsServiceClient) : base(authClient) |
| 30 | + { |
| 31 | + _coursesClient = coursesClient; |
| 32 | + _solutionsServiceClient = solutionsServiceClient; |
| 33 | + } |
44 | 34 |
|
45 | | - //TODO: separate for mentor and student |
46 | | - [HttpGet("getUserData")] |
47 | | - [Authorize] |
48 | | - [ProducesResponseType(typeof(UserDataDto), (int)HttpStatusCode.OK)] |
49 | | - public async Task<IActionResult> GetUserData() |
50 | | - { |
51 | | - var getAccountDataTask = AuthServiceClient.GetAccountData(UserId); |
52 | | - var getCoursesTask = _coursesClient.GetAllUserCourses(); |
| 35 | + [HttpGet("getUserData/{userId}")] |
| 36 | + [ProducesResponseType(typeof(AccountDataDto), (int)HttpStatusCode.OK)] |
| 37 | + public async Task<IActionResult> GetUserDataById(string userId) |
| 38 | + { |
| 39 | + var result = await AuthServiceClient.GetAccountData(userId); |
| 40 | + return result == null |
| 41 | + ? NotFound() |
| 42 | + : Ok(result); |
| 43 | + } |
53 | 44 |
|
54 | | - await Task.WhenAll(getAccountDataTask, getCoursesTask); |
| 45 | + //TODO: separate for mentor and student |
| 46 | + [HttpGet("getUserData")] |
| 47 | + [Authorize] |
| 48 | + [ProducesResponseType(typeof(UserDataDto), (int)HttpStatusCode.OK)] |
| 49 | + public async Task<IActionResult> GetUserData() |
| 50 | + { |
| 51 | + var getAccountDataTask = AuthServiceClient.GetAccountData(UserId); |
| 52 | + var getCoursesTask = _coursesClient.GetAllUserCourses(); |
55 | 53 |
|
56 | | - var courses = GetCoursePreviews(getCoursesTask.Result); |
| 54 | + await Task.WhenAll(getAccountDataTask, getCoursesTask); |
57 | 55 |
|
58 | | - if (User.IsInRole(Roles.LecturerRole)) |
59 | | - { |
60 | | - return Ok(new UserDataDto |
61 | | - { |
62 | | - UserData = getAccountDataTask.Result, |
63 | | - Courses = await courses, |
64 | | - TaskDeadlines = Array.Empty<TaskDeadlineView>() |
65 | | - }); |
66 | | - } |
67 | | - |
68 | | - var taskDeadlines = await _coursesClient.GetTaskDeadlines(); |
69 | | - var taskIds = taskDeadlines.Select(t => t.TaskId).ToArray(); |
70 | | - var solutions = await _solutionsServiceClient.GetLastTaskSolutions(taskIds, UserId); |
71 | | - var taskDeadlinesInfo = taskDeadlines.Select((d, i) => new TaskDeadlineView |
72 | | - { |
73 | | - Deadline = d, |
74 | | - SolutionState = solutions[i]?.State, |
75 | | - Rating = solutions[i]?.Rating, |
76 | | - MaxRating = taskDeadlines[i].MaxRating |
77 | | - }).ToArray(); |
| 56 | + var courses = GetCoursePreviews(getCoursesTask.Result); |
78 | 57 |
|
79 | | - var aggregatedResult = new UserDataDto |
| 58 | + if (User.IsInRole(Roles.LecturerRole)) |
| 59 | + { |
| 60 | + return Ok(new UserDataDto |
80 | 61 | { |
81 | 62 | UserData = getAccountDataTask.Result, |
82 | 63 | Courses = await courses, |
83 | | - TaskDeadlines = taskDeadlinesInfo |
84 | | - }; |
85 | | - return Ok(aggregatedResult); |
| 64 | + TaskDeadlines = Array.Empty<TaskDeadlineView>() |
| 65 | + }); |
86 | 66 | } |
87 | 67 |
|
88 | | - [HttpPost("register")] |
89 | | - [ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)] |
90 | | - public async Task<IActionResult> Register(RegisterViewModel model) |
| 68 | + var taskDeadlines = await _coursesClient.GetTaskDeadlines(); |
| 69 | + var taskIds = taskDeadlines.Select(t => t.TaskId).ToArray(); |
| 70 | + var solutions = await _solutionsServiceClient.GetLastTaskSolutions(taskIds, UserId); |
| 71 | + var taskDeadlinesInfo = taskDeadlines.Select((d, i) => new TaskDeadlineView |
91 | 72 | { |
92 | | - var result = await AuthServiceClient.Register(model); |
93 | | - return Ok(result); |
94 | | - } |
| 73 | + Deadline = d, |
| 74 | + SolutionState = solutions[i]?.State, |
| 75 | + Rating = solutions[i]?.Rating, |
| 76 | + MaxRating = taskDeadlines[i].MaxRating |
| 77 | + }).ToArray(); |
95 | 78 |
|
96 | | - [HttpPost("login")] |
97 | | - [ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)] |
98 | | - public async Task<IActionResult> Login(LoginViewModel model) |
| 79 | + var aggregatedResult = new UserDataDto |
99 | 80 | { |
100 | | - var tokenMeta = await AuthServiceClient.Login(model).ConfigureAwait(false); |
101 | | - return Ok(tokenMeta); |
102 | | - } |
| 81 | + UserData = getAccountDataTask.Result, |
| 82 | + Courses = await courses, |
| 83 | + TaskDeadlines = taskDeadlinesInfo |
| 84 | + }; |
| 85 | + return Ok(aggregatedResult); |
| 86 | + } |
103 | 87 |
|
104 | | - [HttpPut("edit")] |
105 | | - [Authorize] |
106 | | - [ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)] |
107 | | - public async Task<IActionResult> Edit(EditAccountViewModel model) |
108 | | - { |
109 | | - var result = await AuthServiceClient.Edit(model, UserId); |
110 | | - return Ok(result); |
111 | | - } |
| 88 | + [HttpPost("register")] |
| 89 | + [ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)] |
| 90 | + public async Task<IActionResult> Register(RegisterViewModel model) |
| 91 | + { |
| 92 | + var result = await AuthServiceClient.Register(model); |
| 93 | + return Ok(result); |
| 94 | + } |
112 | 95 |
|
113 | | - [HttpPost("inviteNewLecturer")] |
114 | | - [Authorize(Roles = Roles.LecturerRole)] |
115 | | - [ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)] |
116 | | - public async Task<IActionResult> InviteNewLecturer(InviteLecturerViewModel model) |
117 | | - { |
118 | | - var result = await AuthServiceClient.InviteNewLecturer(model).ConfigureAwait(false); |
119 | | - return Ok(result); |
120 | | - } |
| 96 | + [HttpPost("login")] |
| 97 | + [ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)] |
| 98 | + public async Task<IActionResult> Login(LoginViewModel model) |
| 99 | + { |
| 100 | + var tokenMeta = await AuthServiceClient.Login(model).ConfigureAwait(false); |
| 101 | + return Ok(tokenMeta); |
| 102 | + } |
121 | 103 |
|
122 | | - [HttpPost("google")] |
123 | | - [ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)] |
124 | | - public async Task<IActionResult> LoginByGoogle(string tokenId) |
125 | | - { |
126 | | - var tokenMeta = await AuthServiceClient.LoginByGoogle(tokenId).ConfigureAwait(false); |
127 | | - return Ok(tokenMeta); |
128 | | - } |
| 104 | + [HttpPut("edit")] |
| 105 | + [Authorize] |
| 106 | + [ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)] |
| 107 | + public async Task<IActionResult> Edit(EditAccountViewModel model) |
| 108 | + { |
| 109 | + var result = await AuthServiceClient.Edit(model, UserId); |
| 110 | + return Ok(result); |
| 111 | + } |
129 | 112 |
|
130 | | - [HttpPut("editExternal")] |
131 | | - [Authorize] |
132 | | - [ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)] |
133 | | - public async Task<IActionResult> EditExternal(EditExternalViewModel model) |
134 | | - { |
135 | | - var result = await AuthServiceClient.EditExternal(model, UserId); |
136 | | - return Ok(result); |
137 | | - } |
| 113 | + [HttpPost("inviteNewLecturer")] |
| 114 | + [Authorize(Roles = Roles.LecturerRole)] |
| 115 | + [ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)] |
| 116 | + public async Task<IActionResult> InviteNewLecturer(InviteLecturerViewModel model) |
| 117 | + { |
| 118 | + var result = await AuthServiceClient.InviteNewLecturer(model).ConfigureAwait(false); |
| 119 | + return Ok(result); |
| 120 | + } |
138 | 121 |
|
139 | | - [HttpGet("getAllStudents")] |
140 | | - [Authorize(Roles = Roles.LecturerRole)] |
141 | | - [ProducesResponseType(typeof(AccountDataDto[]), (int)HttpStatusCode.OK)] |
142 | | - public async Task<IActionResult> GetAllStudents() |
143 | | - { |
144 | | - var result = await AuthServiceClient.GetAllStudents(); |
145 | | - return Ok(result); |
146 | | - } |
| 122 | + [HttpPost("google")] |
| 123 | + [ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)] |
| 124 | + public async Task<IActionResult> LoginByGoogle(string tokenId) |
| 125 | + { |
| 126 | + var tokenMeta = await AuthServiceClient.LoginByGoogle(tokenId).ConfigureAwait(false); |
| 127 | + return Ok(tokenMeta); |
| 128 | + } |
| 129 | + |
| 130 | + [HttpPut("editExternal")] |
| 131 | + [Authorize] |
| 132 | + [ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)] |
| 133 | + public async Task<IActionResult> EditExternal(EditExternalViewModel model) |
| 134 | + { |
| 135 | + var result = await AuthServiceClient.EditExternal(model, UserId); |
| 136 | + return Ok(result); |
| 137 | + } |
| 138 | + |
| 139 | + [HttpGet("getAllStudents")] |
| 140 | + [Authorize(Roles = Roles.LecturerRole)] |
| 141 | + [ProducesResponseType(typeof(AccountDataDto[]), (int)HttpStatusCode.OK)] |
| 142 | + public async Task<IActionResult> GetAllStudents() |
| 143 | + { |
| 144 | + var result = await AuthServiceClient.GetAllStudents(); |
| 145 | + return Ok(result); |
147 | 146 | } |
148 | 147 | } |
0 commit comments