From 9b4ef19cbcdc36671056df4a6fc5a89b5bbcac98 Mon Sep 17 00:00:00 2001 From: 1billy17 Date: Mon, 11 Nov 2024 15:00:01 +0300 Subject: [PATCH] all presence --- data/Repository/IPresenceRepository.cs | 3 + data/Repository/PresenceRepositoryImpl.cs | 31 ++++++++ domain/Models/Presence.cs | 10 +-- domain/UseCase/IPresenceUseCase.cs | 4 ++ domain/UseCase/PresenceUseCase.cs | 72 +++++++++++++------ .../Controllers/PresenceController.cs | 66 ++++++++++++++++- 6 files changed, 151 insertions(+), 35 deletions(-) diff --git a/data/Repository/IPresenceRepository.cs b/data/Repository/IPresenceRepository.cs index 8e63c14..ff15fac 100644 --- a/data/Repository/IPresenceRepository.cs +++ b/data/Repository/IPresenceRepository.cs @@ -8,5 +8,8 @@ namespace Demo.Data.Repository void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid); List GeneratePresence(List presenceLocalEntities); bool DeletePresence(); + bool DeletePresenceByUserGuid(Guid UserGuid); + bool DeletePresenceByGroup(int groupID); + bool DeletePresenceByDateRange(DateOnly startDate, DateOnly endDate); } } \ No newline at end of file diff --git a/data/Repository/PresenceRepositoryImpl.cs b/data/Repository/PresenceRepositoryImpl.cs index d4b2276..e9b0f5f 100644 --- a/data/Repository/PresenceRepositoryImpl.cs +++ b/data/Repository/PresenceRepositoryImpl.cs @@ -43,6 +43,37 @@ namespace Demo.Data.Repository return true; } + + public bool DeletePresenceByUserGuid(Guid UserGuid) { + var delRecords = _remoteDatabaseContext.PresenceDaos + .Where(x => x.UserGuid == UserGuid).ToList(); + + _remoteDatabaseContext.PresenceDaos.RemoveRange(delRecords); + _remoteDatabaseContext.SaveChanges(); + + return true; + } + + public bool DeletePresenceByGroup(int groupID) { + var delRecords = _remoteDatabaseContext.PresenceDaos + .Where(y => y.userDAO.GroupID == groupID); + + _remoteDatabaseContext.PresenceDaos.RemoveRange(delRecords); + _remoteDatabaseContext.SaveChanges(); + + return true; + } + + public bool DeletePresenceByDateRange(DateOnly startDate, DateOnly endDate) { + var delRecords = _remoteDatabaseContext.PresenceDaos + .Where(x => x.Date >= startDate && x.Date <= endDate); + + _remoteDatabaseContext.PresenceDaos.RemoveRange(delRecords); + _remoteDatabaseContext.SaveChanges(); + + return true; + } + public void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){ var presencesToUpdate = _remoteDatabaseContext.PresenceDaos .Where(x => x.LessonNumber >= firstLesson diff --git a/domain/Models/Presence.cs b/domain/Models/Presence.cs index 46112ff..a66a848 100644 --- a/domain/Models/Presence.cs +++ b/domain/Models/Presence.cs @@ -8,14 +8,6 @@ namespace Demo.Domain.Models public required int LessonNumber { get; set; } } - - public class PresencePost - { - public required int firstLesson {get; set; } - public required int lastLesson {get; set; } - public required int groupID {get; set; } - public required DateOnly date {get; set; } - } public class PresenceGetRequest { @@ -24,7 +16,7 @@ namespace Demo.Domain.Models public DateOnly endDate { get; set; } } - public class PresenceResponce + public class PresenceResponse { public string GroupName { get; set; } public List Users { get; set; } diff --git a/domain/UseCase/IPresenceUseCase.cs b/domain/UseCase/IPresenceUseCase.cs index 85af733..1047670 100644 --- a/domain/UseCase/IPresenceUseCase.cs +++ b/domain/UseCase/IPresenceUseCase.cs @@ -13,5 +13,9 @@ namespace Demo.Domain.UseCase bool GeneratePresence(int firstLesson, int lastLesson, int groupID, DateOnly date); bool GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date); bool DeletePresence(); + bool DeletePresenceByUser(Guid userGuid); + bool DeletePresenceByGroup(int groupID); + bool DeletePresenceByDateRange(DateOnly startDate, DateOnly endDate); + PresenceResponse GetPresencebyAll(int GroupID, DateOnly? start = null, DateOnly? end = null, Guid userGuid = default); } } \ No newline at end of file diff --git a/domain/UseCase/PresenceUseCase.cs b/domain/UseCase/PresenceUseCase.cs index 510e4aa..0073e50 100644 --- a/domain/UseCase/PresenceUseCase.cs +++ b/domain/UseCase/PresenceUseCase.cs @@ -130,31 +130,45 @@ 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 + public PresenceResponse GetPresencebyAll(int GroupID, DateOnly? start = null, DateOnly? end = null, Guid userGuid = default){ + IEnumerable? users = null; + if (userGuid == default) { - 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 + users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == GroupID); + } else { + users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == GroupID && x.Guid == userGuid); + } + var usersFor = new List(); + foreach(var user in users){ + IEnumerable presences; + if (userGuid != default){ + presences = _repositoryPresenceImpl.GetAllPresences().Where(x => x.UserGuid == userGuid).ToList(); + }else{ + presences = _repositoryPresenceImpl.GetAllPresences().ToList(); + } + foreach(var presence in presences) + { + if (start.HasValue && end.HasValue) + { + if (presence.Date >= start && presence.Date <= end) + { + usersFor.Add(new UserPresenceInfo{ + FIO = user.FIO, LessonNumber = presence.LessonNumber, Date = presence.Date, IsAttendance = presence.IsAttedance + }); + } + } else + { + usersFor.Add(new UserPresenceInfo{ + FIO = user.FIO, LessonNumber = presence.LessonNumber, Date = presence.Date, IsAttendance = presence.IsAttedance + }); + } + } + } + var presenceGet = new PresenceResponse{ + GroupName = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(x => x.ID == GroupID).Name, + Users = usersFor }; + return presenceGet; } public bool IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){ @@ -209,6 +223,18 @@ namespace Demo.Domain.UseCase return _repositoryPresenceImpl.DeletePresence(); } + public bool DeletePresenceByUser(Guid userGuid) { + return _repositoryPresenceImpl.DeletePresenceByUserGuid(userGuid); + } + + public bool DeletePresenceByGroup(int groupID) { + return _repositoryPresenceImpl.DeletePresenceByGroup(groupID); + } + + public bool DeletePresenceByDateRange(DateOnly startDate, DateOnly endDate) { + return _repositoryPresenceImpl.DeletePresenceByDateRange(startDate, endDate); + } + public bool GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date){ for (int i = 0; i < 8; i++){ GeneratePresence(firstLesson, lastLesson, groupID, date.AddDays(i)); diff --git a/presence_api/Controllers/PresenceController.cs b/presence_api/Controllers/PresenceController.cs index 74df57e..56ae8b2 100644 --- a/presence_api/Controllers/PresenceController.cs +++ b/presence_api/Controllers/PresenceController.cs @@ -30,7 +30,46 @@ public class PresenceController: ControllerBase{ } [HttpGet] - public ActionResult GetPresence(int groupID, string startDate, string endDate) + public ActionResult 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 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 DeletePresenceByGroup(int groupID) + { + var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID); + if (isDeleted == false){ + return NotFound("Group not found"); + } + return Ok(true); + } + + [HttpDelete("datarange")] + public ActionResult DeletePresenceByDateRange(string startDate, string endDate) { if (!DateOnly.TryParse(startDate, out var parsedStartDate) || !DateOnly.TryParse(endDate, out var parsedEndDate)) @@ -38,7 +77,28 @@ public class PresenceController: ControllerBase{ return BadRequest("Invalid date format"); } - var presence = _presenceUseCase.GetPresenceByDataRangeAndGroup(groupID, parsedStartDate, parsedEndDate); - return Ok(presence); + var isDeleted = _presenceUseCase.DeletePresenceByDateRange(parsedStartDate, parsedEndDate); + if (isDeleted == false){ + return NotFound("Presence not found"); + } + + return Ok(true); } + + [HttpPatch] + public ActionResult 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); + } + } \ No newline at end of file