presence/presence_api/Controllers/PresenceController.cs

112 lines
3.6 KiB
C#
Raw Permalink Normal View History

2024-11-11 07:01:04 +00:00
using Demo.Domain.Models;
using Demo.Domain.UseCase;
using Microsoft.AspNetCore.Mvc;
namespace presence_api.Controllers;
[ApiController]
[Route("api/[controller]")]
public class PresenceController: ControllerBase{
private readonly PresenceUseCase _presenceUseCase;
2024-11-14 13:33:46 +00:00
private readonly ILogger<PresenceController> _logger;
2024-11-11 07:01:04 +00:00
2024-11-14 13:33:46 +00:00
public PresenceController(PresenceUseCase presenceUseCase, ILogger<PresenceController> logger){
2024-11-11 07:01:04 +00:00
_presenceUseCase = presenceUseCase;
2024-11-14 13:33:46 +00:00
_logger = logger;
2024-11-11 07:01:04 +00:00
}
[HttpPost]
public ActionResult<bool> GeneratePresence(int firstLesson, int lastLesson, int groupID, string date) {
if (!DateOnly.TryParse(date, out var parseDate))
{
2024-11-14 13:33:46 +00:00
_logger.LogWarning("Invalid date format");
2024-11-11 07:01:04 +00:00
return BadRequest("Invalid date format");
}
var isGenerate = _presenceUseCase.GeneratePresence(firstLesson, lastLesson, groupID, parseDate);
return Ok(true);
}
[HttpGet]
2024-11-11 12:00:01 +00:00
public ActionResult<PresenceResponse> GetPresence(int groupID, string start = null, string end = null, Guid userGuid = default)
{
PresenceResponse? presences = null;
if (start != null && end != null)
{
DateOnly.TryParse(start, out DateOnly start1);
DateOnly.TryParse(end, out DateOnly end1);
presences = _presenceUseCase.GetPresencebyAll(groupID, start1, end1, userGuid);
} else {
presences = _presenceUseCase.GetPresencebyAll(groupID, null, null, userGuid);
}
return Ok(presences);
}
[HttpDelete("user")]
public ActionResult<bool> DeletePresenceByUserGuid(Guid userGuid)
{
if (userGuid == Guid.Empty){
2024-11-14 13:33:46 +00:00
_logger.LogWarning("Invalid Guid");
2024-11-11 12:00:01 +00:00
return BadRequest("Invalid request");
}
var isDeleted = _presenceUseCase.DeletePresenceByUser(userGuid);
if (isDeleted == false){
2024-11-14 13:33:46 +00:00
_logger.LogWarning("User not found");
2024-11-11 12:00:01 +00:00
return NotFound("User not found");
}
return Ok(true);
}
[HttpDelete("group")]
public ActionResult<bool> DeletePresenceByGroup(int groupID)
{
var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID);
if (isDeleted == false){
2024-11-14 13:33:46 +00:00
_logger.LogWarning("Group not found");
2024-11-11 12:00:01 +00:00
return NotFound("Group not found");
}
return Ok(true);
}
[HttpDelete("datarange")]
public ActionResult<bool> DeletePresenceByDateRange(string startDate, string endDate)
2024-11-11 07:01:04 +00:00
{
if (!DateOnly.TryParse(startDate, out var parsedStartDate) ||
!DateOnly.TryParse(endDate, out var parsedEndDate))
{
2024-11-14 13:33:46 +00:00
_logger.LogWarning("Invalid date format");
2024-11-11 07:01:04 +00:00
return BadRequest("Invalid date format");
}
2024-11-11 12:00:01 +00:00
var isDeleted = _presenceUseCase.DeletePresenceByDateRange(parsedStartDate, parsedEndDate);
if (isDeleted == false){
2024-11-14 13:33:46 +00:00
_logger.LogWarning("Presence not found");
2024-11-11 12:00:01 +00:00
return NotFound("Presence not found");
}
return Ok(true);
}
[HttpPatch]
public ActionResult<bool> UpdatePresence(int firstLesson, int lastLesson, string date, Guid UserGuid)
{
if (!DateOnly.TryParse(date, out var parseDate))
{
2024-11-14 13:33:46 +00:00
_logger.LogWarning("Invalid date format");
2024-11-11 12:00:01 +00:00
return BadRequest("Invalid date format");
}
var isUpdated = _presenceUseCase.IsAttedance(firstLesson, lastLesson, parseDate, UserGuid);
if (isUpdated == false){
2024-11-14 13:33:46 +00:00
_logger.LogWarning("Presence not found");
2024-11-11 12:00:01 +00:00
return NotFound("Presence not found");
}
return Ok(true);
2024-11-11 07:01:04 +00:00
}
}