presence123/Demo/Data/Repository/SQLUserRepositoryImpl.cs

44 lines
1.4 KiB
C#
Raw Normal View History

2024-10-25 08:47:11 +00:00
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;
}
2024-10-28 12:03:51 +00:00
public IEnumerable<UserDao> GetAllUsers => _remoteDatabaseContext.Users;
2024-10-25 08:47:11 +00:00
2024-10-28 12:03:51 +00:00
public bool RemoveUserById(int userId)
2024-10-25 08:47:11 +00:00
{
var user = _remoteDatabaseContext.Users.FirstOrDefault(u => u.UserId == userId);
if (user == null) throw new UserNotFoundException(userId);
_remoteDatabaseContext.Users.Remove(user);
return true;
}
2024-10-28 12:03:51 +00:00
public UserDao? UpdateUser(UserDao user)
2024-10-25 08:47:11 +00:00
{
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;
}
}
}