117 lines
3.7 KiB
C#
117 lines
3.7 KiB
C#
|
using Demo.Data.RemoteData.RemoteDataBase;
|
|||
|
using Demo.Data.RemoteData.RemoteDataBase.DAO;
|
|||
|
using Demo.domain.Models;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Demo.Data.Repository
|
|||
|
{
|
|||
|
public class SQLGroupRepositoryImpl : IGroupRepository
|
|||
|
{
|
|||
|
private readonly RemoteDatabaseContext _remoteDatabaseContext;
|
|||
|
|
|||
|
public SQLGroupRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
|
|||
|
{
|
|||
|
_remoteDatabaseContext = remoteDatabaseContext;
|
|||
|
}
|
|||
|
|
|||
|
// Метод для добавления новой группы
|
|||
|
public bool AddGroup(GroupDao newGroup)
|
|||
|
{
|
|||
|
var groupDao = new GroupDao
|
|||
|
{
|
|||
|
Name = newGroup.Name
|
|||
|
};
|
|||
|
_remoteDatabaseContext.Groups.Add(groupDao);
|
|||
|
_remoteDatabaseContext.SaveChanges();
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
// Метод для получения группы по ID
|
|||
|
public GroupDao GetGroupById(int groupId)
|
|||
|
{
|
|||
|
var groupDao = _remoteDatabaseContext.Groups
|
|||
|
.Include(g => g.Users)
|
|||
|
.FirstOrDefault(g => g.Id == groupId);
|
|||
|
if (groupDao == null) return null;
|
|||
|
|
|||
|
return new GroupDao
|
|||
|
{
|
|||
|
Id = groupDao.Id,
|
|||
|
Name = groupDao.Name,
|
|||
|
Users = groupDao.Users.Select(u => new UserDao
|
|||
|
{
|
|||
|
UserId = u.UserId,
|
|||
|
FIO = u.FIO,
|
|||
|
GroupId = u.GroupId
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
// Метод для получения всех групп
|
|||
|
public List<GroupDao> GetAllGroups()
|
|||
|
{
|
|||
|
return _remoteDatabaseContext.Groups
|
|||
|
.Include(g => g.Users)
|
|||
|
.Select(g => new GroupDao
|
|||
|
{
|
|||
|
Id = g.Id,
|
|||
|
Name = g.Name,
|
|||
|
Users = g.Users.Select(u => new UserDao
|
|||
|
{
|
|||
|
UserId = u.UserId,
|
|||
|
FIO = u.FIO,
|
|||
|
GroupId = u.GroupId
|
|||
|
}).ToList()
|
|||
|
})
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
// Метод для обновления группы по ID
|
|||
|
public bool UpdateGroupById(int groupId, GroupDao updatedGroup)
|
|||
|
{
|
|||
|
var groupDao = _remoteDatabaseContext.Groups
|
|||
|
.Include(g => g.Users)
|
|||
|
.FirstOrDefault(g => g.Id == groupId);
|
|||
|
if (groupDao == null) return false;
|
|||
|
|
|||
|
groupDao.Name = updatedGroup.Name;
|
|||
|
// Пример обновления списка пользователей
|
|||
|
groupDao.Users = updatedGroup.Users.Select(user => new UserDao
|
|||
|
{
|
|||
|
UserId = user.UserId,
|
|||
|
FIO = user.FIO,
|
|||
|
GroupId = user.GroupId
|
|||
|
}).ToList();
|
|||
|
|
|||
|
_remoteDatabaseContext.SaveChanges();
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
// Метод для удаления группы по ID
|
|||
|
public bool RemoveGroupById(int groupId)
|
|||
|
{
|
|||
|
var groupDao = _remoteDatabaseContext.Groups.Find(groupId);
|
|||
|
if (groupDao == null) return false;
|
|||
|
|
|||
|
_remoteDatabaseContext.Groups.Remove(groupDao);
|
|||
|
_remoteDatabaseContext.SaveChanges();
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool AddGroup(string Name)
|
|||
|
{
|
|||
|
|
|||
|
var groupDao = new GroupDao
|
|||
|
{
|
|||
|
Name = Name
|
|||
|
};
|
|||
|
_remoteDatabaseContext.Groups.Add(groupDao);
|
|||
|
_remoteDatabaseContext.SaveChanges();
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|