presence/Data/Repository/SQLUserRepository.cs
Class_Student e771fe1659 new
2024-12-06 11:51:13 +03:00

94 lines
2.7 KiB
C#

using presence.Data.LocalData;
using presence.Data.LocalData.Entity;
using presence.Data.RemoteData.RemoteDatabase;
using presence.Data.RemoteData.RemoteDatabase.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace presence.Data.Repository
{
public class SQLUserRepositoryImpl : IUserRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLUserRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
public UserDao FindUserById(int userId)
{
var user = _remoteDatabaseContext.Users
.FirstOrDefault(x => x.UserID == userId);
if (user == null) throw new Exception("Пользователь не найден");
return user;
}
public List<UserDao> GetAllUser()
{
return _remoteDatabaseContext.Users
.Select(u => new UserDao
{
UserID = u.UserID,
UserFIO = u.UserFIO,
GroupID = u.GroupID,
Group = u.Group
})
.ToList();
}
public UserDao? GetUserById(int userId)
{
return _remoteDatabaseContext.Users
.FirstOrDefault(x => x.UserID == userId);
}
public bool RemoveUserById(int userId)
{
try
{
var user = _remoteDatabaseContext.Users
.FirstOrDefault(x => x.UserID == userId);
if (user == null) return false;
_remoteDatabaseContext.Users.Remove(user);
_remoteDatabaseContext.SaveChanges();
return true;
}
catch
{
return false;
}
}
public UserDao? UpdateUser(UserDao userUpdate)
{
try
{
var user = _remoteDatabaseContext.Users
.FirstOrDefault(x => x.UserID == userUpdate.UserID);
if (user == null) return null;
user.UserFIO = userUpdate.UserFIO;
user.GroupID = userUpdate.GroupID;
_remoteDatabaseContext.SaveChanges();
return user;
}
catch
{
return null;
}
}
public UserDao? UpdateUserById(int userId)
{
return _remoteDatabaseContext.Users
.FirstOrDefault(x => x.UserID == userId);
}
}
}