Demo/Data/Repository/GroupRepositoryImpl.cs

94 lines
2.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2024-11-06 04:10:09 +00:00
using Microsoft.EntityFrameworkCore;
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 SQLGroupRepositoryImpl : IGroupRepository
{
2024-11-06 04:10:09 +00:00
private readonly RemoteDataBaseContext _remoteDatabaseContext;
2024-10-23 07:06:56 +00:00
2024-11-06 04:10:09 +00:00
public SQLGroupRepositoryImpl(RemoteDataBaseContext remoteDatabaseContext)
2024-10-23 07:06:56 +00:00
{
2024-11-06 04:10:09 +00:00
_remoteDatabaseContext = remoteDatabaseContext;
2024-10-23 07:06:56 +00:00
}
2024-11-06 04:10:09 +00:00
public List<GroupLocalEntity> GetAllGroups() => LocalStaticData.groups;
public bool AddGroup(GroupDao group)
2024-10-23 07:06:56 +00:00
{
2024-11-06 04:10:09 +00:00
var groupDao = new GroupDao
{
Name = group.Name
};
_remoteDatabaseContext.Groups.Add(groupDao);
_remoteDatabaseContext.SaveChanges();
return true;
2024-10-23 07:06:56 +00:00
}
2024-11-06 04:10:09 +00:00
public List<GroupDao> GetAllGroup()
{
return _remoteDatabaseContext.Groups
.Include(g => g.Users)
.Select(g => new GroupDao
{
Name = g.Name,
Id = g.Id,
Users = g.Users.Select(u => new UserDao
{
UserId = u.UserId,
FIO = u.FIO,
GroupId = u.GroupId,
}).ToList()
}).ToList();
}
2024-10-23 07:06:56 +00:00
2024-11-06 04:10:09 +00:00
public GroupDao GetGroupById(int groupID)
{
2024-11-06 04:10:09 +00:00
var groupLocal = _remoteDatabaseContext.Groups
.Where(g => g.Id == groupID).FirstOrDefault();
if (groupLocal == null) return null;
return groupLocal;
}
2024-11-06 04:10:09 +00:00
public bool RemoveGroupById(int groupID)
{
var groupLocal = _remoteDatabaseContext.Groups
2024-10-23 07:06:56 +00:00
.Where(x => x.Id == groupID).FirstOrDefault();
if (groupLocal == null) return false;
2024-11-06 04:10:09 +00:00
_remoteDatabaseContext.Groups.Remove(groupLocal);
_remoteDatabaseContext.SaveChanges();
return true;
2024-10-23 07:06:56 +00:00
}
2024-11-06 04:10:09 +00:00
public bool UpdateGroupById(int groupID, string name)
2024-10-23 07:06:56 +00:00
{
2024-11-06 04:10:09 +00:00
var groupLocal = _remoteDatabaseContext.Groups
.Include(g => g.Users)
.Where(x => x.Id == groupID).FirstOrDefault();
2024-10-23 07:06:56 +00:00
if (groupLocal == null) return false;
2024-11-06 04:10:09 +00:00
2024-10-18 08:43:40 +00:00
groupLocal.Name = name;
2024-11-06 04:10:09 +00:00
groupLocal.Users = _remoteDatabaseContext.Users
.Where(x => x.GroupId == groupLocal.Id)
.Select(user => new UserDao
{
UserId = user.UserId,
FIO = user.FIO,
GroupId = user.GroupId
}).ToList();
_remoteDatabaseContext.SaveChanges();
2024-10-23 07:06:56 +00:00
return true;
2024-10-18 08:43:40 +00:00
}
}
2024-11-06 04:10:09 +00:00
}