-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAccountController.cs
More file actions
147 lines (130 loc) · 4.99 KB
/
AccountController.cs
File metadata and controls
147 lines (130 loc) · 4.99 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
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()
: Ok(result);
}
//TODO: separate for mentor and student
[HttpGet("getUserData")]
[Authorize]
[ProducesResponseType(typeof(UserDataDto), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetUserData()
{
var getAccountDataTask = AuthServiceClient.GetAccountData(UserId);
var getCoursesTask = _coursesClient.GetAllUserCourses();
await Task.WhenAll(getAccountDataTask, getCoursesTask);
var courses = GetCoursePreviews(getCoursesTask.Result);
if (User.IsInRole(Roles.LecturerRole))
{
return Ok(new UserDataDto
{
UserData = getAccountDataTask.Result,
Courses = await courses,
TaskDeadlines = Array.Empty<TaskDeadlineView>()
});
}
var taskDeadlines = await _coursesClient.GetTaskDeadlines();
var taskIds = taskDeadlines.Select(t => t.TaskId).ToArray();
var solutions = await _solutionsServiceClient.GetLastTaskSolutions(taskIds, UserId);
var taskDeadlinesInfo = taskDeadlines.Select((d, i) => new TaskDeadlineView
{
Deadline = d,
SolutionState = solutions[i]?.State,
Rating = solutions[i]?.Rating,
MaxRating = taskDeadlines[i].MaxRating
}).ToArray();
var aggregatedResult = new UserDataDto
{
UserData = getAccountDataTask.Result,
Courses = await courses,
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);
}
[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);
}
[HttpPost("google")]
[ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> LoginByGoogle(string tokenId)
{
var tokenMeta = await AuthServiceClient.LoginByGoogle(tokenId).ConfigureAwait(false);
return Ok(tokenMeta);
}
[HttpPut("editExternal")]
[Authorize]
[ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)]
public async Task<IActionResult> EditExternal(EditExternalViewModel model)
{
var result = await AuthServiceClient.EditExternal(model, UserId);
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);
}
}