changed to sqlrepositories

This commit is contained in:
Dasha06 2024-11-06 07:10:09 +03:00
parent 0705829116
commit bc674c9649
19 changed files with 281 additions and 205 deletions

View File

@ -7,7 +7,7 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase.DAO
{ {
public class GroupDao public class GroupDao
{ {
public required int Id {get; set;} public int Id {get; set;}
public required string Name {get; set;} public required string Name {get; set;}
public IEnumerable<UserDao> Users {get; set;} public IEnumerable<UserDao> Users {get; set;}

View File

@ -10,6 +10,8 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase.DAO
public required DateOnly Date {get; set;} public required DateOnly Date {get; set;}
public int ClassNumber {get; set;} public int ClassNumber {get; set;}
public bool IsAttendence {get; set;} = true; public bool IsAttendence {get; set;} = true;
public int UserId {get; set;}
public required UserDao User {get; set;} public required UserDao User {get; set;}
public int GroupId {get; set;}
} }
} }

View File

@ -8,7 +8,9 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase.DAO
public class UserDao public class UserDao
{ {
public required string FIO {get; set; } public required string FIO {get; set; }
public Guid Guid {get; set;}
public required GroupDao Group {get; set;} public int UserId { get; set; }
public required int GroupId {get; set;}
public GroupDao Group {get; set;}
} }
} }

View File

@ -10,15 +10,15 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase
public class RemoteDataBaseContext: DbContext public class RemoteDataBaseContext: DbContext
{ {
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){
optionsBuilder.UseNpgsql(); optionsBuilder.UseNpgsql("Host=localhost; Database=presencedb; Username=postgres; Password=123");
} }
protected override void OnModelCreating(ModelBuilder modelBuilder) { protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<GroupDao>().HasKey(group => group.Id); modelBuilder.Entity<GroupDao>().HasKey(group => group.Id);
modelBuilder.Entity<GroupDao>().Property(group => group.Id).ValueGeneratedOnAdd(); modelBuilder.Entity<GroupDao>().Property(group => group.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<UserDao>().HasKey(user => user.Guid); modelBuilder.Entity<UserDao>().HasKey(user => user.UserId);
modelBuilder.Entity<UserDao>().Property(user => user.Guid).ValueGeneratedOnAdd(); modelBuilder.Entity<UserDao>().Property(user => user.UserId).ValueGeneratedOnAdd();
modelBuilder.Entity<PresenceDao>().HasKey(presence => new { modelBuilder.Entity<PresenceDao>().HasKey(presence => new {
presence.User, presence.User,
presence.Date, presence.Date,
@ -28,8 +28,8 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase
} }
public DbSet<GroupDao> groups{ get; set; } public DbSet<GroupDao> Groups{ get; set; }
public DbSet<UserDao> users{ get; set; } public DbSet<UserDao> Users{ get; set; }
public DbSet<PresenceDao> presences{ get; set; } public DbSet<PresenceDao> Presences{ get; set; }
} }
} }

View File

@ -1,77 +1,94 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Posechaemost.Data.LocalData; using Posechaemost.Data.LocalData;
using Posechaemost.Data.LocalData.Entity; using Posechaemost.Data.LocalData.Entity;
using Posechaemost.Data.RemoteData.RemoteDataBase;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
namespace Posechaemost.Data.Repository namespace Posechaemost.Data.Repository
{ {
public class GroupRepositoryImpl: IGroupRepository public class SQLGroupRepositoryImpl : IGroupRepository
{ {
private readonly RemoteDataBaseContext _remoteDatabaseContext;
public bool AddGroup(String name, String id) public SQLGroupRepositoryImpl(RemoteDataBaseContext remoteDatabaseContext)
{ {
GroupLocalEntity? groupLocal = GetAllGroups().FirstOrDefault(); _remoteDatabaseContext = remoteDatabaseContext;
// GroupLocalEntity? group = new GroupLocalEntity();
groupLocal.Name = name;
groupLocal.Id = int.Parse(id);
return true;
}
public List<GroupLocalEntity> GetAllGroup()
{
return GetAllGroups();
} }
public List<GroupLocalEntity> GetAllGroups() => LocalStaticData.groups; public List<GroupLocalEntity> GetAllGroups() => LocalStaticData.groups;
public GroupLocalEntity GetGroupById(int groupID) public bool AddGroup(GroupDao group)
{ {
GroupLocalEntity groupLocal = GetAllGroups() var groupDao = new GroupDao
.Where(x => x.Id == groupID).FirstOrDefault(); {
Name = group.Name
};
_remoteDatabaseContext.Groups.Add(groupDao);
_remoteDatabaseContext.SaveChanges();
return true;
}
public List<GroupDao> GetAllGroup()
{
return _remoteDatabaseContext.Groups
.Include(g => g.Users)
.Select(g => new GroupDao
{
Name = g.Name,
Id = g.Id,
Users = g.Users.Select(u => new UserDao
{
UserId = u.UserId,
FIO = u.FIO,
GroupId = u.GroupId,
}).ToList()
}).ToList();
}
public GroupDao GetGroupById(int groupID)
{
var groupLocal = _remoteDatabaseContext.Groups
.Where(g => g.Id == groupID).FirstOrDefault();
if (groupLocal == null) return null; if (groupLocal == null) return null;
return groupLocal; return groupLocal;
} }
public bool RemoveGroupById(int groupID){ public bool RemoveGroupById(int groupID)
GroupLocalEntity? groupLocal = GetAllGroups() {
var groupLocal = _remoteDatabaseContext.Groups
.Where(x => x.Id == groupID).FirstOrDefault(); .Where(x => x.Id == groupID).FirstOrDefault();
if (groupLocal == null) return false; if (groupLocal == null) return false;
return GetAllGroups().Remove(groupLocal); _remoteDatabaseContext.Groups.Remove(groupLocal);
_remoteDatabaseContext.SaveChanges();
return true;
} }
public bool UpdateGroupById(int groupID, String name) public bool UpdateGroupById(int groupID, string name)
{ {
GroupLocalEntity? groupLocal = GetAllGroups() var groupLocal = _remoteDatabaseContext.Groups
.Include(g => g.Users)
.Where(x => x.Id == groupID).FirstOrDefault(); .Where(x => x.Id == groupID).FirstOrDefault();
if (groupLocal == null) return false; if (groupLocal == null) return false;
groupLocal.Name = name; groupLocal.Name = name;
groupLocal.Users = _remoteDatabaseContext.Users
.Where(x => x.GroupId == groupLocal.Id)
.Select(user => new UserDao
{
UserId = user.UserId,
FIO = user.FIO,
GroupId = user.GroupId
}).ToList();
_remoteDatabaseContext.SaveChanges();
return true; return true;
} }
} }
} }
// {
// public List<GroupLocalEntity> GetAllGroups() => LocalStaticData.groups;
// public GroupLocalEntity? UpdateGroup(String name)
// {
// GroupLocalEntity? groupLocal = GetAllGroups()
// .Where(x => x.Name == name).FirstOrDefault();
// if (groupLocal == null) return null;
// groupLocal.Name = name;
// return groupLocal;
// }
// public GroupLocalEntity? AddGroup(String name, String id)
// {
// GroupLocalEntity? groupLocal = GetAllGroups().FirstOrDefault();
// // GroupLocalEntity? group = new GroupLocalEntity(name, id);
// groupLocal.Name = name;
// groupLocal.Id = int.Parse(id);
// return groupLocal;
// }
// }

View File

@ -4,15 +4,16 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Posechaemost.Data.LocalData.Entity; using Posechaemost.Data.LocalData.Entity;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
namespace Posechaemost.Data.Repository { namespace Posechaemost.Data.Repository {
public interface IGroupRepository public interface IGroupRepository
{ {
List<GroupLocalEntity> GetAllGroup(); List<GroupDao> GetAllGroup();
bool RemoveGroupById(int groupID); bool RemoveGroupById(int groupID);
bool UpdateGroupById(int groupID, String name); bool UpdateGroupById(int groupID, String name);
GroupLocalEntity GetGroupById(int groupID); GroupDao GetGroupById(int groupID);
bool AddGroup(String name, String id); bool AddGroup(GroupDao group);
List<GroupLocalEntity> GetAllGroups(); List<GroupLocalEntity> GetAllGroups();
} }
} }

View File

@ -3,16 +3,17 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Posechaemost.Data.LocalData.Entity; using Posechaemost.Data.LocalData.Entity;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
using Posechaemost.Domain.Models; using Posechaemost.Domain.Models;
namespace Posechaemost.Data.Repository namespace Posechaemost.Data.Repository
{ {
public interface IPresenceRepository public interface IPresenceRepository
{ {
List<PresenceLocalEntity> GetPresenceByGroup(int groupId); List<PresenceDao> GetPresenceByGroup(int groupId);
List<PresenceLocalEntity> GetPresenceByGroupAndDate(int groupId, DateOnly date); List<PresenceDao> GetPresenceByGroupAndDate(int groupId, DateOnly date);
bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, Guid userGuid); bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId);
void AddPresence(PresenceLocalEntity presence); bool AddPresence(PresenceDao presence);
} }
} }

View File

@ -4,14 +4,15 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Posechaemost.Data.LocalData.Entity; using Posechaemost.Data.LocalData.Entity;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
namespace Posechaemost.Data.Repository { namespace Posechaemost.Data.Repository {
public interface IUserRepository public interface IUserRepository
{ {
List<UserLocalEntity> GetAllUser(); List<UserDao> GetAllUser();
bool RemoveUserByGuid(Guid userGuid); bool RemoveUserById(int usesId);
UserLocalEntity? GetUserByGuid(Guid userGuid); UserDao GetUserById(int userId);
UserLocalEntity? UpdateUser(UserLocalEntity userUpdateLocalEnity); bool UpdateUser(UserDao userUpdate);
UserLocalEntity? UpdateUserByGuid(Guid userGuid); bool UpdateUserById(int userId, string fio, int groupId);
} }
} }

View File

@ -2,49 +2,62 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Posechaemost.Data.LocalData; using Posechaemost.Data.LocalData;
using Posechaemost.Data.LocalData.Entity; using Posechaemost.Data.LocalData.Entity;
using Posechaemost.Data.RemoteData.RemoteDataBase;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
using Posechaemost.Domain.Models; using Posechaemost.Domain.Models;
namespace Posechaemost.Data.Repository namespace Posechaemost.Data.Repository
{ {
public class PresenceRepositoryImpl: IPresenceRepository public class SQLPresenceRepositoryImpl: IPresenceRepository
{ {
public PresenceRepositoryImpl() { private readonly RemoteDataBaseContext _remoteDatabaseContext;
GetAllPresences = LocalStaticData.presences;
}
public List<PresenceLocalEntity> GetAllPresences
{ get; set; }
public void AddPresence(PresenceLocalEntity presence) public SQLPresenceRepositoryImpl(RemoteDataBaseContext remoteDatabaseContext)
{ {
PresenceLocalEntity? presenceLocal = GetAllPresences.FirstOrDefault(); _remoteDatabaseContext = remoteDatabaseContext;
// GroupLocalEntity? group = new GroupLocalEntity();
presenceLocal.ClassNumber = presence.ClassNumber;
presence.UserGuid = presence.UserGuid;
presenceLocal.Date = presence.Date;
presenceLocal.IsAttendence = presence.IsAttendence;
} }
public List<PresenceLocalEntity> GetPresenceByGroup(int groupId) public bool AddPresence(PresenceDao presence)
{ {
return GetAllPresences; var presenceDao = new PresenceDao
{
Date = presence.Date,
ClassNumber = presence.ClassNumber,
UserId = presence.UserId,
User = presence.User
};
_remoteDatabaseContext.Presences.Add(presenceDao);
_remoteDatabaseContext.SaveChanges();
return true;
} }
public List<PresenceLocalEntity> GetPresenceByGroupAndDate(int groupId, DateOnly date) public List<PresenceDao> GetPresenceByGroup(int groupId)
{ {
return GetAllPresences; var listPresences = _remoteDatabaseContext.Presences
.Where(x => x.GroupId == groupId).ToList();
return listPresences;
} }
public bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, Guid userGuid) public List<PresenceDao> GetPresenceByGroupAndDate(int groupId, DateOnly date)
{ {
var presToUpdate = GetAllPresences var listPresences = _remoteDatabaseContext.Presences
.Where(x => x.UserGuid == userGuid && x.ClassNumber >= firstClass .Where(x => x.GroupId == groupId && x.Date == date).ToList();
return listPresences;
}
public bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId)
{
var presToUpdate = _remoteDatabaseContext.Presences
.Where(x => x.UserId == userId && x.ClassNumber >= firstClass
&& x.ClassNumber <= lastClass && x.Date == date).ToList(); && x.ClassNumber <= lastClass && x.Date == date).ToList();
foreach (var pres in presToUpdate) { foreach (var pres in presToUpdate) {
pres.IsAttendence = false; pres.IsAttendence = false;
} }
_remoteDatabaseContext.SaveChanges();
return true; return true;
} }
} }

View File

@ -3,58 +3,73 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Posechaemost.Data.LocalData; using Posechaemost.Data.LocalData;
using Posechaemost.Data.LocalData.Entity; using Posechaemost.Data.LocalData.Entity;
using Posechaemost.Data.RemoteData.RemoteDataBase;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
namespace Posechaemost.Data.Repository namespace Posechaemost.Data.Repository
{ {
public class UserRepositoryImpl: IUserRepository public class SQLUserRepositoryImpl: IUserRepository
{ {
public UserRepositoryImpl() { private readonly RemoteDataBaseContext _remoteDatabaseContext;
public SQLUserRepositoryImpl(RemoteDataBaseContext remoteDatabaseContext)
GetAllUsers = LocalStaticData.users; {
_remoteDatabaseContext = remoteDatabaseContext;
} }
public List<UserLocalEntity> GetAllUser() public List<UserDao> GetAllUser()
{ {
return GetAllUsers; return _remoteDatabaseContext.Users.Select(u => new UserDao
{
FIO = u.FIO,
UserId = u.UserId,
GroupId = u.GroupId
}).ToList();
} }
public List<UserLocalEntity> GetAllUsers
{ get; set; }
public bool RemoveUserByGuid(Guid userGuid) public bool RemoveUserById(int userId)
{ {
UserLocalEntity? userLocal = GetAllUsers var userLocal = _remoteDatabaseContext.Users
.Where(x => x.Guid == userGuid).FirstOrDefault(); .Where(x => x.UserId== userId).FirstOrDefault();
if (userLocal == null) return false; if (userLocal == null) return false;
return GetAllUsers.Remove(userLocal); _remoteDatabaseContext.Users.Remove(userLocal);
_remoteDatabaseContext.SaveChanges();
return true;
} }
public UserLocalEntity? GetUserByGuid(Guid userGuid) { public UserDao GetUserById (int userId) {
UserLocalEntity? userLocal = GetAllUsers var userLocal = _remoteDatabaseContext.Users
.Where(x => x.Guid == userGuid).FirstOrDefault(); .Where(x => x.UserId == userId).FirstOrDefault();
if (userLocal == null) return null; if (userLocal == null) return null;
return userLocal; return userLocal;
} }
public UserLocalEntity? UpdateUser(UserLocalEntity userUpdateLocalEnity) { public bool UpdateUser(UserDao userUpdate) {
UserLocalEntity? userLocal = GetAllUsers var userLocal = _remoteDatabaseContext.Users
.Where(x => x.Guid == userUpdateLocalEnity.Guid).FirstOrDefault(); .Where(x => x.UserId == userUpdate.UserId).FirstOrDefault();
if (userLocal == null) return null; if (userLocal == null) return false;
userLocal.FIO = userUpdateLocalEnity.FIO; userLocal.FIO = userUpdate.FIO;
userLocal.GroupID = userUpdateLocalEnity.GroupID; userLocal.GroupId = userUpdate.GroupId;
return userLocal; _remoteDatabaseContext.SaveChanges();
return true;
} }
public UserLocalEntity? UpdateUserByGuid(Guid userGuid) { public bool UpdateUserById(int userId, string fio, int groupId) {
UserLocalEntity? userLocal = GetAllUsers var userLocal = _remoteDatabaseContext.Users
.Where(x => x.Guid == userGuid).FirstOrDefault(); .Where(x => x.UserId == userId).FirstOrDefault();
if (userLocal == null) return null; if (userLocal == null) return false;
return userLocal;
userLocal.FIO = fio;
userLocal.GroupId = groupId;
_remoteDatabaseContext.SaveChanges();
return true;
} }
} }
} }

View File

@ -1,4 +1,5 @@
using Posechaemost.Data.LocalData.Entity; using Posechaemost.Data.LocalData.Entity;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
using Posechaemost.Data.Repository; using Posechaemost.Data.Repository;
using Posechaemost.Domain.Models; using Posechaemost.Domain.Models;
using System; using System;
@ -11,22 +12,22 @@ namespace Posechaemost.Domain.UseCase
{ {
public class GroupUseCase public class GroupUseCase
{ {
private GroupRepositoryImpl _repositoryGroupImpl; private SQLGroupRepositoryImpl _repositoryGroupImpl;
public GroupUseCase(GroupRepositoryImpl repositoryGroupImpl) public GroupUseCase(SQLGroupRepositoryImpl repositoryGroupImpl)
{ {
_repositoryGroupImpl = repositoryGroupImpl; _repositoryGroupImpl = repositoryGroupImpl;
} }
public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups() public List<GroupDao> GetAllGroups() => _repositoryGroupImpl.GetAllGroups()
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); .Select(it => new GroupDao { Id = it.Id, Name = it.Name}).ToList();
public bool UpdateGroupName(String id, String name1) { public bool UpdateGroupName(String id, String name1) {
return _repositoryGroupImpl.UpdateGroupById(int.Parse(id), name1); return _repositoryGroupImpl.UpdateGroupById(int.Parse(id), name1);
} }
public bool AddGroup(String name, String id) public bool AddGroup(String name, String id)
{ {
return _repositoryGroupImpl.AddGroup(name, id); return _repositoryGroupImpl.AddGroup(new GroupDao { Name = name, Id = int.Parse(id) });
} }
} }
} }

View File

@ -5,6 +5,7 @@ using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks; using System.Threading.Tasks;
using Posechaemost.Data.LocalData.Entity; using Posechaemost.Data.LocalData.Entity;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
using Posechaemost.Data.Repository; using Posechaemost.Data.Repository;
using Posechaemost.Domain.Models; using Posechaemost.Domain.Models;
@ -12,29 +13,28 @@ namespace Posechaemost.Domain.UseCase
{ {
public class PresenceUseCase public class PresenceUseCase
{ {
private readonly PresenceRepositoryImpl _repositoryPresenceImpl; private readonly SQLPresenceRepositoryImpl _repositoryPresenceImpl;
private readonly UserRepositoryImpl _repositoryUserImpl; private readonly SQLUserRepositoryImpl _repositoryUserImpl;
private readonly GroupRepositoryImpl _repositoryGroupImpl; private readonly SQLGroupRepositoryImpl _repositoryGroupImpl;
public PresenceUseCase(PresenceRepositoryImpl repositoryImpl, UserRepositoryImpl userRepositoryImpl, GroupRepositoryImpl groupRepositoryImpl) { public PresenceUseCase(SQLPresenceRepositoryImpl repositoryImpl,
SQLUserRepositoryImpl userRepositoryImpl,
SQLGroupRepositoryImpl groupRepositoryImpl) {
_repositoryPresenceImpl = repositoryImpl; _repositoryPresenceImpl = repositoryImpl;
_repositoryUserImpl = userRepositoryImpl; _repositoryUserImpl = userRepositoryImpl;
_repositoryGroupImpl = groupRepositoryImpl; _repositoryGroupImpl = groupRepositoryImpl;
} }
public List<Presence> GetPresenceByGroup(int groupId) { public List<PresenceDao> GetPresenceByGroup(int groupId) {
var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == groupId).ToList(); var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupId == groupId).ToList();
var presenceByGroup = _repositoryPresenceImpl.GetPresenceByGroup(groupId) var presenceByGroup = _repositoryPresenceImpl.GetPresenceByGroup(groupId)
.Where(x => users.Any(user => user.Guid == x.UserGuid)) .Where(x => users.Any(user => user.UserId == x.UserId))
.Select(presence => new Presence{ .Select(presence => new PresenceDao{
User = new User{ User = new UserDao{
Guid = presence.UserGuid, UserId = presence.UserId,
GroupId = new Group{ GroupId = groupId,
Id = groupId, FIO = users.First(user => user.UserId == presence.UserId).FIO,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.Id == groupId).Name
},
FIO = users.First(user => user.Guid == presence.UserGuid).FIO,
}, },
ClassNumber = presence.ClassNumber, ClassNumber = presence.ClassNumber,
Date = presence.Date, Date = presence.Date,
@ -43,19 +43,16 @@ namespace Posechaemost.Domain.UseCase
return presenceByGroup; return presenceByGroup;
} }
public List<Presence> GetPresenceByGroupAndDate(int groupId, DateOnly date) { public List<PresenceDao> GetPresenceByGroupAndDate(int groupId, DateOnly date) {
var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == groupId).ToList(); var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupId == groupId).ToList();
var presenceByGroupAndDate = _repositoryPresenceImpl.GetPresenceByGroupAndDate(groupId, date) var presenceByGroupAndDate = _repositoryPresenceImpl.GetPresenceByGroupAndDate(groupId, date)
.Where(x => users.Any(user => user.Guid == x.UserGuid && x.Date == date)) .Where(x => users.Any(user => user.UserId == x.UserId && x.Date == date))
.Select(presence => new Presence{ .Select(presence => new PresenceDao{
User = new User{ User = new UserDao{
Guid = presence.UserGuid, UserId = presence.UserId,
GroupId = new Group{ GroupId = groupId,
Id = groupId, FIO = users.First(user => user.UserId == presence.UserId).FIO,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.Id == groupId).Name
},
FIO = users.First(user => user.Guid == presence.UserGuid).FIO,
}, },
ClassNumber = presence.ClassNumber, ClassNumber = presence.ClassNumber,
Date = presence.Date, Date = presence.Date,
@ -64,17 +61,19 @@ namespace Posechaemost.Domain.UseCase
return presenceByGroupAndDate; return presenceByGroupAndDate;
} }
public bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, Guid userGuid) { public bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId) {
return _repositoryPresenceImpl.UncheckAttendence(firstClass, lastClass, date, userGuid); return _repositoryPresenceImpl.UncheckAttendence(firstClass, lastClass, date, userId);
} }
public void AddPresence(int firstClass, int lastClass, int groupId,DateOnly date) public void AddPresence(int firstClass, int lastClass, int groupId,DateOnly date)
{ {
var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID==groupId).ToList(); var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupId==groupId).ToList();
List<PresenceLocalEntity> presenceList = new List<PresenceLocalEntity>(); List<PresenceDao> presenceList = new List<PresenceDao>();
for (int i = firstClass; i < lastClass; i++) { for (int i = firstClass; i < lastClass; i++)
foreach (var user in users) { {
PresenceLocalEntity pres = new PresenceLocalEntity {ClassNumber = i, Date = date, UserGuid = user.Guid}; foreach (var user in users)
{
PresenceDao pres = new PresenceDao{ClassNumber = i, Date = date, UserId = user.UserId, User = user};
presenceList.Add(pres); presenceList.Add(pres);
} }
} }

View File

@ -1,4 +1,5 @@
using Posechaemost.Data.LocalData.Entity; using Posechaemost.Data.LocalData.Entity;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
using Posechaemost.Data.Repository; using Posechaemost.Data.Repository;
using Posechaemost.Domain.Models; using Posechaemost.Domain.Models;
using System; using System;
@ -12,50 +13,43 @@ namespace Posechaemost.Domain.UseCase
public class UserUseCase public class UserUseCase
{ {
private readonly UserRepositoryImpl _repositoryUserImpl; private readonly SQLUserRepositoryImpl _repositoryUserImpl;
private readonly IGroupRepository _repositoryGroupImpl; private readonly IGroupRepository _repositoryGroupImpl;
public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl) public UserUseCase(SQLUserRepositoryImpl repositoryImpl, SQLGroupRepositoryImpl repositoryGroupImpl)
{ {
_repositoryUserImpl = repositoryImpl; _repositoryUserImpl = repositoryImpl;
_repositoryGroupImpl = repositoryGroupImpl; _repositoryGroupImpl = repositoryGroupImpl;
} }
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups() private List<GroupDao> GetAllGroups() => _repositoryGroupImpl.GetAllGroups()
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); .Select(it => new GroupDao { Id = it.Id, Name = it.Name}).ToList();
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers public List<UserDao> GetAllUsers() => _repositoryUserImpl.GetAllUser()
.Join(_repositoryGroupImpl.GetAllGroups(), .Join(_repositoryGroupImpl.GetAllGroups(),
user => user.GroupID, user => user.GroupId,
group => group.Id, group => group.Id,
(user, group) => (user, group) =>
new User { FIO = user.FIO, new UserDao { FIO = user.FIO,
Guid = user.Guid, UserId = user.UserId,
GroupId = new Group {Id = group.Id, Name = group.Name } } GroupId = group.Id }
).ToList(); ).ToList();
public bool RemoveUserByGuid(Guid userGuid) { public bool RemoveUserById(int userId) {
return _repositoryUserImpl.RemoveUserByGuid(userGuid); return _repositoryUserImpl.RemoveUserById(userId);
} }
public User UpdateUser(User user) { public bool UpdateUser(UserDao user) {
UserLocalEntity userLocalEnity = new UserLocalEntity { FIO = user.FIO, GroupID = user.GroupId.Id, Guid = user.Guid }; UserDao userDao = new UserDao { FIO = user.FIO, GroupId = user.GroupId };
UserLocalEntity? result = _repositoryUserImpl.UpdateUser(userLocalEnity); return _repositoryUserImpl.UpdateUser(userDao);
if (result == null) throw new Exception("");
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID);
if (group == null) throw new Exception("");
return new User { FIO = user.FIO, Guid = user.Guid, GroupId = group};
} }
public UserLocalEntity GetUserByGuid(Guid userGuid) { public UserDao GetUserById(int userId) {
return _repositoryUserImpl.GetUserByGuid(userGuid); return _repositoryUserImpl.GetUserById(userId);
} }
public User UpdateUserByGuid(Guid userGuid, String fio, String groupId) { public bool UpdateUserById(int userId, String fio, int groupId) {
UserLocalEntity? result = _repositoryUserImpl.UpdateUserByGuid(userGuid); UserDao userDao = new UserDao { FIO = fio, GroupId = groupId };
if (result == null) throw new Exception(""); return _repositoryUserImpl.UpdateUser(userDao);
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == int.Parse(groupId));
if (group == null) throw new Exception("");
return new User { FIO = fio, GroupId = group, Guid = userGuid };
} }
} }
} }

View File

@ -7,13 +7,17 @@ using System.Text.RegularExpressions;
IServiceCollection services = new ServiceCollection(); IServiceCollection services = new ServiceCollection();
services services
.AddSingleton<IGroupRepository, GroupRepositoryImpl>() .AddSingleton<IGroupRepository, SQLGroupRepositoryImpl>()
.AddSingleton<UserUseCase>(); .AddSingleton<IUserRepository, SQLUserRepositoryImpl>()
.AddSingleton<IPresenceRepository, SQLPresenceRepositoryImpl>()
.AddSingleton<UserUseCase>()
.AddSingleton<GroupUseCase>()
.AddSingleton<PresenceUseCase>();
var serviceProvider = services.BuildServiceProvider(); var serviceProvider = services.BuildServiceProvider();
GroupRepositoryImpl groupRepositoryImpl = new GroupRepositoryImpl(); var userUseCase = serviceProvider.GetService<UserUseCase>();
UserUseCase userUseCase = serviceProvider.GetService<UserUseCase>(); var groupUseCase = serviceProvider.GetService<GroupUseCase>();
GroupUseCase groupUseCase = new GroupUseCase(groupRepositoryImpl); var presenceUseCase = serviceProvider.GetService<PresenceUseCase>();
MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase); MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase, presenceUseCase);

View File

@ -32,12 +32,16 @@ namespace Posechaemost.UI
switch (Console.ReadLine()) switch (Console.ReadLine())
{ {
case "1": _userConsoleUI.DisplayAllUsers(); break; case "1": _userConsoleUI.DisplayAllUsers(); break;
case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; case "2": _userConsoleUI.RemoveUserById(int.Parse(Console.ReadLine())); break;
case "3": _groupConsoleUI.DisplayAllGroups(); break; case "3": _groupConsoleUI.DisplayAllGroups(); break;
case "4": _userConsoleUI.GetUserByGuid(Guid.Parse(Console.ReadLine())); break; case "4": _userConsoleUI.GetUserById(int.Parse(Console.ReadLine())); break;
case "5": _userConsoleUI.UpdateUserByGuid(Guid.Parse(Console.ReadLine()), Console.ReadLine(), Console.ReadLine()); break; case "5": _userConsoleUI.UpdateUserById(int.Parse(Console.ReadLine()), Console.ReadLine(), int.Parse(Console.ReadLine())); break;
case "6": _groupConsoleUI.UpdateGroupName(Console.ReadLine(), Console.ReadLine()); break; case "6": _groupConsoleUI.UpdateGroupName(Console.ReadLine(), Console.ReadLine()); break;
case "7": _groupConsoleUI.AddGroup(Console.ReadLine(), Console.ReadLine()); break; case "7": _groupConsoleUI.AddGroup(Console.ReadLine(), Console.ReadLine()); break;
case "8": _presenceConsoleUI.GetPresenceByGroup(int.Parse(Console.ReadLine())); break;
case "9": _presenceConsoleUI.GetPresenceByGroupAndDate(int.Parse(Console.ReadLine()), DateOnly.Parse(Console.ReadLine())); break;
case "10": _presenceConsoleUI.UncheckAttendence(int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), DateOnly.Parse(Console.ReadLine()), int.Parse(Console.ReadLine())); break;
case "11": _presenceConsoleUI.AddPresence(int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), DateOnly.Parse(Console.ReadLine())); break;
default: DisplayMenu(); default: DisplayMenu();
break; break;

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Posechaemost.Domain.UseCase; using Posechaemost.Domain.UseCase;
@ -9,24 +10,46 @@ namespace Posechaemost.UI
public class PresenceConsoleUI public class PresenceConsoleUI
{ {
PresenceUseCase _presenceUseCase; PresenceUseCase _presenceUseCase;
public PresenceConsoleUI(PresenceUseCase presenceUseCase) { public PresenceConsoleUI(PresenceUseCase presenceUseCase)
{
_presenceUseCase = presenceUseCase; _presenceUseCase = presenceUseCase;
} }
public void GetPresenceByGroup(int groupId) { public void GetPresenceByGroup(int groupId)
{
StringBuilder presenceOutput = new StringBuilder();
var presence = _presenceUseCase.GetPresenceByGroup(groupId); var presence = _presenceUseCase.GetPresenceByGroup(groupId);
foreach (var p in presence)
{
presenceOutput.AppendLine($"{p.User.UserId}\t{p.User.FIO}\t{p.ClassNumber}\t{p.Date}\t{p.IsAttendence}");
}
Console.WriteLine(presenceOutput);
} }
public void GetPresenceByGroupAndDate(int groupId, DateOnly date) { public void GetPresenceByGroupAndDate(int groupId, DateOnly date)
{
StringBuilder presenceOutput = new StringBuilder();
var presence = _presenceUseCase.GetPresenceByGroupAndDate(groupId, date); var presence = _presenceUseCase.GetPresenceByGroupAndDate(groupId, date);
foreach (var p in presence)
{
presenceOutput.AppendLine($"{p.User.UserId}\t{p.User.FIO}\t{p.ClassNumber}\t{p.Date}\t{p.IsAttendence}");
}
Console.WriteLine(presenceOutput);
} }
public void UncheckAttendence(int firstClass, int lastClass, DateOnly date, Guid userGuid) { public void UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId)
var presence = _presenceUseCase.UncheckAttendence(firstClass, lastClass, date, userGuid); {
string output = _presenceUseCase.UncheckAttendence(firstClass, lastClass, date, userId) ?
"Посещаемость обновлена" : "Посещаемость не обновлена";
Console.WriteLine(output);
} }
public void AddPresence(int firstClass, int lastClass, int groupId, DateOnly date) { public void AddPresence(int firstClass, int lastClass, int groupId, DateOnly date)
{
StringBuilder presenceOutput = new StringBuilder();
_presenceUseCase.AddPresence(firstClass, lastClass, groupId, date); _presenceUseCase.AddPresence(firstClass, lastClass, groupId, date);
presenceOutput.AppendLine("Посещаемость добавлена");
Console.WriteLine(presenceOutput);
} }
} }
} }

View File

@ -15,9 +15,9 @@ namespace Posechaemost.UI
_userUseCase = userUseCase; _userUseCase = userUseCase;
} }
public void RemoveUserByGuid(Guid guidUser) { public void RemoveUserById(int userId) {
string output = _userUseCase.RemoveUserByGuid(guidUser) ? "Пользователь удален" : "Пользователь не удален"; string output = _userUseCase.RemoveUserById(userId) ? "Пользователь удален" : "Пользователь не удален";
Console.WriteLine(output); Console.WriteLine(output);
} }
@ -26,22 +26,21 @@ namespace Posechaemost.UI
StringBuilder userOutput = new StringBuilder(); StringBuilder userOutput = new StringBuilder();
foreach (var user in _userUseCase.GetAllUsers()) foreach (var user in _userUseCase.GetAllUsers())
{ {
userOutput.AppendLine($"{user.Guid}\t{user.FIO}\t{user.GroupId.Name}"); userOutput.AppendLine($"{user.UserId}\t{user.FIO}\t{user.GroupId}");
} }
Console.WriteLine(userOutput); Console.WriteLine(userOutput);
} }
public void GetUserByGuid(Guid userGuid) { public void GetUserById(int userId) {
StringBuilder userOutput = new StringBuilder(); StringBuilder userOutput = new StringBuilder();
var user = _userUseCase.GetUserByGuid(userGuid); var user = _userUseCase.GetUserById(userId);
userOutput.AppendLine($"{user.Guid}\t{user.FIO}\t{user.GroupID}"); userOutput.AppendLine($"{user.UserId}\t{user.FIO}\t{user.GroupId}");
Console.WriteLine(userOutput); Console.WriteLine(userOutput);
} }
public void UpdateUserByGuid(Guid userGuid, String name, String groupId) { public void UpdateUserById(int userId, String name, int groupId) {
StringBuilder userOutput = new StringBuilder(); StringBuilder userOutput = new StringBuilder();
var user = _userUseCase.UpdateUserByGuid(userGuid, name, groupId); var user = _userUseCase.UpdateUserById(userId, name, groupId) ? "Пользователь обновлен" : "Пользователь не обновлен";
userOutput.AppendLine($"{user.Guid}\t{user.FIO}\t{user.GroupId.Name}");
Console.WriteLine(userOutput); Console.WriteLine(userOutput);
} }
} }

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Posechaemost")] [assembly: System.Reflection.AssemblyCompanyAttribute("Posechaemost")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+dd18de386525d2859a6db90bb74a2bdc95bc47e3")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0705829116f2c729cee9977f2de521bf54b6e4b1")]
[assembly: System.Reflection.AssemblyProductAttribute("Posechaemost")] [assembly: System.Reflection.AssemblyProductAttribute("Posechaemost")]
[assembly: System.Reflection.AssemblyTitleAttribute("Posechaemost")] [assembly: System.Reflection.AssemblyTitleAttribute("Posechaemost")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
e484c6d2be3801b673b3b8bec4389b40f150f0a04dc9544c29a279722a7318c6 04d7b3aacea28cffe6e56680c2c82b72f1a837729ee485f47bdc07c464612b47