presence/Presence.API/Controllers/PresenceController.cs

102 lines
2.9 KiB
C#
Raw Normal View History

2024-12-05 07:31:49 +00:00
using domain.UseCase;
using Microsoft.AspNetCore.Mvc;
using Presence.API.Response;
2024-12-10 05:26:38 +00:00
using domain.Request;
2024-12-05 07:31:49 +00:00
using System;
using System.Linq;
2024-12-04 12:21:15 +00:00
namespace Presence.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class PresenceController : ControllerBase
{
2024-12-05 07:31:49 +00:00
private readonly IPresenceUseCase _presenceUseCase;
public PresenceController(IPresenceUseCase presenceUseCase)
{
_presenceUseCase = presenceUseCase;
}
[HttpGet("/presence/{groupId}")]
2024-12-10 05:26:38 +00:00
public ActionResult<IEnumerable<PresenceResponse>> GetPresence(
2024-12-05 07:31:49 +00:00
int groupId,
[FromQuery] int? subject,
2024-12-10 05:26:38 +00:00
[FromQuery] DateOnly? date,
2024-12-05 07:31:49 +00:00
[FromQuery] int? student)
{
var presences = _presenceUseCase.GetPresence(groupId, subject, date, student)
.Select(p => new PresenceResponse
{
Id = p.Id,
StudentId = p.StudentId,
StudentName = $"{p.Student.LastName} {p.Student.FirstName} {p.Student.Patronymic}",
SubjectId = p.SubjectId,
SubjectName = p.Subject.Name,
2024-12-10 05:26:38 +00:00
TrafficId = p.TrafficId,
TrafficName = p.Traffic.Name,
LessonNumber = p.LessonNumber,
2024-12-05 07:31:49 +00:00
Date = p.Date
})
.ToList();
return Ok(presences);
}
2024-12-10 05:26:38 +00:00
[HttpPost("/presence")]
public IActionResult AddPresenceRecords([FromBody] AddPresenceRequest request)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
try
{
_presenceUseCase.AddPresenceRecords(request);
return Ok();
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
}
[HttpPut("/presence")]
public IActionResult UpdatePresenceRecords([FromBody] UpdatePresenceRequest request)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
try
{
_presenceUseCase.UpdatePresenceRecords(request);
return Ok();
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
}
[HttpDelete("/presence")]
public IActionResult DeleteAllPresence()
{
_presenceUseCase.DeleteAllPresence();
return NoContent();
}
[HttpDelete("/presence/group")]
public IActionResult DeleteGroupPresence([FromQuery] int? group)
{
if (!group.HasValue)
{
_presenceUseCase.DeleteAllPresence();
}
else
{
_presenceUseCase.DeleteGroupPresence(group.Value);
}
return NoContent();
}
2024-12-04 12:21:15 +00:00
}
}