using domain.Request; 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 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 }; bool isDeleted = _groupService.RemoveGroup(removeGroupRequest); if (!isDeleted) return NotFound(); else return NoContent(); } [HttpPost("/group")] public ActionResult PostGroup(AddGroupRequest addGroupRequest) { if (addGroupRequest is null) return BadRequest(new ArgumentNullException()); bool isCreated = _groupService.AddGroup(addGroupRequest); if (isCreated) return CreatedAtAction(nameof(GetGroupByName), new { name = addGroupRequest.Name }, addGroupRequest ); else return BadRequest(); } [HttpGet("/group/{id}/subjects")] public ActionResult GetGroupSubject(int id) { return Ok(_groupService.GetGroupSubject(id)); } [HttpGet("/group/{name}")] public ActionResult GetGroupByName(string name) { var result = _groupService .GetGroupsWithStudents() .Where(g => g.Name == name) .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 result.Count > 0 ? Ok(result) : NotFound(); } } }