All presence

This commit is contained in:
1billy17 2024-10-23 22:46:46 +03:00
parent 6f930408b5
commit cd64ec54de
8 changed files with 232 additions and 3 deletions

View File

@ -6,12 +6,11 @@ using System.Threading.Tasks;
namespace Demo.domain.Models
{
internal class PresenceLocalEntity
public class PresenceLocalEntity
{
public required Guid UserGuid { get; set; }
public bool IsAttedance { get; set; } = true;
public required DateOnly Date { get; set; }
public required int LessonNumber { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using Demo.domain.Models;
namespace Demo.Data.Repository
{
public interface IPresenceRepository
{
List<PresenceLocalEntity> GetPresences();
List<PresenceLocalEntity> GetPresencesByGroup();
List<PresenceLocalEntity> GetPresencesByGroupAndDate();
public List<PresenceLocalEntity> SavePresence(List<PresenceLocalEntity> presenceLocalEntities);
void UpdateAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid);
}
}

View File

@ -0,0 +1,37 @@
using Demo.domain.Models;
namespace Demo.Data.Repository
{
public class PresenceRepositoryImpl : IPresenceRepository
{
public List<PresenceLocalEntity> GetAllPresences = new List<PresenceLocalEntity>();
public List<PresenceLocalEntity> GetPresences()
{
return GetAllPresences;
}
public List<PresenceLocalEntity> GetPresencesByGroup()
{
return GetAllPresences;
}
public List<PresenceLocalEntity> GetPresencesByGroupAndDate()
{
return GetAllPresences;
}
public List<PresenceLocalEntity> SavePresence(List<PresenceLocalEntity> presenceLocalEntities){
GetAllPresences.AddRange(presenceLocalEntities);
return presenceLocalEntities;
}
public void UpdateAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){
foreach(PresenceLocalEntity presence in GetAllPresences.Where(x => x.LessonNumber >= firstLesson && x.LessonNumber <= lastLesson && x.Date == date)){
if (presence.UserGuid == UserGuid){
presence.IsAttedance = false;
}
}
}
}
}

View File

@ -0,0 +1,13 @@
using Demo.domain.Models;
namespace Demo.Domain.UseCase
{
public interface IPresenceUseCase
{
List<Presence> GetPresencesByGroup(int groupId);
List<Presence> GetPresencesByGroupAndDate(int groupId, DateOnly date);
bool UpdateAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid);
bool GeneratePresence(int firstLesson, int lastLesson, int groupId, DateOnly date);
bool GenerateWeekPresence(int firstLesson, int lastLesson, int groupId, DateOnly date);
}
}

View File

@ -0,0 +1,111 @@
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;
}
}
}

View File

@ -9,10 +9,13 @@ IServiceCollection services = new ServiceCollection();
services
.AddSingleton<IGroupRepository, GroupRepositoryImpl>()
.AddSingleton<IUserRepository, UserRepositoryImpl>()
.AddSingleton<IPresenceRepository, PresenceRepositoryImpl>()
.AddSingleton<IGroupUseCase, GroupUseCase>()
.AddSingleton<IUserUseCase, UserUseCase>()
.AddSingleton<IPresenceUseCase, PresenceUseCase>()
.AddSingleton<UserConsoleUI>()
.AddSingleton<GroupConsoleUI>()
.AddSingleton<PresenceConsoleUI>()
.AddSingleton<MainMenuUI>();
var serviceProvider = services.BuildServiceProvider();

View File

@ -13,10 +13,12 @@ namespace Demo.UI
UserConsoleUI _userConsoleUI;
GroupConsoleUI _groupConsoleUI;
PresenceConsoleUI _presenceConsoleUI;
public MainMenuUI(IUserUseCase userUseCase, IGroupUseCase groupUseCase) {
public MainMenuUI(IUserUseCase userUseCase, IGroupUseCase groupUseCase, IPresenceUseCase presenceUseCase) {
_userConsoleUI = new UserConsoleUI(userUseCase);
_groupConsoleUI = new GroupConsoleUI(groupUseCase);
_presenceConsoleUI = new PresenceConsoleUI(presenceUseCase);
DisplayMenu();
}
@ -33,6 +35,11 @@ namespace Demo.UI
case "5": _groupConsoleUI.DisplayAllGroups(); break;
case "6": _groupConsoleUI.CreateNewGroup(Group.Parse(Console.ReadLine())); break;
case "7": _groupConsoleUI.UpdateGroupName(Group.Parse(Console.ReadLine())); break;
case "8": _presenceConsoleUI.DisplayPresenceByGroup(Convert.ToInt32(Console.ReadLine())); break;
case "9": _presenceConsoleUI.DisplayPresenceByGroupByTime(Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine()))); break;
case "10": _presenceConsoleUI.GeneratePresence(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine()))); break;
case "11": _presenceConsoleUI.GeneratePresenceWeek(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine()))); break;
case "12": _presenceConsoleUI.IsAttedance(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine())), Guid.Parse(Console.ReadLine())); break;
default: DisplayMenu();
break;

46
UI/PresenceConsole.cs Normal file
View File

@ -0,0 +1,46 @@
using Demo.Domain.UseCase;
using Demo.domain.Models;
using System.Text;
namespace Demo.UI
{
public class PresenceConsoleUI {
IPresenceUseCase _presenceUseCase;
public PresenceConsoleUI (IPresenceUseCase presenceUseCase){
_presenceUseCase = presenceUseCase;
}
public void DisplayPresenceByGroup(int groupID){
StringBuilder stringBuilder = new StringBuilder();
foreach(Presence presence in _presenceUseCase.GetPresencesByGroup(groupID)){
stringBuilder.AppendLine($"{presence.User.FIO}, {presence.User.Group.Name}, {presence.IsAttedance}, {presence.Date}, {presence.LessonNumber}");
}
Console.WriteLine(stringBuilder);
}
public void DisplayPresenceByGroupByTime(int groupID, DateOnly date){
StringBuilder stringBuilder = new StringBuilder();
foreach(Presence presence in _presenceUseCase.GetPresencesByGroupAndDate(groupID, date)){
stringBuilder.AppendLine($"{presence.User.FIO}, {presence.User.Group.Name}, {presence.IsAttedance}, {presence.Date}, {presence.LessonNumber}");
}
Console.WriteLine(stringBuilder);
}
public void GeneratePresence(int firstLesson, int lastLesson, int groupID, DateOnly date){
string output = _presenceUseCase.GeneratePresence(firstLesson, lastLesson, groupID, date) ? "Сгенерированно" : "Не сгенерированно";
Console.WriteLine(output);
}
public void GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date){
string output = _presenceUseCase.GenerateWeekPresence(firstLesson, lastLesson, groupID, date) ? "Сгенерированно" : "Не сгенерированно";
Console.WriteLine(output);
}
public void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){
string output = _presenceUseCase.UpdateAttedance(firstLesson, lastLesson, date, UserGuid) ? "Обновлено" : "Не обновлено";
Console.WriteLine(output);
}
}
}