Demo/Data/Repository/GroupRepositoryImpl.cs

55 lines
2.1 KiB
C#
Raw Normal View History

2024-10-28 03:24:11 +00:00
using Demo.Domain.Models;
using Demo.Data.LocalData;
2024-10-28 03:24:11 +00:00
using Demo.Data.RemoteData.RemoteDataBase;
2024-10-24 20:41:31 +00:00
using Demo.Data.RemoteData.RemoteDataBase.DAO;
namespace Demo.Data.Repository
{
2024-10-24 20:41:31 +00:00
public class SQLGroupRepositoryImpl : IGroupRepository
{
2024-10-24 20:41:31 +00:00
private readonly RemoteDatabaseContext _remoteDatabaseContext;
2024-10-28 03:24:11 +00:00
public SQLGroupRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext){
2024-10-24 20:41:31 +00:00
_remoteDatabaseContext = remoteDatabaseContext;
2024-10-28 03:24:11 +00:00
GetAllGroups = _remoteDatabaseContext.Groups.Select(x => new GroupLocalEntity{Name = x.Name, ID = x.ID}).ToList();
}
2024-10-28 03:24:11 +00:00
public List<GroupLocalEntity> GetAllGroups
{ get; set; }
2024-10-21 09:07:49 +00:00
public List<GroupLocalEntity> GetAllGroup()
{
2024-10-28 03:24:11 +00:00
return _remoteDatabaseContext.Groups.Select(x => new GroupLocalEntity{Name = x.Name, ID = x.ID}).ToList();
2024-10-21 09:07:49 +00:00
}
2024-10-28 03:24:11 +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-24 20:41:31 +00:00
public GroupLocalEntity? CreateGroup(string Name) {
2024-10-28 03:24:11 +00:00
GroupDAO groupDAO = new GroupDAO{Name = Name};
2024-10-24 20:41:31 +00:00
var result = _remoteDatabaseContext.Groups.Add(groupDAO);
_remoteDatabaseContext.SaveChanges();
2024-10-28 03:24:11 +00:00
return new GroupLocalEntity{Name = groupDAO.Name, ID = groupDAO.ID};
}
2024-10-21 09:07:49 +00:00
2024-10-28 03:24:11 +00:00
public GroupLocalEntity? UpdateGroup(GroupLocalEntity updatedGroup){
var group = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.ID == updatedGroup.ID);
if (group == null){
return null;
}
group.Name = updatedGroup.Name;
2024-10-24 20:41:31 +00:00
_remoteDatabaseContext.SaveChanges();
2024-10-28 03:24:11 +00:00
return new GroupLocalEntity{Name = group.Name, ID = group.ID};
2024-10-21 09:07:49 +00:00
}
2024-10-28 03:24:11 +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:07:49 +00:00
}
}
2024-10-28 03:24:11 +00:00
}