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; private readonly ILogger _logger; public PresenceController(PresenceUseCase presenceUseCase, ILogger logger){ _presenceUseCase = presenceUseCase; _logger = logger; } [HttpPost] public ActionResult GeneratePresence(int firstLesson, int lastLesson, int groupID, string date) { if (!DateOnly.TryParse(date, out var parseDate)) { _logger.LogWarning("Invalid date format"); return BadRequest("Invalid date format"); } var isGenerate = _presenceUseCase.GeneratePresence(firstLesson, lastLesson, groupID, parseDate); return Ok(true); } [HttpGet] public ActionResult 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 DeletePresenceByUserGuid(Guid userGuid) { if (userGuid == Guid.Empty){ _logger.LogWarning("Invalid Guid"); return BadRequest("Invalid request"); } var isDeleted = _presenceUseCase.DeletePresenceByUser(userGuid); if (isDeleted == false){ _logger.LogWarning("User not found"); return NotFound("User not found"); } return Ok(true); } [HttpDelete("group")] public ActionResult DeletePresenceByGroup(int groupID) { var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID); if (isDeleted == false){ _logger.LogWarning("Group not found"); return NotFound("Group not found"); } return Ok(true); } [HttpDelete("datarange")] public ActionResult DeletePresenceByDateRange(string startDate, string endDate) { if (!DateOnly.TryParse(startDate, out var parsedStartDate) || !DateOnly.TryParse(endDate, out var parsedEndDate)) { _logger.LogWarning("Invalid date format"); return BadRequest("Invalid date format"); } var isDeleted = _presenceUseCase.DeletePresenceByDateRange(parsedStartDate, parsedEndDate); if (isDeleted == false){ _logger.LogWarning("Presence not found"); return NotFound("Presence not found"); } return Ok(true); } [HttpPatch] public ActionResult UpdatePresence(int firstLesson, int lastLesson, string date, Guid UserGuid) { if (!DateOnly.TryParse(date, out var parseDate)) { _logger.LogWarning("Invalid date format"); return BadRequest("Invalid date format"); } var isUpdated = _presenceUseCase.IsAttedance(firstLesson, lastLesson, parseDate, UserGuid); if (isUpdated == false){ _logger.LogWarning("Presence not found"); return NotFound("Presence not found"); } return Ok(true); } }