presence/Presence.API/Controllers/GroupController.cs

105 lines
3.6 KiB
C#
Raw Normal View History

2024-11-26 05:09:12 +00:00
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<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());
bool isCreated = _groupService.AddGroup(addGroupRequest);
if (isCreated) return CreatedAtAction(nameof(GetGroupByName), new { name = addGroupRequest.Name }, addGroupRequest );
else return BadRequest();
}
/*
[HttpGet("/group/{id}")]
public ActionResult<GroupResponse> GetGroupById(int id)
{
var result = _groupService
.GetGroupsWithStudents()
.Where(g => g.Id == id)
.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();
}
*/
[HttpGet("/group/{name}")]
public ActionResult<GroupResponse> 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();
2024-11-26 05:09:12 +00:00
}
}
}