112 lines
3.7 KiB
C#
112 lines
3.7 KiB
C#
|
||
using data.RemoteData.RemoteDataBase.DAO;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using data.RemoteData.RemoteDataBase;
|
||
|
||
namespace data.Repository
|
||
{
|
||
public class SQLUserRepositoryImpl : IUserRepository
|
||
{
|
||
private readonly RemoteDatabaseContext _context;
|
||
|
||
public SQLUserRepositoryImpl(RemoteDatabaseContext context)
|
||
{
|
||
_context = context;
|
||
}
|
||
|
||
public async Task<bool> RemoveUserByIdAsync(int userId)
|
||
{
|
||
var user = await _context.Users
|
||
.FirstOrDefaultAsync(u => u.UserId == userId)
|
||
?? throw new ArgumentException($"Пользователь с id: {userId} не найден.");
|
||
|
||
_context.Users.Remove(user);
|
||
return await _context.SaveChangesAsync() > 0;
|
||
}
|
||
|
||
public async Task<UserDAO> AddUserAsync(UserDAO user)
|
||
{
|
||
await _context.Users.AddAsync(user);
|
||
await _context.SaveChangesAsync();
|
||
return user;
|
||
}
|
||
|
||
public UserDAO UpdateUser(UserDAO user)
|
||
{
|
||
// Оптимизированный запрос с проверкой группы
|
||
var groupExists = _context.Groups
|
||
.AnyAsync(g => g.Id == user.GroupId);
|
||
|
||
if (groupExists==null)
|
||
throw new ArgumentException($"Группа с id: {user.GroupId} не найдена.");
|
||
|
||
var existingUser = _context.Users
|
||
.FirstOrDefault(u => u.UserId == user.UserId);
|
||
|
||
if (existingUser == null)
|
||
throw new ArgumentException($"Пользователь с id: {user.UserId} не найден.");
|
||
|
||
existingUser.FIO = user.FIO;
|
||
existingUser.GroupId = user.GroupId;
|
||
|
||
_context.SaveChangesAsync();
|
||
return existingUser;
|
||
}
|
||
|
||
public async Task<List<UserDAO>> GetAllUsersAsync(bool trackEntities = false)
|
||
{
|
||
var query = _context.Users
|
||
.OrderBy(u => u.UserId)
|
||
.Include(u => u.Group);
|
||
|
||
return trackEntities
|
||
? await query.ToListAsync()
|
||
: await query.AsNoTracking().ToListAsync();
|
||
}
|
||
|
||
public List<UserDAO> GetAllUsers()
|
||
{
|
||
return _context.Users
|
||
.OrderBy(u => u.UserId)
|
||
.AsNoTracking()
|
||
.ToList();
|
||
}
|
||
|
||
public bool RemoveUserById(int userId)
|
||
{
|
||
var user = _context.Users
|
||
.FirstOrDefault(u => u.UserId == userId)
|
||
?? throw new ArgumentException($"Пользователь с id: {userId} не найден.");
|
||
|
||
_context.Users.Remove(user);
|
||
return _context.SaveChanges() > 0;
|
||
}
|
||
|
||
public UserDAO GetUserById(int userId)
|
||
{
|
||
return _context.Users
|
||
.Include(u => u.Group)
|
||
.AsNoTracking()
|
||
.FirstOrDefault(u => u.UserId == userId)
|
||
?? throw new ArgumentException($"Пользователь с id: {userId} не найден.");
|
||
}
|
||
|
||
public List<UserDAO> GetUsersByGroupId(int groupId)
|
||
{
|
||
return _context.Users
|
||
.Where(u => u.GroupId == groupId)
|
||
.Select(u => new UserDAO
|
||
{
|
||
UserId = u.UserId,
|
||
FIO = u.FIO,
|
||
GroupId = u.GroupId,
|
||
Group = new GroupDAO { Id = u.Group.Id, Name = u.Group.Name }
|
||
})
|
||
.AsNoTracking()
|
||
.ToList();
|
||
}
|
||
}
|
||
} |