presence_new/data/Repository/AdminRepositoryImp.cs

58 lines
1.9 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using presence.data.RemoteData;
using presence.data.RemoteData.RemoteDataBase;
using presence.data.RemoteData.RemoteDataBase.DAO;
using presence.data.Repository;
namespace data.Repository
{
public class AdminRepositoryImp: IAdminRepository
{
private readonly RemoteDataBaseContext _remoteDataBaseContext;
public AdminRepositoryImp(RemoteDataBaseContext remoteDataBaseContext)
{
_remoteDataBaseContext = remoteDataBaseContext;
}
2024-12-13 07:21:14 +00:00
public bool AddStudents(GroupDao group, List<UserDao> students)
{
_remoteDataBaseContext.Groups.Add(group);
2024-11-19 12:41:31 +00:00
_remoteDataBaseContext.SaveChanges();
2024-12-13 07:21:14 +00:00
foreach (UserDao student in students)
{
2024-12-13 07:21:14 +00:00
_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,
User = _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;
}
}
}