presence/Demo/Data/Repository/SQLUserRepositoryImpl.cs
2024-10-28 15:03:51 +03:00

44 lines
1.4 KiB
C#

using Demo.Data.Exceptions;
using Demo.Data.RemoteData.RemoteDataBase;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
using Demo.domain.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Demo.Data.Repository
{
public class SQLUserRepositoryImpl : IUserRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLUserRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
public IEnumerable<UserDao> GetAllUsers => _remoteDatabaseContext.Users;
public bool RemoveUserById(int userId)
{
var user = _remoteDatabaseContext.Users.FirstOrDefault(u => u.UserId == userId);
if (user == null) throw new UserNotFoundException(userId);
_remoteDatabaseContext.Users.Remove(user);
return true;
}
public UserDao? UpdateUser(UserDao user)
{
var existingUser = _remoteDatabaseContext.Users.FirstOrDefault(u => u.UserId == user.UserId);
if (existingUser == null) throw new UserNotFoundException(user.UserId);
existingUser.FIO = user.FIO;
existingUser.GroupId = user.GroupId;
return existingUser;
}
}
}