presence/Presence.API/Controllers/GroupController.cs

99 lines
3.3 KiB
C#
Raw Normal View History

2024-12-05 07:31:49 +00:00
using domain.Request;
2024-11-26 05:09:12 +00:00
using domain.UseCase;
using Microsoft.AspNetCore.Mvc;
using Presence.API.Response;
namespace Presence.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class GroupController : ControllerBase
{
private readonly IGroupUseCase _groupService;
public GroupController(IGroupUseCase groupService)
{
_groupService = groupService;
}
[HttpGet("/group")]
public ActionResult<GroupResponse> GetAllGroups()
{
var result = _groupService
.GetGroupsWithStudents()
.Select(group => new GroupResponse
{
Id = group.Id,
Name = group.Name,
Users = group.Users?.Select(
user => new UserResponse
{
Id = user.Id,
LastName = user.LastName,
FirstName = user.FirstName,
Patronymic = user.Patronymic
}).ToList()
}).ToList();
return Ok(result);
}
[HttpDelete("/group/{id}")]
public IActionResult DeleteGroup(int id)
{
RemoveGroupRequest removeGroupRequest = new() { GroupId = id };
2024-11-28 07:36:11 +00:00
bool isDeleted = _groupService.RemoveGroup(removeGroupRequest);
if (!isDeleted) return NotFound();
else return NoContent();
}
[HttpPost("/group")]
public ActionResult<GroupResponse> PostGroup(AddGroupRequest addGroupRequest)
{
if (addGroupRequest is null)
return BadRequest(new ArgumentNullException());
2024-12-05 07:31:49 +00:00
var groupWithStudentsRequest = new AddGroupWithStudentRequest
{
AddGroupRequest = new AddGroupRequest { Name = addGroupRequest.Name },
AddStudentRequests = addGroupRequest.Students
};
_groupService.AddGroupWithStudents(groupWithStudentsRequest);
var createdGroup = _groupService.GetGroupsWithStudents()
.FirstOrDefault(g => g.Name == addGroupRequest.Name);
return CreatedAtAction(nameof(GetGroupById), new { id = createdGroup.Id }, createdGroup);
2024-11-28 07:36:11 +00:00
}
2024-12-04 12:21:15 +00:00
2024-12-05 07:31:49 +00:00
[HttpGet("/group/{id:int}")]
public ActionResult<GroupResponse> GetGroupById(int id)
2024-11-28 07:36:11 +00:00
{
2024-12-05 07:31:49 +00:00
var group = _groupService.GetGroupsWithStudents().FirstOrDefault(g => g.Id == id);
if (group == null)
return NotFound();
var response = new GroupResponse
{
Id = group.Id,
Name = group.Name,
Users = group.Users?.Select(
user => new UserResponse
{
Id = user.Id,
LastName = user.LastName,
FirstName = user.FirstName,
Patronymic = user.Patronymic
}).ToList()
};
return Ok(response);
2024-11-28 07:36:11 +00:00
}
2024-12-04 12:21:15 +00:00
2024-12-05 07:31:49 +00:00
[HttpGet("/group/{id}/subjects")]
public ActionResult<GroupSubjectResponse> GetGroupSubject(int id)
2024-11-28 07:36:11 +00:00
{
2024-12-05 07:31:49 +00:00
return Ok(_groupService.GetGroupSubject(id));
2024-11-26 05:09:12 +00:00
}
}
}