using Demo.Data.Entity; using Demo.Data.RemoteData.RemoteDataBase; using Demo.Data.RemoteData.RemoteDataBase.DAO; using System.Collections.Generic; using System.Linq; namespace Demo.Data.Repository { class SQLGroupRepositoryLmpl : IGroupRepository { private readonly RemoteDatabaseContex _remoteDatabaseContext; public SQLGroupRepositoryLmpl(RemoteDatabaseContex remoteDatabaseContext) { _remoteDatabaseContext = remoteDatabaseContext; } public bool AddGroup(GroupLocalEntity newGroup) { GroupDao groupDao = new GroupDao { Name = newGroup.Name }; var result = _remoteDatabaseContext.Groups.Add(groupDao); _remoteDatabaseContext.SaveChanges(); if (result != null) return true; return false; } public List GetAllGroup() { var groups = _remoteDatabaseContext.Groups.ToList(); return groups.Select(g => new GroupLocalEntity { Id = g.Id, Name = g.Name }).ToList(); } public GroupLocalEntity GetGroupById(int groupID) { var group = _remoteDatabaseContext.Groups .FirstOrDefault(g => g.Id == groupID); if (group == null) { return null; } return new GroupLocalEntity { Id = group.Id, Name = group.Name }; } public bool RemoveGroupById(int groupID) { var group = _remoteDatabaseContext.Groups .FirstOrDefault(g => g.Id == groupID); if (group == null) { return false; } _remoteDatabaseContext.Groups.Remove(group); _remoteDatabaseContext.SaveChanges(); return true; } 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; } } }