presence123/Demo/Data/Repository/SQLUserRepositoryImpl.cs

50 lines
1.7 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 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);
2024-11-01 09:42:31 +00:00
// Обновляем поля существующего пользователя
2024-10-25 08:47:11 +00:00
existingUser.FIO = user.FIO;
existingUser.GroupId = user.GroupId;
2024-11-01 09:42:31 +00:00
_remoteDatabaseContext.SaveChanges();
2024-10-25 08:47:11 +00:00
return existingUser;
}
2024-11-01 09:42:31 +00:00
public List<UserDao> GetAllUsers()
{
// Возвращаем пользователей, отсортированных по UserId
return _remoteDatabaseContext.Users.OrderBy(u => u.UserId).ToList();
}
2024-10-25 08:47:11 +00:00
}
}