presence/presence_api/Controllers/PresenceController.cs
2024-11-11 15:00:01 +03:00

104 lines
3.1 KiB
C#

using Demo.Domain.Models;
using Demo.Domain.UseCase;
using Microsoft.AspNetCore.Mvc;
using Npgsql.TypeMapping;
namespace presence_api.Controllers;
[ApiController]
[Route("api/[controller]")]
public class PresenceController: ControllerBase{
private readonly PresenceUseCase _presenceUseCase;
public PresenceController(PresenceUseCase presenceUseCase){
_presenceUseCase = presenceUseCase;
}
[HttpPost]
public ActionResult<bool> GeneratePresence(int firstLesson, int lastLesson, int groupID, string date) {
if (!DateOnly.TryParse(date, out var parseDate))
{
return BadRequest("Invalid date format");
}
var isGenerate = _presenceUseCase.GeneratePresence(firstLesson, lastLesson, groupID, parseDate);
return Ok(true);
}
[HttpGet]
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){
return BadRequest("Invalid request");
}
var isDeleted = _presenceUseCase.DeletePresenceByUser(userGuid);
if (isDeleted == false){
return NotFound("User not found");
}
return Ok(true);
}
[HttpDelete("group")]
public ActionResult<bool> DeletePresenceByGroup(int groupID)
{
var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID);
if (isDeleted == false){
return NotFound("Group not found");
}
return Ok(true);
}
[HttpDelete("datarange")]
public ActionResult<bool> DeletePresenceByDateRange(string startDate, string endDate)
{
if (!DateOnly.TryParse(startDate, out var parsedStartDate) ||
!DateOnly.TryParse(endDate, out var parsedEndDate))
{
return BadRequest("Invalid date format");
}
var isDeleted = _presenceUseCase.DeletePresenceByDateRange(parsedStartDate, parsedEndDate);
if (isDeleted == false){
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))
{
return BadRequest("Invalid date format");
}
var isUpdated = _presenceUseCase.IsAttedance(firstLesson, lastLesson, parseDate, UserGuid);
if (isUpdated == false){
return NotFound("Presence not found");
}
return Ok(true);
}
}