-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCourseGroupsController.cs
More file actions
116 lines (103 loc) · 4.2 KB
/
CourseGroupsController.cs
File metadata and controls
116 lines (103 loc) · 4.2 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
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;
using HwProj.Common.Net8;
using HwProj.CoursesService.API.Filters;
using HwProj.CoursesService.API.Models;
using HwProj.CoursesService.API.Services;
using HwProj.Models.CoursesService.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace HwProj.CoursesService.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class CourseGroupsController : Controller
{
private readonly IGroupsService _groupsService;
private readonly IMapper _mapper;
private readonly ICourseFilterService _courseFilterService;
private readonly ICoursesService _coursesService;
public CourseGroupsController(
IMapper mapper,
IGroupsService groupsService,
ICourseFilterService courseFilterService,
ICoursesService coursesService)
{
_mapper = mapper;
_groupsService = groupsService;
_courseFilterService = courseFilterService;
_coursesService = coursesService;
}
[HttpGet("{courseId}/getAll")]
public async Task<GroupViewModel[]> GetAll(long courseId)
{
var groups = await _groupsService.GetAllAsync(courseId);
var result = groups.Select(t => new GroupViewModel
{
Id = t.Id,
Name = t.Name,
StudentsIds = t.GroupMates.Select(s => s.StudentId).ToArray()
}).ToArray();
return result;
}
[HttpPost("{courseId}/create")]
public async Task<IActionResult> CreateGroup([FromBody] CreateGroupViewModel groupViewModel)
{
var group = new Group
{
CourseId = groupViewModel.CourseId,
Name = groupViewModel.Name,
GroupMates = groupViewModel.GroupMatesIds.Select(t => new GroupMate() { StudentId = t }).ToList()
};
var id = await _groupsService.AddGroupAsync(group);
var userId = Request.GetUserIdFromHeader();
var courseMentorIds = await _coursesService.GetCourseLecturers(groupViewModel.CourseId);
if (userId != null && courseMentorIds.Contains(userId))
await _courseFilterService.AddToFilter(groupViewModel.CourseId, userId, new Filter
{
GroupIds = new List<long> { id }
});
return Ok(id);
}
[HttpDelete("{courseId}/delete/{groupId}")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> DeleteGroup(long groupId)
{
await _groupsService.DeleteGroupAsync(groupId).ConfigureAwait(false);
return Ok();
}
[HttpPost("{courseId}/update/{groupId}")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> UpdateGroup(long groupId, [FromBody] UpdateGroupViewModel groupViewModel)
{
await _groupsService.UpdateAsync(groupId, _mapper.Map<Group>(groupViewModel));
return Ok();
}
[HttpGet("{courseId}/get")]
public async Task<IActionResult> GetCoursesGroups(long courseId, [FromQuery] string userId)
{
var groups = await _groupsService.GetStudentGroupsAsync(courseId, userId);
return Ok(groups);
}
[HttpPost("{courseId}/addStudentInGroup/{groupId}")]
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
public async Task<IActionResult> AddStudentInGroup(long groupId, [FromQuery] string userId)
{
await _groupsService.AddGroupMateAsync(groupId, userId);
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get([FromBody] long[] groupIds)
{
var groups = await _groupsService.GetGroupsAsync(groupIds);
var result = groups.Select(group => new GroupViewModel
{
Id = group.Id,
Name = group.Name,
StudentsIds = group.GroupMates.Select(g => g.StudentId).ToArray()
}).ToArray();
return Ok(result) as IActionResult;
}
}
}