presence/Demo/Data/Repository/UserRepositoryImpl.cs

65 lines
1.8 KiB
C#
Raw Permalink Normal View History

2024-11-01 07:14:01 +00:00
using Demo.Data.LocalData;
using Demo.domain.Models;
using Demo.Data.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo.Data.Repository
{
public class UserRepositoryImpl
{
private List<UserLocalEntity> _users;
public UserRepositoryImpl()
{
_users = LocalStaticData.users;
}
public IEnumerable<UserLocalEntity> GetAllUsers => _users;
public void DeleteUserByGuid(Guid guid)
{
var user = _users.FirstOrDefault(u => u.Guid == guid);
if (user != null)
{
_users.Remove(user);
}
else
{
throw new GuidNotFoundException($"Пользователь с GUID {guid} не найден.");
}
}
public UserLocalEntity? FindUserByGuid(Guid guid)
{
var user = _users.FirstOrDefault(u => u.Guid == guid);
if (user == null)
{
throw new GuidNotFoundException($"Пользователь с GUID {guid} не найден.");
}
return user;
}
public void UpdateUser(string guidOrInt, string newFIO, int newGroupID)
{
Guid guid;
if (!Guid.TryParse(guidOrInt, out guid))
{
throw new ArgumentException($"Значение '{guidOrInt}' не является корректным GUID.");
}
var user = _users.FirstOrDefault(u => u.Guid == guid);
if (user != null)
{
user.FIO = newFIO;
user.GroupID = newGroupID;
}
else
{
throw new GuidNotFoundException($"Пользователь с GUID {guid} не найден.");
}
}
}
}