Mega_New_Presence/Demo/Data/Repository/SQLUserRepositoryImpl.cs
2024-11-04 23:16:00 +03:00

62 lines
2.1 KiB
C#

using Demo.Data.Exceptions;
using Demo.Data.RemoteData.RemoteDataBase;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
using Demo.domain.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Demo.Data.Repository
{
public class SQLUserRepositoryImpl : IUserRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLUserRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
public bool RemoveUserById(Guid UserGuid)
{
var user = _remoteDatabaseContext.Users.FirstOrDefault(u => u.UserGuid == UserGuid);
if (user == null) throw new UserNotFoundException(UserGuid);
_remoteDatabaseContext.Users.Remove(user);
_remoteDatabaseContext.SaveChanges();
return true;
}
public UserDao? UpdateUser(UserDao user)
{
var existingUser = _remoteDatabaseContext.Users.FirstOrDefault(u => u.UserGuid == user.UserGuid);
if (existingUser == null) throw new UserNotFoundException(user.UserGuid);
// Обновляем поля существующего пользователя
existingUser.FIO = user.FIO;
existingUser.GroupId = user.GroupId;
_remoteDatabaseContext.SaveChanges();
return existingUser;
}
public List<UserDao> GetAllUsers()
{
// Возвращаем пользователей, отсортированных по UserId
return _remoteDatabaseContext.Users.OrderBy(u => u.UserGuid).ToList();
}
public List<UserDao> GetUserNames()
{
var users = GetAllUsers();
List<UserDao> names = new List<UserDao>();
foreach (var user in users)
{
names.Add(new UserDao{ UserGuid = user.UserGuid, FIO=user.FIO });
}
return names;
}
}
}