53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
using data.RemoteData.RemoteDataBase;
|
|
using data.RemoteData.RemoteDatabase.DAO;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace data.Repository;
|
|
|
|
public class SQLGroupRepository : IGroupRepository
|
|
{
|
|
private readonly RemoteDatabaseContext _context;
|
|
|
|
public SQLGroupRepository(RemoteDatabaseContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
public List<GroupDAO> GetAllGroups()
|
|
{
|
|
return _context.Groups.ToList();
|
|
}
|
|
|
|
public void AddGroup(string groupName)
|
|
{
|
|
_context.Groups.Add(new GroupDAO
|
|
{
|
|
Id = _context.Groups.Select(g=>g.Id).Max() + 1,
|
|
Name = groupName
|
|
});
|
|
_context.SaveChanges();
|
|
}
|
|
|
|
public bool UpdateGroup(int groupId, GroupDAO newGroup)
|
|
{
|
|
var GroupDAO = _context.Groups
|
|
.Include(g => g.Users)
|
|
.FirstOrDefault(g => g.Id == groupId);
|
|
if (GroupDAO == null) return false;
|
|
|
|
GroupDAO.Name = newGroup.Name;
|
|
GroupDAO.Users = newGroup.Users.Select(user => new UserDAO
|
|
{
|
|
UserId = user.UserId,
|
|
FIO = user.FIO,
|
|
GroupId = user.GroupId
|
|
}).ToList();
|
|
|
|
_context.SaveChanges();
|
|
return true;
|
|
}
|
|
|
|
public GroupDAO GetGroupById(int groupId)
|
|
{
|
|
return _context.Groups.FirstOrDefault(g => g.Id == groupId);
|
|
}
|
|
} |