xxxproject/Demo/Data/Repository/SQLGroupRepositoryLmpl.cs

91 lines
2.3 KiB
C#
Raw Permalink Normal View History

2024-10-30 09:11:19 +00:00
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<GroupLocalEntity> 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;
}
}
}