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; } //get [HttpGet] public ActionResult GetPresence(int groupID, string start = null, string end = null, Guid userGuid = default) { PresenceGet? 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); } //post [HttpPost("generate")] public ActionResult GeneratePresence([FromBody] PresencePost request) { if (request == null){ _logger.LogWarning("NotFound"); return BadRequest("buba"); } DateOnly.TryParse(request.date, out DateOnly date); var isGenerated = _presenceUseCase.GeneratePresence(request.firstLesson, request.lastLesson, request.groupID, date); return Ok(isGenerated); } //delete [HttpDelete("gruop")] public ActionResult DeletePresenceByGroup(int groupID) { var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID); if (isDeleted == false) { _logger.LogWarning("NotFound"); return NotFound("buba"); } return isDeleted; } [HttpDelete("user")] public ActionResult DeletePresenceByUser(Guid userGuid) { var isDeleted = _presenceUseCase.DeletePresenceByUser(userGuid); if (isDeleted == false) { _logger.LogWarning("NotFound"); return NotFound("buba"); } return isDeleted; } [HttpDelete("range")] public ActionResult DeletePresenceByRange([FromBody] PresenceDeleteByRange request) { DateOnly.TryParse(request.start, out DateOnly start); DateOnly.TryParse(request.end, out DateOnly end); var isDeleted = _presenceUseCase.DeletePresenceByRange(start, end); if (isDeleted == false) { _logger.LogWarning("NotFound"); return NotFound("buba"); } return isDeleted; } //update [HttpPatch] public ActionResult UpdatePresence([FromBody] PresenceUpdate request) { DateOnly.TryParse(request.date, out DateOnly date); var isUpdated = _presenceUseCase.IsAttedance(request.firstLesson, request.lastLesson, date, request.UserGuid); if (isUpdated == false) { _logger.LogWarning("NotFound"); return NotFound("buba"); } return isUpdated; } }