Skip to content

Commit 6739ad2

Browse files
committed
API Gateway: code cleanup
1 parent fd616c0 commit 6739ad2

26 files changed

+1637
-1689
lines changed

HwProj.APIGateway/HwProj.APIGateway.API/ApplicationProfile.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
using HwProj.Models.AuthService.ViewModels;
33
using HwProj.Models.CoursesService.DTO;
44

5-
namespace HwProj.APIGateway.API
5+
namespace HwProj.APIGateway.API;
6+
7+
public class ApplicationProfile : Profile
68
{
7-
public class ApplicationProfile : Profile
9+
public ApplicationProfile()
810
{
9-
public ApplicationProfile()
10-
{
11-
CreateMap<InviteExpertViewModel, CreateCourseFilterDTO>();
12-
CreateMap<EditMentorWorkspaceDTO, CreateCourseFilterDTO>();
13-
}
11+
CreateMap<InviteExpertViewModel, CreateCourseFilterDTO>();
12+
CreateMap<EditMentorWorkspaceDTO, CreateCourseFilterDTO>();
1413
}
15-
}
14+
}

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/AccountController.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,12 @@ namespace HwProj.APIGateway.API.Controllers
1818
{
1919
[Route("api/[controller]")]
2020
[ApiController]
21-
public class AccountController : AggregationController
21+
public class AccountController(
22+
IAuthServiceClient authClient,
23+
ICoursesServiceClient coursesClient,
24+
ISolutionsServiceClient solutionsServiceClient)
25+
: AggregationController(authClient)
2226
{
23-
private readonly ICoursesServiceClient _coursesClient;
24-
private readonly ISolutionsServiceClient _solutionsServiceClient;
25-
26-
public AccountController(
27-
IAuthServiceClient authClient,
28-
ICoursesServiceClient coursesClient,
29-
ISolutionsServiceClient solutionsServiceClient) : base(authClient)
30-
{
31-
_coursesClient = coursesClient;
32-
_solutionsServiceClient = solutionsServiceClient;
33-
}
34-
3527
[HttpGet("getUserData/{userId}")]
3628
[ProducesResponseType(typeof(AccountDataDto), (int)HttpStatusCode.OK)]
3729
public async Task<IActionResult> GetUserDataById(string userId)
@@ -52,7 +44,7 @@ public async Task<IActionResult> GetUserData()
5244

5345
if (User.IsInRole(Roles.LecturerRole))
5446
{
55-
var courses = await _coursesClient.GetAllUserCourses();
47+
var courses = await coursesClient.GetAllUserCourses();
5648
var courseEvents = courses
5749
.Select(t => new CourseEvents
5850
{
@@ -74,9 +66,9 @@ public async Task<IActionResult> GetUserData()
7466
}
7567

7668
var currentTime = DateTime.UtcNow;
77-
var taskDeadlines = await _coursesClient.GetTaskDeadlines();
69+
var taskDeadlines = await coursesClient.GetTaskDeadlines();
7870
var taskIds = taskDeadlines.Select(t => t.TaskId).ToArray();
79-
var solutions = await _solutionsServiceClient.GetLastTaskSolutions(taskIds, UserId);
71+
var solutions = await solutionsServiceClient.GetLastTaskSolutions(taskIds, UserId);
8072
var taskDeadlinesInfo = taskDeadlines
8173
.Zip(solutions, (deadline, solution) => (deadline, solution))
8274
.Where(t => currentTime <= t.deadline.DeadlineDate || t.solution == null)
@@ -160,7 +152,7 @@ public async Task<Result> ResetPassword(ResetPasswordViewModel model)
160152
{
161153
return await AuthServiceClient.ResetPassword(model);
162154
}
163-
155+
164156
[Authorize]
165157
[HttpPost("github/url")]
166158
[ProducesResponseType(typeof(UrlDto), (int)HttpStatusCode.OK)]

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/AggregationController.cs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,38 @@
55
using HwProj.Models.CoursesService.ViewModels;
66
using Microsoft.AspNetCore.Mvc;
77

8-
namespace HwProj.APIGateway.API.Controllers
8+
namespace HwProj.APIGateway.API.Controllers;
9+
10+
public class AggregationController : ControllerBase
911
{
10-
public class AggregationController : ControllerBase
11-
{
12-
protected readonly IAuthServiceClient AuthServiceClient;
12+
protected readonly IAuthServiceClient AuthServiceClient;
1313

14-
protected AggregationController(IAuthServiceClient authServiceClient)
15-
{
16-
AuthServiceClient = authServiceClient;
17-
}
14+
protected AggregationController(IAuthServiceClient authServiceClient)
15+
{
16+
AuthServiceClient = authServiceClient;
17+
}
1818

19-
protected string? UserId =>
20-
Request.HttpContext.User.Claims
21-
.FirstOrDefault(claim => claim.Type.ToString() == "_id")
22-
?.Value;
19+
protected string? UserId =>
20+
Request.HttpContext.User.Claims
21+
.FirstOrDefault(claim => claim.Type.ToString() == "_id")
22+
?.Value;
2323

24-
protected async Task<CoursePreviewView[]> GetCoursePreviews(CoursePreview[] courses)
24+
protected async Task<CoursePreviewView[]> GetCoursePreviews(CoursePreview[] courses)
25+
{
26+
var mentorIds = courses.SelectMany(t => t.MentorIds).Distinct().ToArray();
27+
var mentors = await AuthServiceClient.GetAccountsData(mentorIds);
28+
var mentorsDict = mentors.Where(x => x != null).ToDictionary(x => x.UserId);
29+
return courses.Select(course => new CoursePreviewView
2530
{
26-
var mentorIds = courses.SelectMany(t => t.MentorIds).Distinct().ToArray();
27-
var mentors = await AuthServiceClient.GetAccountsData(mentorIds);
28-
var mentorsDict = mentors.Where(x => x != null).ToDictionary(x => x.UserId);
29-
return courses.Select(course => new CoursePreviewView
30-
{
31-
Id = course.Id,
32-
Name = course.Name,
33-
GroupName = course.GroupName,
34-
IsCompleted = course.IsCompleted,
35-
Mentors = course.MentorIds
36-
.Select(x => mentorsDict.TryGetValue(x, out var mentor) ? mentor : null)
37-
.Where(x => x != null)
38-
.ToArray()!,
39-
TaskId = course.TaskId
40-
}).ToArray();
41-
}
31+
Id = course.Id,
32+
Name = course.Name,
33+
GroupName = course.GroupName,
34+
IsCompleted = course.IsCompleted,
35+
Mentors = course.MentorIds
36+
.Select(x => mentorsDict.TryGetValue(x, out var mentor) ? mentor : null)
37+
.Where(x => x != null)
38+
.ToArray()!,
39+
TaskId = course.TaskId
40+
}).ToArray();
4241
}
4342
}

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/CourseGroupsController.cs

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,97 +7,96 @@
77
using Microsoft.AspNetCore.Authorization;
88
using Microsoft.AspNetCore.Mvc;
99

10-
namespace HwProj.APIGateway.API.Controllers
10+
namespace HwProj.APIGateway.API.Controllers;
11+
12+
[Route("api/[controller]")]
13+
[ApiController]
14+
public class CourseGroupsController : AggregationController
1115
{
12-
[Route("api/[controller]")]
13-
[ApiController]
14-
public class CourseGroupsController : AggregationController
15-
{
16-
private readonly ICoursesServiceClient _coursesClient;
16+
private readonly ICoursesServiceClient _coursesClient;
1717

18-
public CourseGroupsController(ICoursesServiceClient coursesClient) : base(null)
19-
{
20-
_coursesClient = coursesClient;
21-
}
18+
public CourseGroupsController(ICoursesServiceClient coursesClient) : base(null)
19+
{
20+
_coursesClient = coursesClient;
21+
}
2222

23-
[HttpGet("{courseId}/getAll")]
24-
[ProducesResponseType(typeof(GroupViewModel[]), (int)HttpStatusCode.OK)]
25-
public async Task<IActionResult> GetAllCourseGroups(long courseId)
26-
{
27-
var result = await _coursesClient.GetAllCourseGroups(courseId);
28-
return result == null
29-
? NotFound() as IActionResult
30-
: Ok(result);
31-
}
23+
[HttpGet("{courseId}/getAll")]
24+
[ProducesResponseType(typeof(GroupViewModel[]), (int)HttpStatusCode.OK)]
25+
public async Task<IActionResult> GetAllCourseGroups(long courseId)
26+
{
27+
var result = await _coursesClient.GetAllCourseGroups(courseId);
28+
return result == null
29+
? NotFound()
30+
: Ok(result);
31+
}
3232

33-
[HttpPost("{courseId}/create")]
34-
[Authorize(Roles = Roles.LecturerRole)]
35-
[ProducesResponseType(typeof(long), (int)HttpStatusCode.OK)]
36-
public async Task<IActionResult> CreateCourseGroup(CreateGroupViewModel model, long courseId)
37-
{
38-
var result = await _coursesClient.CreateCourseGroup(model, courseId);
39-
return Ok(result);
40-
}
33+
[HttpPost("{courseId}/create")]
34+
[Authorize(Roles = Roles.LecturerRole)]
35+
[ProducesResponseType(typeof(long), (int)HttpStatusCode.OK)]
36+
public async Task<IActionResult> CreateCourseGroup(CreateGroupViewModel model, long courseId)
37+
{
38+
var result = await _coursesClient.CreateCourseGroup(model, courseId);
39+
return Ok(result);
40+
}
4141

42-
[HttpDelete("{courseId}/delete/{groupId}")]
43-
[Authorize(Roles = Roles.LecturerRole)]
44-
public async Task<IActionResult> DeleteCourseGroup(long courseId, long groupId)
45-
{
46-
await _coursesClient.DeleteCourseGroup(courseId, groupId);
47-
return Ok();
48-
}
42+
[HttpDelete("{courseId}/delete/{groupId}")]
43+
[Authorize(Roles = Roles.LecturerRole)]
44+
public async Task<IActionResult> DeleteCourseGroup(long courseId, long groupId)
45+
{
46+
await _coursesClient.DeleteCourseGroup(courseId, groupId);
47+
return Ok();
48+
}
4949

50-
[HttpPost("{courseId}/update/{groupId}")]
51-
[Authorize(Roles = Roles.LecturerRole)]
52-
public async Task<IActionResult> UpdateCourseGroup(UpdateGroupViewModel model, long courseId, long groupId)
53-
{
54-
await _coursesClient.UpdateCourseGroup(model, courseId, groupId);
55-
return Ok();
56-
}
50+
[HttpPost("{courseId}/update/{groupId}")]
51+
[Authorize(Roles = Roles.LecturerRole)]
52+
public async Task<IActionResult> UpdateCourseGroup(UpdateGroupViewModel model, long courseId, long groupId)
53+
{
54+
await _coursesClient.UpdateCourseGroup(model, courseId, groupId);
55+
return Ok();
56+
}
5757

58-
[HttpGet("{courseId}/get")]
59-
[Authorize]
60-
[ProducesResponseType(typeof(GroupViewModel), (int)HttpStatusCode.OK)]
61-
public async Task<IActionResult> GetCourseGroupsById(long courseId)
62-
{
63-
var result = await _coursesClient.GetCourseGroupsById(courseId, UserId);
64-
return result == null
65-
? NotFound() as IActionResult
66-
: Ok(result);
67-
}
58+
[HttpGet("{courseId}/get")]
59+
[Authorize]
60+
[ProducesResponseType(typeof(GroupViewModel), (int)HttpStatusCode.OK)]
61+
public async Task<IActionResult> GetCourseGroupsById(long courseId)
62+
{
63+
var result = await _coursesClient.GetCourseGroupsById(courseId, UserId);
64+
return result == null
65+
? NotFound()
66+
: Ok(result);
67+
}
6868

69-
[HttpPost("{courseId}/addStudentInGroup/{groupId}")]
70-
[Authorize(Roles = Roles.LecturerRole)]
71-
public async Task<IActionResult> AddStudentInGroup(long courseId, long groupId, [FromQuery] string userId)
72-
{
73-
await _coursesClient.AddStudentInGroup(courseId, groupId, userId);
74-
return Ok();
75-
}
69+
[HttpPost("{courseId}/addStudentInGroup/{groupId}")]
70+
[Authorize(Roles = Roles.LecturerRole)]
71+
public async Task<IActionResult> AddStudentInGroup(long courseId, long groupId, [FromQuery] string userId)
72+
{
73+
await _coursesClient.AddStudentInGroup(courseId, groupId, userId);
74+
return Ok();
75+
}
7676

77-
[HttpPost("{courseId}/removeStudentFromGroup/{groupId}")]
78-
[Authorize(Roles = Roles.LecturerRole)]
79-
public async Task<IActionResult> RemoveStudentFromGroup(long courseId, long groupId, [FromQuery] string userId)
80-
{
81-
await _coursesClient.RemoveStudentFromGroup(courseId, groupId, userId);
82-
return Ok();
83-
}
77+
[HttpPost("{courseId}/removeStudentFromGroup/{groupId}")]
78+
[Authorize(Roles = Roles.LecturerRole)]
79+
public async Task<IActionResult> RemoveStudentFromGroup(long courseId, long groupId, [FromQuery] string userId)
80+
{
81+
await _coursesClient.RemoveStudentFromGroup(courseId, groupId, userId);
82+
return Ok();
83+
}
8484

85-
[HttpGet("get/{groupId}")]
86-
[ProducesResponseType(typeof(GroupViewModel), (int)HttpStatusCode.OK)]
87-
public async Task<IActionResult> GetGroup(long groupId)
88-
{
89-
var result = (await _coursesClient.GetGroupsById(groupId)).FirstOrDefault();
90-
return result == null
91-
? NotFound() as IActionResult
92-
: Ok(result);
93-
}
85+
[HttpGet("get/{groupId}")]
86+
[ProducesResponseType(typeof(GroupViewModel), (int)HttpStatusCode.OK)]
87+
public async Task<IActionResult> GetGroup(long groupId)
88+
{
89+
var result = (await _coursesClient.GetGroupsById(groupId)).FirstOrDefault();
90+
return result == null
91+
? NotFound()
92+
: Ok(result);
93+
}
9494

95-
[HttpGet("getTasks/{groupId}")]
96-
[ProducesResponseType(typeof(long[]), (int)HttpStatusCode.OK)]
97-
public async Task<IActionResult> GetGroupTasks(long groupId)
98-
{
99-
var result = await _coursesClient.GetGroupTasks(groupId);
100-
return Ok(result);
101-
}
95+
[HttpGet("getTasks/{groupId}")]
96+
[ProducesResponseType(typeof(long[]), (int)HttpStatusCode.OK)]
97+
public async Task<IActionResult> GetGroupTasks(long groupId)
98+
{
99+
var result = await _coursesClient.GetGroupTasks(groupId);
100+
return Ok(result);
102101
}
103102
}

0 commit comments

Comments
 (0)