Demo/Data/Repository/UserRepositoryImpl.cs
2024-10-28 06:24:11 +03:00

47 lines
1.8 KiB
C#

using Demo.Domain.Models;
using Demo.Data.LocalData;
using Demo.Data.RemoteData.RemoteDataBase;
namespace Demo.Data.Repository
{
public class SQLUserRepositoryImpl : IUserRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLUserRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext){
_remoteDatabaseContext = remoteDatabaseContext;
GetAllUsers = _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList();
}
public List<UserLocalEntity> GetAllUsers
{ get; set; }
public List<UserLocalEntity> GetAllUser(){
return _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList();
}
public UserLocalEntity? GetUserByGuid(Guid guid){
var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == guid);
return new UserLocalEntity{FIO = userDAO.FIO, GroupID = userDAO.GroupID, Guid = guid};
}
public bool RemoveUserByGuid(Guid guid){
var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == guid);
_remoteDatabaseContext.Users.Remove(userDAO);
_remoteDatabaseContext.SaveChanges();
return true;
}
public UserLocalEntity? UpdateUser(UserLocalEntity updatedUser){
var user = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == updatedUser.Guid);
if (user == null){
return null;
}
user.FIO = updatedUser.FIO;
user.GroupID = updatedUser.GroupID;
_remoteDatabaseContext.SaveChanges();
return new UserLocalEntity{FIO = user.FIO, Guid = user.Guid, GroupID = user.GroupID};
}
}
}