xxxproject/Demo/Data/Repository/SQLUserRepositoeyLmpl.cs

95 lines
2.4 KiB
C#
Raw Normal View History

2024-10-30 09:11:19 +00:00
using Demo.Data.Entity;
using Demo.Data.RemoteData.RemoteDataBase;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
using System.Collections.Generic;
using System.Linq;
namespace Demo.Data.Repository
{
public class SQLUserRepositoryImpl : IUserRepository
{
private readonly RemoteDatabaseContex _remoteDatabaseContext;
public SQLUserRepositoryImpl(RemoteDatabaseContex remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
public bool AddUser(UserLocalEnity newUser)
{
var userDao = new UserDao
{
Guid = Guid.NewGuid(),
F10 = newUser.FIO,
GroupId = newUser.GroupID
};
_remoteDatabaseContext.Users.Add(userDao);
return _remoteDatabaseContext.SaveChanges() > 0;
}
public List<UserLocalEnity> GetAllUsers()
{
return _remoteDatabaseContext.Users
.Select(user => new UserLocalEnity
{
Guid = user.Guid,
FIO = user.F10,
GroupID = user.GroupId
})
.ToList();
}
public UserLocalEnity GetUserById(Guid userID)
{
var user = _remoteDatabaseContext.Users.Find(userID);
if (user == null)
{
return null;
}
return new UserLocalEnity
{
Guid = user.Guid,
GroupID = user.GroupId,
FIO = user.F10
};
}
public bool RemoveUserById(Guid userID)
{
var user = _remoteDatabaseContext.Users.Find(userID);
if (user == null)
{
return false;
}
_remoteDatabaseContext.Users.Remove(user);
_remoteDatabaseContext.SaveChanges();
return true;
}
public bool UpdateUserById(Guid userID, UserLocalEnity updatedUser)
{
var user = _remoteDatabaseContext.Users.Find(userID);
if (user == null)
{
return false;
}
user.GroupId = updatedUser.GroupID;
user.Guid = updatedUser.Guid;
_remoteDatabaseContext.SaveChanges();
return true;
}
}
}