presence/presence_api/Controllers/PresenceController.cs

105 lines
3.1 KiB
C#
Raw Normal View History

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 12:30:27 +00:00
private readonly ILogger<PresenceController> _logger;
2024-11-14 12:30:27 +00:00
public PresenceController(PresenceUseCase presenceUseCase, ILogger<PresenceController> logger)
{
_presenceUseCase = presenceUseCase;
2024-11-14 12:30:27 +00:00
_logger = logger;
}
//get
2024-11-11 11:49:39 +00:00
[HttpGet]
public ActionResult<PresenceGet> 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<bool> GeneratePresence([FromBody] PresencePost request)
{
if (request == null){
2024-11-14 12:30:27 +00:00
_logger.LogWarning("NotFound");
return BadRequest("buba");
}
2024-11-11 11:49:39 +00:00
DateOnly.TryParse(request.date, out DateOnly date);
var isGenerated = _presenceUseCase.GeneratePresence(request.firstLesson, request.lastLesson, request.groupID, date);
return Ok(isGenerated);
}
//delete
2024-11-11 11:49:39 +00:00
[HttpDelete("gruop")]
public ActionResult<bool> DeletePresenceByGroup(int groupID)
{
var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID);
2024-11-14 12:30:27 +00:00
if (isDeleted == false)
{
_logger.LogWarning("NotFound");
return NotFound("buba");
}
2024-11-11 11:49:39 +00:00
return isDeleted;
}
[HttpDelete("user")]
public ActionResult<bool> DeletePresenceByUser(Guid userGuid)
{
var isDeleted = _presenceUseCase.DeletePresenceByUser(userGuid);
2024-11-14 12:30:27 +00:00
if (isDeleted == false)
{
_logger.LogWarning("NotFound");
return NotFound("buba");
}
2024-11-11 11:49:39 +00:00
return isDeleted;
}
[HttpDelete("range")]
public ActionResult<bool> DeletePresenceByRange([FromBody] PresenceDeleteByRange request)
{
DateOnly.TryParse(request.start, out DateOnly start);
DateOnly.TryParse(request.end, out DateOnly end);
var isDeleted = _presenceUseCase.DeletePresenceByRange(start, end);
2024-11-14 12:30:27 +00:00
if (isDeleted == false)
{
_logger.LogWarning("NotFound");
return NotFound("buba");
}
2024-11-11 11:49:39 +00:00
return isDeleted;
}
//update
2024-11-11 11:49:39 +00:00
[HttpPatch]
public ActionResult<bool> UpdatePresence([FromBody] PresenceUpdate request)
{
DateOnly.TryParse(request.date, out DateOnly date);
2024-11-14 12:30:27 +00:00
var isUpdated = _presenceUseCase.IsAttedance(request.firstLesson, request.lastLesson, date, request.UserGuid);
if (isUpdated == false)
{
_logger.LogWarning("NotFound");
return NotFound("buba");
}
return isUpdated;
2024-11-11 11:49:39 +00:00
}
}