all presence

This commit is contained in:
1billy17 2024-11-11 15:00:01 +03:00
parent af64fee08f
commit 9b4ef19cbc
6 changed files with 151 additions and 35 deletions

View File

@ -8,5 +8,8 @@ namespace Demo.Data.Repository
void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid);
List<PresenceLocalEntity> GeneratePresence(List<PresenceLocalEntity> presenceLocalEntities);
bool DeletePresence();
bool DeletePresenceByUserGuid(Guid UserGuid);
bool DeletePresenceByGroup(int groupID);
bool DeletePresenceByDateRange(DateOnly startDate, DateOnly endDate);
}
}

View File

@ -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

View File

@ -9,14 +9,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
{
public int groupID { get; set; }
@ -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<UserPresenceInfo> Users { get; set; }

View File

@ -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);
}
}

View File

@ -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<UserLocalEntity>? 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<UserPresenceInfo>();
foreach(var user in users){
IEnumerable<PresenceLocalEntity> 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));

View File

@ -30,7 +30,46 @@ public class PresenceController: ControllerBase{
}
[HttpGet]
public ActionResult<PresenceResponce> GetPresence(int groupID, string startDate, string endDate)
public ActionResult<PresenceResponse> 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<bool> 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<bool> DeletePresenceByGroup(int groupID)
{
var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID);
if (isDeleted == false){
return NotFound("Group not found");
}
return Ok(true);
}
[HttpDelete("datarange")]
public ActionResult<bool> 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<bool> 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);
}
}