76 lines
2.1 KiB
C#
76 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Demo.Data.RemoteData.RemoteDataBase.DAO; // DAO User
|
|
using UserDomain = Demo.Domain.Models.User; // Доменная модель
|
|
using UserEntity = Demo.Data.LocalData.Entity.User; // Локальная сущность
|
|
|
|
namespace Demo.Data.Repository
|
|
{
|
|
public class SQLUserRepositoryImpl : IUserRepository
|
|
{
|
|
private readonly RemoteDatabaseContext _context;
|
|
|
|
public SQLUserRepositoryImpl(RemoteDatabaseContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public void AddUser(UserDomain user)
|
|
{
|
|
var daoUser = new UserEntity // Используем локальную сущность
|
|
{
|
|
Id = user.Id,
|
|
FIO = user.FIO,
|
|
GroupID = user.GroupID
|
|
};
|
|
_context.Users.Add(daoUser);
|
|
_context.SaveChanges();
|
|
}
|
|
|
|
public IEnumerable<UserDomain> GetAllUsers()
|
|
{
|
|
return _context.Users.Select(u => new UserDomain
|
|
{
|
|
Id = u.Id,
|
|
FIO = u.FIO,
|
|
GroupID = u.GroupID
|
|
}).ToList();
|
|
}
|
|
|
|
public UserDomain GetUserById(Guid id)
|
|
{
|
|
var daoUser = _context.Users.Find(id);
|
|
if (daoUser == null) return null;
|
|
|
|
return new UserDomain
|
|
{
|
|
Id = daoUser.Id,
|
|
FIO = daoUser.FIO,
|
|
GroupID = daoUser.GroupID
|
|
};
|
|
}
|
|
|
|
public void UpdateUser(UserDomain user)
|
|
{
|
|
var daoUser = _context.Users.Find(user.Id);
|
|
if (daoUser != null)
|
|
{
|
|
daoUser.FIO = user.FIO;
|
|
daoUser.GroupID = user.GroupID;
|
|
_context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public void DeleteUser(Guid id)
|
|
{
|
|
var daoUser = _context.Users.Find(id);
|
|
if (daoUser != null)
|
|
{
|
|
_context.Users.Remove(daoUser);
|
|
_context.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|