presence_sample_v2/data/Repository/SQLGroupRepository.cs
2024-11-25 13:47:17 +03:00

56 lines
1.6 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();
}
public async Task<IEnumerable<GroupDAO>> getAllGroupAsync()
{
return await _dbContext.groups.Include(group => group.Users).ToListAsync();
}
}
}