102 lines
2.9 KiB
C#
102 lines
2.9 KiB
C#
using domain.UseCase;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Presence.API.Response;
|
|
using domain.Request;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace Presence.API.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class PresenceController : ControllerBase
|
|
{
|
|
private readonly IPresenceUseCase _presenceUseCase;
|
|
|
|
public PresenceController(IPresenceUseCase presenceUseCase)
|
|
{
|
|
_presenceUseCase = presenceUseCase;
|
|
}
|
|
|
|
[HttpGet("/presence/{groupId}")]
|
|
public ActionResult<IEnumerable<PresenceResponse>> GetPresence(
|
|
int groupId,
|
|
[FromQuery] int? subject,
|
|
[FromQuery] DateOnly? date,
|
|
[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,
|
|
TrafficId = p.TrafficId,
|
|
TrafficName = p.Traffic.Name,
|
|
LessonNumber = p.LessonNumber,
|
|
Date = p.Date
|
|
})
|
|
.ToList();
|
|
|
|
return Ok(presences);
|
|
}
|
|
|
|
[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();
|
|
}
|
|
}
|
|
}
|