Demo/Data/Repository/UserRepositoryImpl.cs

47 lines
1.8 KiB
C#
Raw Normal View History

2024-10-28 03:24:11 +00:00
using Demo.Domain.Models;
using Demo.Data.LocalData;
2024-10-28 03:24:11 +00:00
using Demo.Data.RemoteData.RemoteDataBase;
namespace Demo.Data.Repository
{
2024-10-28 03:24:11 +00:00
public class SQLUserRepositoryImpl : IUserRepository
{
2024-10-24 20:41:31 +00:00
private readonly RemoteDatabaseContext _remoteDatabaseContext;
2024-10-28 03:24:11 +00:00
public SQLUserRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext){
2024-10-24 20:41:31 +00:00
_remoteDatabaseContext = remoteDatabaseContext;
GetAllUsers = _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList();
}
2024-10-28 03:24:11 +00:00
public List<UserLocalEntity> GetAllUsers
{ get; set; }
2024-10-28 03:24:11 +00:00
public List<UserLocalEntity> GetAllUser(){
2024-10-24 20:41:31 +00:00
return _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList();
2024-10-21 09:07:49 +00:00
}
2024-10-28 03:24:11 +00:00
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);
2024-10-24 20:41:31 +00:00
_remoteDatabaseContext.Users.Remove(userDAO);
_remoteDatabaseContext.SaveChanges();
return true;
}
2024-10-28 03:24:11 +00:00
public UserLocalEntity? UpdateUser(UserLocalEntity updatedUser){
var user = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == updatedUser.Guid);
2024-10-24 20:41:31 +00:00
if (user == null){
return null;
}
2024-10-28 03:24:11 +00:00
user.FIO = updatedUser.FIO;
user.GroupID = updatedUser.GroupID;
2024-10-24 20:41:31 +00:00
_remoteDatabaseContext.SaveChanges();
return new UserLocalEntity{FIO = user.FIO, Guid = user.Guid, GroupID = user.GroupID};
}
}
}