using data.DAO; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace data.Repository { public class SQLGroupRepository : IGroupRepository { private readonly RemoteDatabaseContext _dbContext; public SQLGroupRepository(RemoteDatabaseContext remoteDatabaseContext) { _dbContext = remoteDatabaseContext; } public bool addGroup(GroupDAO group) { _dbContext.groups.Add(group); return _dbContext.SaveChanges() > 1; } public bool addGroupWithStudents(GroupDAO groupDAO, IEnumerable userDAOs) { using var transaction = _dbContext.Database.BeginTransaction(); try { _dbContext.groups.Add(groupDAO); _dbContext.SaveChanges(); foreach (var item in userDAOs) { item.Group = groupDAO; _dbContext.users.Add(item); } _dbContext.SaveChanges(); transaction.Commit(); return true; } catch (Exception ex) { transaction.Rollback(); } return false; } public IEnumerable getAllGroup() { return _dbContext.groups.Include(group => group.Users).ToList(); } public async Task> getAllGroupAsync() { return await _dbContext.groups.Include(group => group.Users).ToListAsync(); } } }