-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAccountController.cs
More file actions
193 lines (173 loc) · 7.19 KB
/
AccountController.cs
File metadata and controls
193 lines (173 loc) · 7.19 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
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using HwProj.APIGateway.API.Models;
using HwProj.APIGateway.API.Models.Tasks;
using HwProj.AuthService.Client;
using HwProj.CoursesService.Client;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Models.Result;
using HwProj.Models.Roles;
using HwProj.SolutionsService.Client;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace HwProj.APIGateway.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AccountController : AggregationController
{
private readonly ICoursesServiceClient _coursesClient;
private readonly ISolutionsServiceClient _solutionsServiceClient;
public AccountController(
IAuthServiceClient authClient,
ICoursesServiceClient coursesClient,
ISolutionsServiceClient solutionsServiceClient) : base(authClient)
{
_coursesClient = coursesClient;
_solutionsServiceClient = solutionsServiceClient;
}
[HttpGet("getUserData/{userId}")]
[ProducesResponseType(typeof(AccountDataDto), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetUserDataById(string userId)
{
var result = await AuthServiceClient.GetAccountData(userId);
return result == null
? NotFound() as IActionResult
: Ok(result);
}
//TODO: separate for mentor and student
[HttpGet("getUserData")]
[Authorize]
[ProducesResponseType(typeof(UserDataDto), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetUserData()
{
var accountData = await AuthServiceClient.GetAccountData(UserId);
if (User.IsInRole(Roles.LecturerRole))
{
var courses = await _coursesClient.GetAllUserCourses();
var courseEvents = courses
.Select(t => new CourseEvents
{
Id = t.Id,
Name = t.Name,
GroupName = t.GroupName,
IsCompleted = t.IsCompleted,
NewStudentsCount = t.NewStudents.Count()
})
.Where(t => t.NewStudentsCount > 0)
.ToArray();
return Ok(new UserDataDto
{
UserData = accountData,
CourseEvents = courseEvents,
TaskDeadlines = Array.Empty<TaskDeadlineView>()
});
}
var currentTime = DateTime.UtcNow;
var taskDeadlines = await _coursesClient.GetTaskDeadlines();
var taskIds = taskDeadlines.Select(t => t.TaskId).ToArray();
var solutions = await _solutionsServiceClient.GetLastTaskSolutions(taskIds, UserId);
var taskDeadlinesInfo = taskDeadlines
.Zip(solutions, (deadline, solution) => (deadline, solution))
.Where(t => currentTime <= t.deadline.DeadlineDate || t.solution == null)
.Select(t => new TaskDeadlineView
{
Deadline = t.deadline,
SolutionState = t.solution?.State,
MaxRating = t.deadline.MaxRating,
Rating = t.solution?.Rating,
DeadlinePast = currentTime > t.deadline.DeadlineDate
}).ToArray();
var aggregatedResult = new UserDataDto
{
UserData = accountData,
TaskDeadlines = taskDeadlinesInfo
};
return Ok(aggregatedResult);
}
[HttpPost("register")]
[ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Register(RegisterViewModel model)
{
var result = await AuthServiceClient.Register(model);
return Ok(result);
}
[HttpPost("login")]
[ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Login(LoginViewModel model)
{
var tokenMeta = await AuthServiceClient.Login(model).ConfigureAwait(false);
return Ok(tokenMeta);
}
[Authorize]
[HttpGet("refreshToken")]
[ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> RefreshToken()
{
var tokenMeta = await AuthServiceClient.RefreshToken(UserId!);
return Ok(tokenMeta);
}
[HttpGet("getGuestToken")]
[Authorize(Roles = Roles.LecturerRole)]
[ProducesResponseType(typeof(TokenCredentials), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetGuestToken([FromQuery] string courseId)
{
var tokenMeta = await AuthServiceClient.GetGuestToken(courseId);
return Ok(tokenMeta);
}
[HttpPut("edit")]
[Authorize]
[ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Edit(EditAccountViewModel model)
{
var result = await AuthServiceClient.Edit(model, UserId);
return Ok(result);
}
[HttpPost("inviteNewLecturer")]
[Authorize(Roles = Roles.LecturerRole)]
[ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)]
public async Task<IActionResult> InviteNewLecturer(InviteLecturerViewModel model)
{
var result = await AuthServiceClient.InviteNewLecturer(model).ConfigureAwait(false);
return Ok(result);
}
[HttpGet("getAllStudents")]
[Authorize(Roles = Roles.LecturerRole)]
[ProducesResponseType(typeof(AccountDataDto[]), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetAllStudents()
{
var result = await AuthServiceClient.GetAllStudents();
return Ok(result);
}
[HttpPost("requestPasswordRecovery")]
public async Task<Result> RequestPasswordRecovery(RequestPasswordRecoveryViewModel model)
{
return await AuthServiceClient.RequestPasswordRecovery(model);
}
[HttpPost("resetPassword")]
public async Task<Result> ResetPassword(ResetPasswordViewModel model)
{
return await AuthServiceClient.ResetPassword(model);
}
[Authorize]
[HttpPost("github/url")]
[ProducesResponseType(typeof(UrlDto), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetGithubLoginUrl([FromBody] UrlDto urlDto)
{
var result = await AuthServiceClient.GetGithubLoginUrl(urlDto);
return Ok(result);
}
[Authorize]
[HttpPost("github/authorize")]
[ProducesResponseType(typeof(GithubCredentials), (int)HttpStatusCode.OK)]
public async Task<IActionResult> AuthorizeGithub(
[FromQuery] string code)
{
var result = await AuthServiceClient.AuthorizeGithub(code, UserId);
return Ok(result);
}
}
}