Demo/Data/Repository/GroupRepositoryImpl.cs

55 lines
2.1 KiB
C#
Raw Permalink Normal View History

2024-10-17 11:46:19 +00:00
using Demo.Domain.Models;
using Demo.Data.LocalData;
2024-10-24 15:57:52 +00:00
using Demo.Data.RemoteData.RemoteDataBase;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
2024-10-17 11:46:19 +00:00
namespace Demo.Data.Repository
{
2024-10-24 15:57:52 +00:00
public class SQLGroupRepositoryImpl : IGroupRepository
2024-10-17 11:46:19 +00:00
{
2024-10-24 15:57:52 +00:00
private readonly RemoteDatabaseContext _remoteDatabaseContext;
2024-10-17 11:46:19 +00:00
2024-10-24 15:57:52 +00:00
public SQLGroupRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext){
_remoteDatabaseContext = remoteDatabaseContext;
GetAllGroups = _remoteDatabaseContext.Groups.Select(x => new GroupLocalEntity{Name = x.Name, ID = x.ID}).ToList();
2024-10-17 11:46:19 +00:00
}
2024-10-24 15:57:52 +00:00
2024-10-17 11:46:19 +00:00
public List<GroupLocalEntity> GetAllGroups
{ get; set; }
2024-10-21 09:38:07 +00:00
public List<GroupLocalEntity> GetAllGroup()
2024-10-17 11:46:19 +00:00
{
2024-10-24 15:57:52 +00:00
return _remoteDatabaseContext.Groups.Select(x => new GroupLocalEntity{Name = x.Name, ID = x.ID}).ToList();
2024-10-21 09:38:07 +00:00
}
2024-10-24 15:57:52 +00:00
public GroupLocalEntity? GetGroupById(int ID){
var groupDAO = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.ID == ID);
return new GroupLocalEntity{Name = groupDAO.Name, ID = groupDAO.ID};
2024-10-17 11:46:19 +00:00
}
2024-10-24 15:57:52 +00:00
public GroupLocalEntity? CreateGroup(string Name) {
GroupDAO groupDAO = new GroupDAO{Name = Name};
var result = _remoteDatabaseContext.Groups.Add(groupDAO);
_remoteDatabaseContext.SaveChanges();
return new GroupLocalEntity{Name = groupDAO.Name, ID = groupDAO.ID};
2024-10-17 11:46:19 +00:00
}
2024-10-21 09:38:07 +00:00
2024-10-24 15:57:52 +00:00
public GroupLocalEntity? UpdateGroup(GroupLocalEntity updatedGroup){
var group = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.ID == updatedGroup.ID);
if (group == null){
return null;
}
2024-10-21 09:38:07 +00:00
2024-10-24 15:57:52 +00:00
group.Name = updatedGroup.Name;
_remoteDatabaseContext.SaveChanges();
return new GroupLocalEntity{Name = group.Name, ID = group.ID};
2024-10-21 09:38:07 +00:00
}
2024-10-24 15:57:52 +00:00
public bool RemoveGroupByID(int ID){
var groupDAO = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.ID == ID);
_remoteDatabaseContext.Groups.Remove(groupDAO);
_remoteDatabaseContext.SaveChanges();
return true;
2024-10-21 09:38:07 +00:00
}
2024-10-17 11:46:19 +00:00
}
}