Demo/Data/Repository/GroupRepositoryImpl.cs

65 lines
2.3 KiB
C#
Raw Normal View History

using Demo.Data.LocalData;
using Demo.domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-10-24 20:41:31 +00:00
using Data.RemoteData.RemoteDataBase.DAO;
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;
public SQLGroupRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext) {
_remoteDatabaseContext = remoteDatabaseContext;
GetAllGroups = _remoteDatabaseContext.Groups.Select(x => new GroupLocalEntity{Name = x.Name, Id = x.Id}).ToList();
}
public List<GroupLocalEntity> GetAllGroups
{ get; set; }
2024-10-21 09:07:49 +00:00
public List<GroupLocalEntity> GetAllGroup()
{
2024-10-24 20:41:31 +00:00
return _remoteDatabaseContext.Groups.Select(x => new GroupLocalEntity{Name = x.Name, Id = x.Id}).ToList();
2024-10-21 09:07:49 +00:00
}
public GroupLocalEntity? UpdateGroupName(GroupLocalEntity groupUpdateLocalEntity) {
2024-10-24 20:41:31 +00:00
var group = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Id == groupUpdateLocalEntity.Id);
if (group == null){
return null;
}
group.Name = groupUpdateLocalEntity.Name;
_remoteDatabaseContext.SaveChanges();
return new GroupLocalEntity{Name = group.Name, Id = group.Id};
}
2024-10-24 20:41:31 +00:00
public GroupLocalEntity? CreateGroup(string Name) {
2024-10-24 20:41:31 +00:00
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-21 09:07:49 +00:00
public bool RemoveGroupById(int groupID)
{
2024-10-24 20:41:31 +00:00
var groupDao = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Id == groupID);
_remoteDatabaseContext.Groups.Remove(groupDao);
_remoteDatabaseContext.SaveChanges();
return true;
2024-10-21 09:07:49 +00:00
}
2024-10-24 20:41:31 +00:00
public GroupLocalEntity? GetGroupById(int groupId) {
var groupDao = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Id == groupId);
return new GroupLocalEntity{Name = groupDao.Name, Id = groupDao.Id};
2024-10-21 09:07:49 +00:00
}
}
}