presence/Data/Repository/SQLGroupRepository.cs

122 lines
3.3 KiB
C#
Raw Normal View History

2024-12-06 08:51:13 +00:00
using presence.Data.LocalData;
using presence.Data.LocalData.Entity;
using presence.Data.RemoteData.RemoteDatabase;
using presence.Data.RemoteData.RemoteDatabase.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace presence.Data.Repository
{
public class SQLGroupRepositoryImpl : IGroupRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLGroupRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
public List<GroupDao> GetAllGroups()
{
var groups = _remoteDatabaseContext.Groups
.Select(g => new GroupDao
{
ID = g.ID,
Name = g.Name
})
.ToList();
return groups;
}
public bool AddGroup(string name, string id)
{
try
{
var groupDao = new GroupDao
{
ID = int.Parse(id),
Name = name,
Users = new List<UserDao>()
};
_remoteDatabaseContext.Groups.Add(groupDao);
_remoteDatabaseContext.SaveChanges();
return true;
}
catch
{
return false;
}
}
public List<GroupDao> GetAllGroup()
{
return GetAllGroups();
}
public GroupDao GetGroupById(int groupID)
{
var group = _remoteDatabaseContext.Groups
.FirstOrDefault(x => x.ID == groupID);
if (group == null) return null;
return new GroupDao
{
ID = group.ID,
Name = group.Name,
Users = group.Users?.Select(u => new UserDao
{
UserID = u.UserID,
UserFIO = u.UserFIO,
GroupID = group.ID
}).ToList() ?? new List<UserDao>()
};
}
public bool RemoveGroupById(int groupID)
{
try
{
var group = _remoteDatabaseContext.Groups
.FirstOrDefault(x => x.ID == groupID);
if (group == null) return false;
if (group.Users != null && group.Users.Any())
{
return false;
}
_remoteDatabaseContext.Groups.Remove(group);
_remoteDatabaseContext.SaveChanges();
return true;
}
catch
{
return false;
}
}
public bool UpdateGroupById(int groupID, string name)
{
try
{
var group = _remoteDatabaseContext.Groups
.FirstOrDefault(x => x.ID == groupID);
if (group == null) return false;
group.Name = name;
_remoteDatabaseContext.SaveChanges();
return true;
}
catch
{
return false;
}
}
}
}