slarny4/Demo1/Data/Repository/UserRepositoryImpl.cs

64 lines
1.8 KiB
C#
Raw Normal View History

2024-10-24 08:50:32 +00:00
using AttendanceApp.Data.Exceptions;
using AttendanceApp.Data.LocalData;
using AttendanceApp.Domain.Models;
namespace AttendanceApp.Data.Repository
2024-10-21 22:57:01 +00:00
{
2024-10-24 08:50:32 +00:00
public class UserRepositoryImpl : IUserRepository
2024-10-21 22:57:01 +00:00
{
2024-10-24 08:50:32 +00:00
private readonly List<LocalUser> _users = LocalStaticData.Users;
public IEnumerable<User> GetAllUsers()
{
return _users.Select(u => new User
{
Id = u.Id,
FIO = u.FIO,
GroupId = u.GroupID
});
}
2024-10-21 22:57:01 +00:00
2024-10-24 08:50:32 +00:00
public User GetUserById(Guid id)
2024-10-21 22:57:01 +00:00
{
2024-10-24 08:50:32 +00:00
var localUser = _users.FirstOrDefault(u => u.Id == id);
if (localUser == null)
throw new UserNotFoundException($"Пользователь с ID {id} не найден.");
return new User
{
Id = localUser.Id,
FIO = localUser.FIO,
GroupId = localUser.GroupID
};
2024-10-21 22:57:01 +00:00
}
2024-10-24 08:50:32 +00:00
public void AddUser(User user)
2024-10-21 22:57:01 +00:00
{
2024-10-24 08:50:32 +00:00
_users.Add(new LocalUser
{
Id = user.Id,
FIO = user.FIO,
GroupID = user.GroupId
});
2024-10-21 22:57:01 +00:00
}
2024-10-24 08:50:32 +00:00
public void UpdateUser(User user)
2024-10-21 22:57:01 +00:00
{
2024-10-24 08:50:32 +00:00
var localUser = _users.FirstOrDefault(u => u.Id == user.Id);
if (localUser == null)
throw new UserNotFoundException($"Пользователь с ID {user.Id} не найден.");
2024-10-21 22:57:01 +00:00
2024-10-24 08:50:32 +00:00
localUser.FIO = user.FIO;
localUser.GroupID = user.GroupId;
2024-10-21 22:57:01 +00:00
}
2024-10-24 08:50:32 +00:00
public void DeleteUser(Guid id)
2024-10-21 22:57:01 +00:00
{
2024-10-24 08:50:32 +00:00
var user = _users.FirstOrDefault(u => u.Id == id);
if (user == null)
throw new UserNotFoundException($"Пользователь с ID {id} не найден.");
_users.Remove(user);
2024-10-21 22:57:01 +00:00
}
}
2024-10-24 08:50:32 +00:00
}