Demo/Data/Repository/UserRepositoryImpl.cs

75 lines
2.4 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-11-06 04:10:09 +00:00
using Microsoft.EntityFrameworkCore.Infrastructure;
using Posechaemost.Data.LocalData;
using Posechaemost.Data.LocalData.Entity;
2024-11-06 04:10:09 +00:00
using Posechaemost.Data.RemoteData.RemoteDataBase;
using Posechaemost.Data.RemoteData.RemoteDataBase.DAO;
namespace Posechaemost.Data.Repository
{
2024-11-06 04:10:09 +00:00
public class SQLUserRepositoryImpl: IUserRepository
{
2024-11-06 04:10:09 +00:00
private readonly RemoteDataBaseContext _remoteDatabaseContext;
public SQLUserRepositoryImpl(RemoteDataBaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
2024-10-23 07:06:56 +00:00
2024-11-06 04:10:09 +00:00
public List<UserDao> GetAllUser()
2024-10-23 07:06:56 +00:00
{
2024-11-06 04:10:09 +00:00
return _remoteDatabaseContext.Users.Select(u => new UserDao
{
FIO = u.FIO,
UserId = u.UserId,
GroupId = u.GroupId
}).ToList();
2024-10-23 07:06:56 +00:00
}
2024-11-06 04:10:09 +00:00
public bool RemoveUserById(int userId)
{
2024-11-06 04:10:09 +00:00
var userLocal = _remoteDatabaseContext.Users
.Where(x => x.UserId== userId).FirstOrDefault();
if (userLocal == null) return false;
2024-11-06 04:10:09 +00:00
_remoteDatabaseContext.Users.Remove(userLocal);
_remoteDatabaseContext.SaveChanges();
return true;
}
2024-11-06 04:10:09 +00:00
public UserDao GetUserById (int userId) {
var userLocal = _remoteDatabaseContext.Users
.Where(x => x.UserId == userId).FirstOrDefault();
if (userLocal == null) return null;
return userLocal;
}
2024-11-06 04:10:09 +00:00
public bool UpdateUser(UserDao userUpdate) {
var userLocal = _remoteDatabaseContext.Users
.Where(x => x.UserId == userUpdate.UserId).FirstOrDefault();
if (userLocal == null) return false;
userLocal.FIO = userUpdate.FIO;
userLocal.GroupId = userUpdate.GroupId;
_remoteDatabaseContext.SaveChanges();
return true;
}
2024-11-06 04:10:09 +00:00
public bool UpdateUserById(int userId, string fio, int groupId) {
var userLocal = _remoteDatabaseContext.Users
.Where(x => x.UserId == userId).FirstOrDefault();
if (userLocal == null) return false;
userLocal.FIO = fio;
userLocal.GroupId = groupId;
_remoteDatabaseContext.SaveChanges();
return true;
}
}
}