Demo/Domain/UseCase/PresenceUseCase.cs
2024-10-23 22:46:46 +03:00

111 lines
4.5 KiB
C#

using Demo.Data.Repository;
using Demo.domain.Models;
namespace Demo.Domain.UseCase
{
public class PresenceUseCase : IPresenceUseCase
{
private readonly IUserRepository _repositoryUserImpl;
private readonly IPresenceRepository _repositoryPresenceImpl;
private readonly IGroupRepository _repositoryGroupImpl;
public PresenceUseCase(IPresenceRepository presenceRepository, IUserRepository repositoryUserImpl, IGroupRepository repositoryGroupImpl)
{
_repositoryPresenceImpl = presenceRepository;
_repositoryGroupImpl = repositoryGroupImpl;
_repositoryUserImpl = repositoryUserImpl;
}
public List<Presence> GetPresencesByGroup(int groupId)
{
var usersByGroup = _repositoryUserImpl.GetAllUser()
.Where(user => user.GroupID == groupId).ToList();
var presenceByGroup = _repositoryPresenceImpl.GetPresencesByGroup()
.Where(x => usersByGroup.Any(user => user.Guid == x.UserGuid))
.Select(presence => new Presence{
User = new User{
Guid = presence.UserGuid,
Group = new Group{
Id = groupId,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.Id == groupId).Name
},
FIO = usersByGroup.First(user => user.Guid == presence.UserGuid).FIO,
},
LessonNumber = presence.LessonNumber,
Date = presence.Date,
IsAttedance = presence.IsAttedance
}).ToList();
return presenceByGroup;
}
public List<Presence> GetPresencesByGroupAndDate(int groupId, DateOnly date)
{
var usersByGroup = _repositoryUserImpl.GetAllUser()
.Where(user => user.GroupID == groupId).ToList();
var presenceByGroupAndDate = _repositoryPresenceImpl.GetPresencesByGroup()
.Where(x => usersByGroup.Any(user => user.Guid == x.UserGuid && x.Date == date))
.Select(presence => new Presence{
User = new User{
Guid = presence.UserGuid,
Group = new Group{
Id = groupId,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.Id == groupId).Name
},
FIO = usersByGroup.First(user => user.Guid == presence.UserGuid).FIO,
},
LessonNumber = presence.LessonNumber,
Date = presence.Date,
IsAttedance = presence.IsAttedance
}).ToList();
return presenceByGroupAndDate;
}
public bool UpdateAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){
_repositoryPresenceImpl.UpdateAttedance(firstLesson, lastLesson, date, UserGuid);
return true;
}
public bool GeneratePresence(int firstLesson, int lastLesson, int groupId, DateOnly date){
List<Presence> presenceList = new List<Presence>{};
var usersByGroup = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == groupId);
for (int i = firstLesson; i <= lastLesson; i++){
foreach(UserLocalEntity user in usersByGroup){
presenceList.Add(new Presence{
User = new User{
Group = new Group{
Id = groupId,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.Id == groupId).Name
},
FIO = usersByGroup.First(user2 => user2.Guid == user.Guid).FIO,
Guid = user.Guid
},
IsAttedance = true,
Date = date,
LessonNumber = i});
}
}
var presenceLocalEntity = presenceList.Select(x => new PresenceLocalEntity{
UserGuid = x.User.Guid,
IsAttedance = x.IsAttedance,
LessonNumber = x.LessonNumber,
Date = x.Date
}).ToList();
_repositoryPresenceImpl.SavePresence(presenceLocalEntity);
return true;
}
public bool GenerateWeekPresence(int firstLesson, int lastLesson, int groupId, DateOnly date){
for (int i = 0; i < 8; i++){
GeneratePresence(firstLesson, lastLesson, groupId, date.AddDays(i));
}
return true;
}
}
}