slarny4/Demo1/Data/Repository/UserRepositoryImpl.cs

65 lines
1.7 KiB
C#
Raw Permalink Normal View History

2024-10-28 12:42:04 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Demo.Domain.Models;
using Demo.Data.LocalData;
2024-10-24 08:50:32 +00:00
2024-10-28 12:42:04 +00:00
namespace Demo.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
public IEnumerable<User> GetAllUsers()
{
2024-10-28 12:42:04 +00:00
return LocalStaticData.Users.Select(u => new User
2024-10-24 08:50:32 +00:00
{
Id = u.Id,
FIO = u.FIO,
2024-10-28 12:42:04 +00:00
GroupID = u.GroupID
2024-10-24 08:50:32 +00:00
});
}
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-28 12:42:04 +00:00
var localUser = LocalStaticData.Users.FirstOrDefault(u => u.Id == id);
2024-10-24 08:50:32 +00:00
if (localUser == null)
2024-10-28 12:42:04 +00:00
throw new Exception("User not found");
2024-10-24 08:50:32 +00:00
return new User
{
Id = localUser.Id,
FIO = localUser.FIO,
2024-10-28 12:42:04 +00:00
GroupID = localUser.GroupID
2024-10-24 08:50:32 +00:00
};
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-28 12:42:04 +00:00
var localUser = new LocalData.Entity.User
2024-10-24 08:50:32 +00:00
{
Id = user.Id,
FIO = user.FIO,
2024-10-28 12:42:04 +00:00
GroupID = user.GroupID
};
LocalStaticData.Users.Add(localUser);
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-28 12:42:04 +00:00
var localUser = LocalStaticData.Users.FirstOrDefault(u => u.Id == user.Id);
2024-10-24 08:50:32 +00:00
if (localUser == null)
2024-10-28 12:42:04 +00:00
throw new Exception("User not found");
2024-10-21 22:57:01 +00:00
2024-10-24 08:50:32 +00:00
localUser.FIO = user.FIO;
2024-10-28 12:42:04 +00:00
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-28 12:42:04 +00:00
var user = LocalStaticData.Users.FirstOrDefault(u => u.Id == id);
if (user != null)
{
LocalStaticData.Users.Remove(user);
}
2024-10-21 22:57:01 +00:00
}
}
2024-10-24 08:50:32 +00:00
}