Presence.Desktop/presence_api/Controllers/PresenceController/PresenceController.cs
2024-12-20 11:49:17 +03:00

89 lines
3.1 KiB
C#

using data.Domain.UseCase;
using data.RemoteData.RemoteDatabase.DAO;
using data.RemoteData.RemoteDataBase.DAO;
using data.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Storage;
namespace presence_api.Controllers;
[ApiController]
[Route("api/[controller]")]
public class PresenceController : ControllerBase {
readonly UseCaseGeneratePresence _presenceUseCase;
public PresenceController(UseCaseGeneratePresence presenceUseCase)
{
_presenceUseCase = presenceUseCase;
}
[HttpGet]
public ActionResult<IEnumerable<PresenceDao>> getPresence([FromQuery] int GroupId, [FromQuery] DateOnly StartData, [FromQuery] DateOnly EndData, [FromQuery] int UserId)
{
return Ok(_presenceUseCase.GetPresenceAll(EndData,StartData, GroupId, UserId));
}
[HttpPost("update")]
public IActionResult UpdateAttendance([FromBody] List<AttendanceInputModel> attendanceList)
{
if (attendanceList == null || !attendanceList.Any())
{
return BadRequest("Attendance list cannot be empty.");
}
try
{
// Âûçûâàåì ìåòîä ñåðâèñà äëÿ îáíîâëåíèÿ ïîñåùàåìîñòè
_presenceUseCase.UpdateAttendance(attendanceList);
return Ok("Attendance updated successfully.");
}
catch (Exception ex)
{
// Ëîãèðóåì îøèáêó
return StatusCode(500, $"Internal server error: {ex.Message}");
}
}
[HttpDelete]
public ActionResult DeletePresence([FromQuery] int GroupId, [FromQuery] int? UserId, [FromQuery] DateOnly? StartDate, [FromQuery] DateOnly? EndDate)
{
try
{
// Ïðîâåðêà, ÷òî õîòÿ áû îäèí ïàðàìåòð çàäàí
if (!UserId.HasValue && !StartDate.HasValue && !EndDate.HasValue)
{
return BadRequest("At least one parameter (UserId, StartDate, EndDate) must be specified.");
}
// Âûïîëíåíèå óäàëåíèÿ â çàâèñèìîñòè îò ïàðàìåòðîâ
if (UserId.HasValue)
{
// Óäàëåíèå ïîñåùàåìîñòè ïî UserId
_presenceUseCase.DeletePresenceByUser(GroupId, UserId.Value);
}
else if (StartDate.HasValue && EndDate.HasValue)
{
// Óäàëåíèå ïîñåùàåìîñòè ïî äèàïàçîíó äàò
_presenceUseCase.DeletePresenceByDateRange(GroupId, StartDate.Value, EndDate.Value);
}
else
{
// Óäàëåíèå ïîñåùàåìîñòè ïî ãðóïïå (åñëè íå çàäàíî UserId èëè äèàïàçîí äàò)
_presenceUseCase.DeletePresenceByGroup(GroupId);
}
return Ok("Attendance records deleted successfully.");
}
catch (Exception ex)
{
return StatusCode(500, $"Internal server error: {ex.Message}");
}
}
[HttpPost("mark-absent")]
public IActionResult MarkUserAbsent([FromBody] MarkAbsenceRequest request)
{
_presenceUseCase.MarkUserAbsent(request.UserId, request.GroupId, request.Date, request.LessonNumber);
return Ok("User marked as absent.");
}
}