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 { public readonly RemoteDatabaseContext dbContext; public SQLGroupRepository(RemoteDatabaseContext remoteDatabaseContext) { dbContext = remoteDatabaseContext; } public bool addGroup(GroupDAO group) { try { dbContext.groups.Add(group); return dbContext.SaveChanges() > 1; } catch (Exception ex) { return false; } } public IEnumerable getAllGroup() { try { return dbContext.groups.Include(group => group.Users).ToList(); } catch (Exception ex) { return new List(); } } 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 { transaction.Rollback(); } return false; } public async Task> getAllGroupAsync() { return await dbContext.groups.Include(group => group.Users).ToListAsync(); } } }