Raspisanie/Zurnal/Date/Repository/SQLRepos/SQLUserRepositoryImpl.cs

61 lines
1.8 KiB
C#
Raw Normal View History

2024-11-11 11:41:23 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Zurnal.Data.Repository;
using Zurnal.domain.Models;
using Zurnal.RemaDateBase.DateDao;
namespace Zurnal.Date.Repository.SQLRepos
{
public class SQLUserRepositoryImpl : IUserRepository
{
private readonly RemoteDateBaseContext _remoteDataBaseContext;
public SQLUserRepositoryImpl(RemoteDateBaseContext remoteDataBaseContext)
{
_remoteDataBaseContext = remoteDataBaseContext;
}
public List<UserDao> GetAllUsers =>
_remoteDataBaseContext.User.Select(user => new UserDao
{
UserGuid = user.UserGuid,
FIO = user.FIO,
GroupID = user.GroupID,
}).ToList();
public bool RemoveUserByGuid(Guid userGuid)
{
var user = _remoteDataBaseContext.User.Find(userGuid);
if (user == null) return false;
_remoteDataBaseContext.User.Remove(user);
_remoteDataBaseContext.SaveChanges();
return true;
}
public UserDao? GetUserByGuid(Guid userGuid)
{
var user = _remoteDataBaseContext.User.Find(userGuid);
if (user == null) return null;
return new UserDao
{
UserGuid = user.UserGuid,
FIO = user.FIO,
GroupID = user.GroupID,
};
}
public UserDao? UpdateUser(UserDao userUpdateLocalEnity)
{
var user = _remoteDataBaseContext.User.Find(userUpdateLocalEnity.UserGuid);
if (user == null) return null;
user.FIO = userUpdateLocalEnity.FIO;
user.GroupID = userUpdateLocalEnity.GroupID;
_remoteDataBaseContext.SaveChanges();
return userUpdateLocalEnity;
}
}
}