presence/data/Repository/SQLUserRepositoryImpl.cs

70 lines
2.3 KiB
C#
Raw Normal View History

2024-11-13 09:00:26 +00:00
using data.Exception;
2024-11-11 09:07:11 +00:00
using data.RemoteData.RemoteDataBase;
using data.RemoteData.RemoteDataBase.DAO;
using domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace data.Repository
{
public class SQLUserRepositoryImpl : IUserRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLUserRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
2024-11-13 09:00:26 +00:00
2024-11-11 09:07:11 +00:00
public IEnumerable<UserLocalEnity> GetAllUsers => _remoteDatabaseContext.Users
.Select(u => new UserLocalEnity
{
Guid = u.Guid,
FIO = u.FIO,
GroupID = u.GroupID
})
.ToList();
2024-11-13 09:00:26 +00:00
public bool RemoveUserByGuid(Guid userGuid)
2024-11-11 09:07:11 +00:00
{
var user = _remoteDatabaseContext.Users.FirstOrDefault(u => u.Guid == userGuid);
if (user == null) throw new UserNotFoundException(userGuid);
_remoteDatabaseContext.Users.Remove(user);
2024-11-13 09:00:26 +00:00
_remoteDatabaseContext.SaveChanges();
2024-11-11 09:07:11 +00:00
return true;
}
public UserLocalEnity? UpdateUser(UserLocalEnity user)
{
var existingUser = _remoteDatabaseContext.Users.FirstOrDefault(u => u.Guid == user.Guid);
if (existingUser == null) throw new UserNotFoundException(user.Guid);
existingUser.FIO = user.FIO;
existingUser.GroupID = user.GroupID;
2024-11-13 09:00:26 +00:00
_remoteDatabaseContext.SaveChanges();
2024-11-11 09:07:11 +00:00
// Возвращаем обновленный объект UserLocalEnity
return new UserLocalEnity
{
Guid = existingUser.Guid,
FIO = existingUser.FIO,
GroupID = existingUser.GroupID
};
}
// Дополнительный метод для DAO, если требуется
public IEnumerable<RemoteData.RemoteDataBase.DAO.UserDao> GetAllUsersDao => _remoteDatabaseContext.Users.ToList();
public List<UserDao> GetUserNames()
{
return _remoteDatabaseContext.Users
.Select(u => new UserDao { Guid = u.Guid, FIO = u.FIO })
.ToList();
}
}
}