2024-11-16 12:02:17 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using presence.data.Entity;
|
|
|
|
using presence.data.LocalData;
|
|
|
|
using presence.data.LocalData.Entity;
|
|
|
|
using presence.data.RemoteData.RemoteDataBase;
|
|
|
|
using presence.data.RemoteData.RemoteDataBase.DAO;
|
|
|
|
|
|
|
|
namespace presence.data.Repository
|
|
|
|
{
|
|
|
|
public class SQLGroupRepositoryImpl : IGroupRepository
|
|
|
|
{
|
|
|
|
private readonly RemoteDataBaseContext _remoteDatabaseContext;
|
|
|
|
|
|
|
|
public SQLGroupRepositoryImpl(RemoteDataBaseContext remoteDatabaseContext)
|
|
|
|
{
|
|
|
|
_remoteDatabaseContext = remoteDatabaseContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool AddGroup(GroupDao group)
|
|
|
|
{
|
|
|
|
var groupDao = new GroupDao
|
|
|
|
{
|
|
|
|
Name = group.Name
|
|
|
|
};
|
|
|
|
_remoteDatabaseContext.Groups.Add(groupDao);
|
|
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-12-13 07:21:14 +00:00
|
|
|
public List<GroupDao> GetAllGroup()
|
2024-11-16 12:02:17 +00:00
|
|
|
{
|
|
|
|
return _remoteDatabaseContext.Groups
|
2024-12-17 09:13:12 +00:00
|
|
|
.Include(g => g.User)
|
2024-12-13 07:21:14 +00:00
|
|
|
.Select(g => new GroupDao
|
2024-11-16 12:02:17 +00:00
|
|
|
{
|
|
|
|
Name = g.Name,
|
2024-12-17 09:13:12 +00:00
|
|
|
Id = g.Id,
|
|
|
|
User = g.User.Select(u => new UserDao
|
|
|
|
{
|
|
|
|
FIO = u.FIO,
|
|
|
|
UserId = u.UserId,
|
|
|
|
GroupId = u.GroupId
|
|
|
|
}).ToList()
|
2024-11-16 12:02:17 +00:00
|
|
|
}).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public GroupDao GetGroupById(int groupID)
|
|
|
|
{
|
|
|
|
var groupLocal = _remoteDatabaseContext.Groups
|
|
|
|
.Where(g => g.Id == groupID).FirstOrDefault();
|
|
|
|
if (groupLocal == null) return null;
|
|
|
|
return groupLocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool RemoveGroupById(int groupID)
|
|
|
|
{
|
|
|
|
var groupLocal = _remoteDatabaseContext.Groups
|
|
|
|
.Where(x => x.Id == groupID).FirstOrDefault();
|
|
|
|
if (groupLocal == null) return false;
|
2024-11-18 22:12:53 +00:00
|
|
|
var userLocal = _remoteDatabaseContext.Users.Where(x => x.GroupId == groupID).ToList();
|
|
|
|
if (userLocal == null) return false;
|
|
|
|
foreach (var user in userLocal)
|
|
|
|
{
|
|
|
|
_remoteDatabaseContext.Users.Remove(user);
|
|
|
|
}
|
2024-11-16 12:02:17 +00:00
|
|
|
_remoteDatabaseContext.Groups.Remove(groupLocal);
|
|
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool UpdateGroupById(int groupID, string name)
|
|
|
|
{
|
|
|
|
var groupLocal = _remoteDatabaseContext.Groups
|
|
|
|
.Include(g => g.User)
|
|
|
|
.Where(x => x.Id == groupID).FirstOrDefault();
|
|
|
|
if (groupLocal == null) return false;
|
|
|
|
|
|
|
|
groupLocal.Name = name;
|
|
|
|
|
|
|
|
groupLocal.User = _remoteDatabaseContext.Users
|
|
|
|
.Where(x => x.GroupId == groupLocal.Id)
|
|
|
|
.Select(user => new UserDao
|
|
|
|
{
|
|
|
|
UserId = user.UserId,
|
|
|
|
FIO = user.FIO,
|
|
|
|
GroupId = user.GroupId
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
return true;
|
|
|
|
}
|
2024-12-13 07:21:14 +00:00
|
|
|
|
|
|
|
public bool AddStudents(GroupDao group, List<UserDao> students)
|
|
|
|
{
|
|
|
|
_remoteDatabaseContext.Groups.Add(group);
|
|
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
foreach (UserDao student in students)
|
|
|
|
{
|
|
|
|
_remoteDatabaseContext.Users.Add(student);
|
|
|
|
}
|
|
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public async Task<IEnumerable<GroupDao>> getAllGroupAsync()
|
|
|
|
{
|
|
|
|
return await _remoteDatabaseContext.Groups.Include(group => group.User).ToListAsync();
|
|
|
|
}
|
2024-11-16 12:02:17 +00:00
|
|
|
}
|
|
|
|
}
|