diff --git a/domain/Models/Presence.cs b/domain/Models/Presence.cs index 96aa6d6..46112ff 100644 --- a/domain/Models/Presence.cs +++ b/domain/Models/Presence.cs @@ -16,4 +16,17 @@ namespace Demo.Domain.Models public required int groupID {get; set; } public required DateOnly date {get; set; } } + + public class PresenceGetRequest + { + public int groupID { get; set; } + public DateOnly startDate { get; set; } + public DateOnly endDate { get; set; } + } + + public class PresenceResponce + { + public string GroupName { get; set; } + public List Users { get; set; } + } } \ No newline at end of file diff --git a/domain/Models/User.cs b/domain/Models/User.cs index dbc22ad..b0efb91 100644 --- a/domain/Models/User.cs +++ b/domain/Models/User.cs @@ -46,4 +46,12 @@ namespace Demo.Domain.Models { public List UsersGuid { get; set; } } + + public class UserPresenceInfo + { + public string FIO { get; set; } + public int LessonNumber { get; set; } + public DateOnly Date { get; set; } + public bool IsAttendance { get; set; } + } } \ No newline at end of file diff --git a/domain/UseCase/PresenceUseCase.cs b/domain/UseCase/PresenceUseCase.cs index f3170db..510e4aa 100644 --- a/domain/UseCase/PresenceUseCase.cs +++ b/domain/UseCase/PresenceUseCase.cs @@ -130,6 +130,33 @@ namespace Demo.Domain.UseCase return presenceByGroup; } + public PresenceResponce GetPresenceByDataRangeAndGroup(int groupID, DateOnly? startDate, DateOnly? endDate) { + + var group = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.ID == groupID); + var usersByGroup = _repositoryUserImpl.GetAllUser() + .Where(user => user.GroupID == groupID).ToList(); + + var presences = _repositoryPresenceImpl.GetAllPresences() + .Where(presence => + usersByGroup.Any(user => user.Guid == presence.UserGuid) && + presence.Date >= startDate && presence.Date <= endDate) + .ToList(); + + var userPresenceInfos = presences.Select(presence => new UserPresenceInfo + { + FIO = usersByGroup.First(user => user.Guid == presence.UserGuid).FIO, + LessonNumber = presence.LessonNumber, + Date = presence.Date, + IsAttendance = presence.IsAttedance + }).ToList(); + + return new PresenceResponce + { + GroupName = group.Name, + Users = userPresenceInfos + }; + } + public bool IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){ _repositoryPresenceImpl.IsAttedance(firstLesson, lastLesson, date, UserGuid); return true; diff --git a/presence_api/Controllers/PresenceController.cs b/presence_api/Controllers/PresenceController.cs new file mode 100644 index 0000000..74df57e --- /dev/null +++ b/presence_api/Controllers/PresenceController.cs @@ -0,0 +1,44 @@ +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 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 GetPresence(int groupID, string startDate, string endDate) + { + if (!DateOnly.TryParse(startDate, out var parsedStartDate) || + !DateOnly.TryParse(endDate, out var parsedEndDate)) + { + return BadRequest("Invalid date format"); + } + + var presence = _presenceUseCase.GetPresenceByDataRangeAndGroup(groupID, parsedStartDate, parsedEndDate); + return Ok(presence); + } +} \ No newline at end of file