Presence.Desktop/presence_api/Controllers/PresenceController/PresenceController.cs

89 lines
3.1 KiB
C#
Raw Normal View History

2024-12-20 08:49:17 +00:00
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
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
_presenceUseCase.UpdateAttendance(attendanceList);
return Ok("Attendance updated successfully.");
}
catch (Exception ex)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD> <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>
if (!UserId.HasValue && !StartDate.HasValue && !EndDate.HasValue)
{
return BadRequest("At least one parameter (UserId, StartDate, EndDate) must be specified.");
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (UserId.HasValue)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> UserId
_presenceUseCase.DeletePresenceByUser(GroupId, UserId.Value);
}
else if (StartDate.HasValue && EndDate.HasValue)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>
_presenceUseCase.DeletePresenceByDateRange(GroupId, StartDate.Value, EndDate.Value);
}
else
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UserId <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>)
_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.");
}
}