semesterWork/data/Repository/SQLGroupRepository.cs

69 lines
1.9 KiB
C#
Raw Normal View History

using data.DAO;
2024-12-04 21:58:43 +00:00
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<GroupDAO> getAllGroup()
{
2024-11-16 21:56:50 +00:00
try
{
2024-12-04 21:58:43 +00:00
return dbContext.groups.Include(group => group.Users).ToList();
2024-11-16 21:56:50 +00:00
}
catch (Exception ex) {
return new List<GroupDAO>();
}
}
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
{
transaction.Rollback();
}
return false;
}
2024-12-07 15:33:26 +00:00
public async Task<IEnumerable<GroupDAO>> getAllGroupAsync()
{
return await dbContext.groups.Include(group => group.Users).ToListAsync();
}
}
}