presence/data/Repository/SQLGroupRepositoryImpl.cs

70 lines
2.0 KiB
C#
Raw Normal View History

2024-11-13 09:00:26 +00:00
using data.Exception;
2024-11-11 09:07:11 +00:00
using data.LocalData;
using data.RemoteData.RemoteDataBase;
using data.RemoteData.RemoteDataBase.DAO;
using data.Repository;
using domain.Models;
using System.Collections.Generic;
using System.Linq;
public class SQLGroupRepositoryImpl : IGroupRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLGroupRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
2024-11-13 09:00:26 +00:00
2024-11-11 09:07:11 +00:00
public GroupLocalEntity? GetGroupById(int groupId)
{
var groupDao = _remoteDatabaseContext.Groups.FirstOrDefault(g => g.Id == groupId);
return groupDao != null ? new GroupLocalEntity { Id = groupDao.Id, Name = groupDao.Name } : null;
}
2024-11-13 09:00:26 +00:00
2024-11-11 09:07:11 +00:00
public List<GroupLocalEntity> GetAllGroup()
{
return _remoteDatabaseContext.Groups
.Select(g => new GroupLocalEntity { Id = g.Id, Name = g.Name })
.ToList();
}
2024-11-13 09:00:26 +00:00
2024-11-11 09:07:11 +00:00
public bool AddGroup(GroupLocalEntity group)
{
if (_remoteDatabaseContext.Groups.Any(g => g.Id == group.Id))
return false;
var groupDao = new GroupDao { Id = group.Id, Name = group.Name };
_remoteDatabaseContext.Groups.Add(groupDao);
_remoteDatabaseContext.SaveChanges();
return true;
}
2024-11-13 09:00:26 +00:00
2024-11-11 09:07:11 +00:00
public bool UpdateGroupById(int groupID, GroupLocalEntity updatedGroup)
{
var existingGroup = _remoteDatabaseContext.Groups.FirstOrDefault(g => g.Id == groupID);
if (existingGroup == null)
return false;
existingGroup.Name = updatedGroup.Name;
_remoteDatabaseContext.SaveChanges();
return true;
}
2024-11-13 09:00:26 +00:00
2024-11-11 09:07:11 +00:00
public bool RemoveGroupById(int groupID)
{
var existingGroup = _remoteDatabaseContext.Groups.FirstOrDefault(g => g.Id == groupID);
if (existingGroup == null)
return false;
_remoteDatabaseContext.Groups.Remove(existingGroup);
_remoteDatabaseContext.SaveChanges();
return true;
}
}