105 lines
3.1 KiB
C#
105 lines
3.1 KiB
C#
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<PresenceController> _logger;
|
|
|
|
public PresenceController(PresenceUseCase presenceUseCase, ILogger<PresenceController> logger)
|
|
{
|
|
_presenceUseCase = presenceUseCase;
|
|
_logger = logger;
|
|
}
|
|
|
|
//get
|
|
|
|
[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){
|
|
_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<bool> DeletePresenceByGroup(int groupID)
|
|
{
|
|
var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID);
|
|
if (isDeleted == false)
|
|
{
|
|
_logger.LogWarning("NotFound");
|
|
return NotFound("buba");
|
|
}
|
|
return isDeleted;
|
|
}
|
|
|
|
[HttpDelete("user")]
|
|
public ActionResult<bool> DeletePresenceByUser(Guid userGuid)
|
|
{
|
|
var isDeleted = _presenceUseCase.DeletePresenceByUser(userGuid);
|
|
if (isDeleted == false)
|
|
{
|
|
_logger.LogWarning("NotFound");
|
|
return NotFound("buba");
|
|
}
|
|
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);
|
|
if (isDeleted == false)
|
|
{
|
|
_logger.LogWarning("NotFound");
|
|
return NotFound("buba");
|
|
}
|
|
return isDeleted;
|
|
}
|
|
|
|
//update
|
|
|
|
[HttpPatch]
|
|
public ActionResult<bool> 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;
|
|
}
|
|
} |