59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
|
using Demo.Data.RemoteData.RemoteDataBase.DAO;
|
|||
|
using Demo.Data.RemoteData.RemoteDataBase;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace data.Repository
|
|||
|
{
|
|||
|
public class AdminRepositoryImp : IAdminRepository
|
|||
|
{
|
|||
|
private readonly RemoteDatabaseContext _remoteDataBaseContext;
|
|||
|
|
|||
|
public AdminRepositoryImp(RemoteDatabaseContext remoteDataBaseContext)
|
|||
|
{
|
|||
|
_remoteDataBaseContext = remoteDataBaseContext;
|
|||
|
}
|
|||
|
|
|||
|
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 IEnumerable<GroupDao> GetAllGroupsWithStudents()
|
|||
|
{
|
|||
|
return _remoteDataBaseContext.Groups.Select(x => new GroupDao
|
|||
|
{
|
|||
|
Id = x.Id,
|
|||
|
Name = x.Name,
|
|||
|
Users = _remoteDataBaseContext.Users.Where(it => it.GroupId == x.Id).ToList()
|
|||
|
}).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public UserDao GetStudentInfo(int userId)
|
|||
|
{
|
|||
|
return _remoteDataBaseContext.Users.Where(x => x.UserId == userId).FirstOrDefault();
|
|||
|
}
|
|||
|
|
|||
|
public bool RemoveUserById(int userId, int groupId)
|
|||
|
{
|
|||
|
var userLocal = _remoteDataBaseContext.Users
|
|||
|
.Where(x => x.UserId == userId && x.GroupId == groupId).FirstOrDefault();
|
|||
|
if (userLocal == null) return false;
|
|||
|
|
|||
|
_remoteDataBaseContext.Users.Remove(userLocal);
|
|||
|
_remoteDataBaseContext.SaveChanges();
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|