132 lines
4.2 KiB
C#
132 lines
4.2 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 SQLGroupRepositoryImpl : IGroupRepository
|
|
{
|
|
private readonly RemoteDatabaseContext _context;
|
|
|
|
public SQLGroupRepositoryImpl(RemoteDatabaseContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public GroupDAO AddGroup(string groupName)
|
|
{
|
|
if (_context.Groups.AnyAsync(g => g.Name == groupName)!=null)
|
|
throw new ArgumentException($"Группа '{groupName}' уже существует");
|
|
|
|
var newGroup = new GroupDAO { Name = groupName };
|
|
_context.Groups.AddAsync(newGroup);
|
|
_context.SaveChangesAsync();
|
|
return newGroup;
|
|
}
|
|
|
|
public async Task RemoveAllStudentsFromGroup(int groupId)
|
|
{
|
|
using var transaction = _context.Database.BeginTransactionAsync();
|
|
|
|
try
|
|
{
|
|
var users = await _context.Users
|
|
.Where(u => u.GroupId == groupId)
|
|
.ToListAsync();
|
|
|
|
_context.Users.RemoveRange(users);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task AddStudentToGroupAsync(int groupId, UserDAO student)
|
|
{
|
|
var groupExists = await _context.Groups.AnyAsync(g => g.Id == groupId);
|
|
if (!groupExists) throw new ArgumentException("Группа не найдена");
|
|
|
|
var studentExists = await _context.Users.AnyAsync(u => u.UserId == student.UserId);
|
|
if (studentExists) throw new ArgumentException("Студент уже существует");
|
|
|
|
student.GroupId = groupId;
|
|
await _context.Users.AddAsync(student);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public GroupDAO GetGroupById(int groupId)
|
|
{
|
|
return _context.Groups
|
|
.AsNoTracking()
|
|
.Include(g => g.Users)
|
|
.Select(g => new GroupDAO
|
|
{
|
|
Id = g.Id,
|
|
Name = g.Name,
|
|
Users = g.Users.Select(u => new UserDAO
|
|
{
|
|
UserId = u.UserId,
|
|
FIO = u.FIO
|
|
}).ToList()
|
|
})
|
|
.FirstOrDefault(g => g.Id == groupId)
|
|
?? throw new AggregateException("Группа не найдена");
|
|
}
|
|
|
|
public async Task<List<GroupDAO>> GetAllGroupsAsync()
|
|
{
|
|
return await _context.Groups
|
|
.AsNoTracking()
|
|
.Select(g => new GroupDAO
|
|
{
|
|
Id = g.Id,
|
|
Name = g.Name
|
|
})
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task UpdateGroupAsync(int groupId, string newName)
|
|
{
|
|
var group = await _context.Groups
|
|
.FirstOrDefaultAsync(g => g.Id == groupId)
|
|
?? throw new ArgumentException("Группа не найдена");
|
|
|
|
group.Name = newName;
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteGroupAsync(int groupId)
|
|
{
|
|
await using var transaction = await _context.Database.BeginTransactionAsync();
|
|
|
|
try
|
|
{
|
|
// Каскадное удаление пользователей
|
|
await _context.Users
|
|
.Where(u => u.GroupId == groupId)
|
|
.ExecuteDeleteAsync();
|
|
|
|
await _context.Groups
|
|
.Where(g => g.Id == groupId)
|
|
.ExecuteDeleteAsync();
|
|
|
|
await transaction.CommitAsync();
|
|
}
|
|
catch
|
|
{
|
|
await transaction.RollbackAsync();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public List<GroupDAO> GetAllGroups()
|
|
{
|
|
return _context.Groups.ToList();
|
|
}
|
|
}
|
|
} |