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 GetAllUsers() { // Возвращаем пользователей, отсортированных по UserId return _remoteDatabaseContext.Users.OrderBy(u => u.UserGuid).ToList(); } public List GetUserNames() { var users = GetAllUsers(); List names = new List(); foreach (var user in users) { names.Add(new UserDao{ UserGuid = user.UserGuid, FIO=user.FIO }); } return names; } } }