presence/data/Repository/UserRepositorylmpl.cs
2024-12-22 22:41:45 +03:00

95 lines
3.8 KiB
C#

using Demo.Domain.Models;
using Demo.Data.LocalData;
using Demo.Data.RemoteData.RemoteDataBase;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
namespace Demo.Data.Repository
{
public class SQLUserRepositoryImpl : IUserRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLUserRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext){
_remoteDatabaseContext = remoteDatabaseContext;
GetAllUsers = _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList();
}
public List<UserLocalEntity> GetAllUsers
{ get; set; }
public List<UserLocalEntity> GetAllUser(){
return _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList();
}
public UserLocalEntity? GetUserByGuid(Guid guid){
var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == guid);
if (userDAO == null) return null;
return new UserLocalEntity{FIO = userDAO.FIO, GroupID = userDAO.GroupID, Guid = guid};
}
public List<UserLocalEntity> GetUsersByGroupID(int groupID)
{
return _remoteDatabaseContext.Users
.Where(x => x.GroupID == groupID)
.Select(x => new UserLocalEntity
{
FIO = x.FIO,
Guid = x.Guid,
GroupID = x.GroupID
})
.ToList();
}
public UserLocalEntity? CreateUser(string FIO, string GroupName){
var groupDAO = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Name == GroupName);
UserDAO userDAO = new UserDAO{FIO = FIO, Guid = Guid.NewGuid(), GroupID = groupDAO.ID};
var result = _remoteDatabaseContext.Users.Add(userDAO);
_remoteDatabaseContext.SaveChanges();
return new UserLocalEntity{FIO = FIO, Guid = Guid.NewGuid(), GroupID = groupDAO.ID};
}
public bool RemoveUserByGuid(Guid guid){
var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == guid);
_remoteDatabaseContext.Users.Remove(userDAO);
_remoteDatabaseContext.SaveChanges();
return true;
}
public bool RemoveUserByFIOnGroup(string FIO, int groupID)
{
var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.FIO == FIO && x.GroupID == groupID);
if (userDAO == null) return false; // Если пользователь не найден, возвращаем false
_remoteDatabaseContext.Users.Remove(userDAO);
_remoteDatabaseContext.SaveChanges();
return true;
}
public bool RemoveUsersByGroupID(int groupID)
{
var users = GetUsersByGroupID(groupID);
foreach(var user in users)
{
var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == user.Guid);
if (userDAO != null)
{
_remoteDatabaseContext.Users.Remove(userDAO);
}
}
_remoteDatabaseContext.SaveChanges();
return true;
}
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};
}
}
}