Demo/Data/Repository/UserRepositorylmpl.cs

60 lines
2.4 KiB
C#
Raw Normal View History

2024-10-17 11:46:19 +00:00
using Demo.Domain.Models;
using Demo.Data.LocalData;
2024-10-24 15:57:52 +00:00
using Demo.Data.RemoteData.RemoteDataBase;
2024-10-17 11:46:19 +00:00
namespace Demo.Data.Repository
{
2024-10-24 15:57:52 +00:00
public class SQLUserRepositoryImpl : IUserRepository
2024-10-17 11:46:19 +00:00
{
2024-10-24 15:57:52 +00:00
private readonly RemoteDatabaseContext _remoteDatabaseContext;
2024-10-17 11:46:19 +00:00
2024-10-24 15:57:52 +00:00
public SQLUserRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext){
_remoteDatabaseContext = remoteDatabaseContext;
GetAllUsers = _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList();
2024-10-17 11:46:19 +00:00
}
2024-10-24 15:57:52 +00:00
2024-10-17 11:46:19 +00:00
public List<UserLocalEntity> GetAllUsers
{ get; set; }
2024-10-21 10:55:18 +00:00
public List<UserLocalEntity> GetAllUser(){
2024-10-24 15:57:52 +00:00
return _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList();
2024-10-21 09:38:07 +00:00
}
2024-10-24 15:57:52 +00:00
public UserLocalEntity? GetUserByGuid(Guid guid){
var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == guid);
return new UserLocalEntity{FIO = userDAO.FIO, GroupID = userDAO.GroupID, Guid = guid};
2024-10-17 11:46:19 +00:00
}
2024-10-28 10:39:15 +00:00
public List<UserLocalEntity> GetUsersByGroupID(int groupID)
{
return _remoteDatabaseContext.Users
.Where(x => x.GroupID == groupID) // Фильтруем пользователей по GroupID
.Select(x => new UserLocalEntity
{
FIO = x.FIO,
Guid = x.Guid,
GroupID = x.GroupID // Устанавливаем фактический GroupID
})
.ToList();
}
2024-10-24 15:57:52 +00:00
public bool RemoveUserByGuid(Guid guid){
var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == guid);
_remoteDatabaseContext.Users.Remove(userDAO);
_remoteDatabaseContext.SaveChanges();
return true;
2024-10-17 11:46:19 +00:00
}
2024-10-24 15:57:52 +00:00
public UserLocalEntity? UpdateUser(UserLocalEntity updatedUser){
var user = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == updatedUser.Guid);
if (user == null){
return null;
}
user.FIO = updatedUser.FIO;
user.GroupID = updatedUser.GroupID;
_remoteDatabaseContext.SaveChanges();
return new UserLocalEntity{FIO = user.FIO, Guid = user.Guid, GroupID = user.GroupID};
2024-10-17 11:46:19 +00:00
}
}
}