51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
|
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<UserDAO> 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<GroupDAO> getAllGroup()
|
|||
|
{
|
|||
|
return _dbContext.groups.Include(group => group.Users).ToList();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|