95 lines
2.4 KiB
C#
95 lines
2.4 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|