2024-11-08 16:52:52 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-11-14 07:53:42 +00:00
|
|
|
|
if (_remoteDatabaseContext.Groups.Select(g=>g.Id).Contains(groupId))
|
|
|
|
|
{
|
|
|
|
|
var groupDao = _remoteDatabaseContext.Groups
|
|
|
|
|
.Include(g => g.Users)
|
|
|
|
|
.FirstOrDefault(g => g.Id == groupId);
|
|
|
|
|
if (groupDao == null) return null;
|
2024-11-08 16:52:52 +00:00
|
|
|
|
|
2024-11-14 07:53:42 +00:00
|
|
|
|
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()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-11-08 16:52:52 +00:00
|
|
|
|
{
|
2024-11-14 07:53:42 +00:00
|
|
|
|
return new GroupDao
|
2024-11-08 16:52:52 +00:00
|
|
|
|
{
|
2024-11-14 07:53:42 +00:00
|
|
|
|
Id=-1,
|
|
|
|
|
Name="Группы не существует"
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-11-08 16:52:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Метод для получения всех групп
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|