128 lines
3.8 KiB
C#
128 lines
3.8 KiB
C#
using domain.Models;
|
||
using domain.UseCase;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
|
||
|
||
namespace presence_api.Controllers;
|
||
|
||
[ApiController]
|
||
[Route("api/admin")]
|
||
|
||
public class AdminPanelController : ControllerBase
|
||
{
|
||
private readonly GroupUseCase _groupUseCase;
|
||
private readonly UserUseCase _userUseCase;
|
||
private readonly UseCaseGeneratePresence _presenceUseCase;
|
||
private readonly UseCaseAPI _useCaseAPI;
|
||
|
||
public AdminPanelController(GroupUseCase groupUseCase, UserUseCase userUseCase, UseCaseGeneratePresence presenceUseCase, UseCaseAPI useCaseAPI)
|
||
{
|
||
_groupUseCase = groupUseCase;
|
||
_userUseCase = userUseCase;
|
||
_presenceUseCase = presenceUseCase;
|
||
_useCaseAPI = useCaseAPI;
|
||
}
|
||
|
||
[HttpGet("student/{userGuid}")]
|
||
public ActionResult GetStudentInfo(Guid userGuid)
|
||
{
|
||
// Получаем информацию о студенте
|
||
var user = _userUseCase.GetUserInfo(userGuid); // Используем UserUseCase для получения данных о студенте
|
||
|
||
if (user == null)
|
||
{
|
||
return NotFound("Студент не найден");
|
||
}
|
||
|
||
|
||
|
||
// Формируем результат
|
||
var result = new
|
||
{
|
||
FIO = user.FIO,
|
||
Guid = user.Guid,
|
||
GroupName = user.Group?.Name ?? "Не указана",
|
||
|
||
};
|
||
|
||
return Ok(result);
|
||
}
|
||
|
||
[HttpGet("groups")]
|
||
public ActionResult<List<GroupWithStudentsDto>> GetAllGroupsWithUsers()
|
||
{
|
||
try
|
||
{
|
||
// Получаем группы с пользователями
|
||
var groupsWithUsers = _groupUseCase.GetAllGroupWithStident().Select(group => new GroupWithStudentsDto
|
||
{
|
||
GroupName = group.Name, // Название группы
|
||
Students = group.Users.Select(u => u.FIO).ToList() // Список студентов
|
||
}).ToList();
|
||
|
||
return Ok(groupsWithUsers); // Возвращаем результат
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
// Логируем ошибку и возвращаем ошибку
|
||
return BadRequest(new { message = ex.Message });
|
||
}
|
||
}
|
||
|
||
[HttpDelete("presence/clear")]
|
||
public ActionResult ClearPresence()
|
||
{
|
||
try
|
||
{
|
||
_presenceUseCase.ClearAllPresence();
|
||
return Ok(new { message = "Все записи присутствия успешно удалены." });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return BadRequest(new { message = ex.Message });
|
||
}
|
||
}
|
||
|
||
[HttpDelete("groups/{groupId}")]
|
||
public ActionResult RemoveGroupById(int groupId)
|
||
{
|
||
try
|
||
{
|
||
_groupUseCase.RemoveGroupById(groupId);
|
||
return Ok(new { message = "Группа успешно удалена." });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return BadRequest(new { message = ex.Message });
|
||
}
|
||
}
|
||
|
||
[HttpDelete("users/{userGuid}")]
|
||
public ActionResult RemoveUserByGuid(Guid userGuid)
|
||
{
|
||
try
|
||
{
|
||
_userUseCase.RemoveUserByGuid(userGuid);
|
||
return Ok(new { message = "Пользователь удален." });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return BadRequest (new { message = ex.Message });
|
||
}
|
||
}
|
||
|
||
[HttpPost("groups/add")]
|
||
public IActionResult AddGroupWithStudents([FromBody] GroupWithStudentsDto groupDto)
|
||
{
|
||
try
|
||
{
|
||
_useCaseAPI.AddGroupWithStudents(groupDto);
|
||
return Ok(new { message = "Группа успешно добавлена." });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return BadRequest(new { message = ex.Message });
|
||
}
|
||
}
|
||
|
||
} |