presence/data/Repository/SQLGroupRepositoryImpl.cs
2024-11-18 15:42:33 +03:00

79 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using data.Exception;
using data.RemoteData.RemoteDataBase;
using data.RemoteData.RemoteDataBase.DAO;
using data.Repository;
using domain.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
public class SQLGroupRepositoryImpl : IGroupRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLGroupRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
public GroupLocalEntity? GetGroupById(int groupId)
{
var groupDao = _remoteDatabaseContext.Groups.FirstOrDefault(g => g.Id == groupId);
return groupDao != null ? new GroupLocalEntity { Id = groupDao.Id, Name = groupDao.Name } : null;
}
public List<GroupLocalEntity> GetAllGroup()
{
return _remoteDatabaseContext.Groups
.Select(g => new GroupLocalEntity { Id = g.Id, Name = g.Name })
.ToList();
}
public bool AddGroup(GroupLocalEntity group)
{
if (_remoteDatabaseContext.Groups.Any(g => g.Id == group.Id))
return false;
var groupDao = new GroupDao { Id = group.Id, Name = group.Name };
_remoteDatabaseContext.Groups.Add(groupDao);
_remoteDatabaseContext.SaveChanges();
return true;
}
public bool UpdateGroupById(int groupID, GroupLocalEntity updatedGroup)
{
var existingGroup = _remoteDatabaseContext.Groups.FirstOrDefault(g => g.Id == groupID);
if (existingGroup == null)
return false;
existingGroup.Name = updatedGroup.Name;
_remoteDatabaseContext.SaveChanges();
return true;
}
public bool RemoveGroupById(int groupID)
{
var existingGroup = _remoteDatabaseContext.Groups.FirstOrDefault(g => g.Id == groupID);
if (existingGroup == null)
return false;
_remoteDatabaseContext.Groups.Remove(existingGroup);
_remoteDatabaseContext.SaveChanges();
return true;
}
public List<GroupDao> GetAllGroupWithStident()
{
// Убедитесь, что загружаются все пользователи, связанные с группами
return _remoteDatabaseContext.Groups
.Include(g => g.Users) // Загружаем пользователей вместе с группами
.ToList();
}
}