presence_new/presence_api/Controllers/PresenceController/PresenceController.cs
2024-11-18 18:54:36 +03:00

94 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using domain.Models.ResponseModels;
using Microsoft.AspNetCore.Mvc;
using presence.domain.Models;
using presence.domain.UseCase;
namespace presence_api.Controllers.PresenceController;
[ApiController]
[Route("api/[controller]")]
public class PresenceController: ControllerBase
{
private readonly PresenceUseCase _presenceUseCase;
public PresenceController(PresenceUseCase presenceUseCase)
{
_presenceUseCase = presenceUseCase;
}
[HttpGet]
public ActionResult<IEnumerable<Presence>> GetPresence([FromQuery] int GroupId,
[FromQuery] string StartData, [FromQuery] string EndData,
[FromQuery] int UserId)
{
DateOnly startData;
DateOnly endData;
bool isParsed = DateOnly.TryParse(StartData, out startData);
bool isParse = DateOnly.TryParse(EndData, out endData);
if (!isParsed)
{
return BadRequest("Неверный формат даты начала.");
}
if (!isParse)
{
return BadRequest("Неверный формат даты конца.");
}
return Ok(_presenceUseCase.GetPresence(GroupId, startData, endData, UserId));
}
[HttpPost]
public ActionResult<IEnumerable<Presence>> PostPresence([FromQuery] int GroupId,
[FromQuery] string StartData, [FromQuery] string EndData, [FromQuery] int UserId)
{
return Ok(_presenceUseCase.AddPresence(StartData, EndData, GroupId));
}
[HttpDelete]
public ActionResult<IEnumerable<PresenceResponse>> DeletePresenceByGroup([FromQuery] int GroupId)
{
return Ok(_presenceUseCase.DeletePresenceByGroup(GroupId));
}
[HttpDelete]
public ActionResult<IEnumerable<PresenceResponse>> DeletePresenceByUser([FromQuery] int UserId)
{
return Ok(_presenceUseCase.DeletePresenceByUser(UserId));
}
[HttpDelete]
public ActionResult<IEnumerable<PresenceResponse>> DeletePresenceByDate([FromQuery] string StartData, [FromQuery] string EndData)
{
DateOnly startData;
DateOnly endData;
bool isParsed = DateOnly.TryParse(StartData, out startData);
bool isParse = DateOnly.TryParse(EndData, out endData);
if (!isParsed)
{
return BadRequest("Неверный формат даты начала.");
}
if (!isParse)
{
return BadRequest("Неверный формат даты конца.");
}
return Ok(_presenceUseCase.DeletePresenceByDate(startData, endData));
}
[HttpPost]
public ActionResult<PresenceResponse> UpdateAttendance([FromQuery] int FirstClass,
[FromQuery] int LastClass, [FromQuery] string Data,
[FromQuery] int UserId)
{
DateOnly data;
bool isParsed = DateOnly.TryParse(Data, out data);
if (!isParsed)
{
return BadRequest("Неверный формат даты.");
}
return Ok(_presenceUseCase.UncheckAttendence(FirstClass, LastClass, data, UserId));
}
}