This commit is contained in:
NikitaOnianov 2024-10-19 12:39:40 +03:00
parent fc811ba8d4
commit 685c86d2ec
2 changed files with 88 additions and 18 deletions

View File

@ -1,4 +1,6 @@
using System;
using Demo.Data.Repository;
using Demo.domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,5 +10,36 @@ namespace Demo.Domain.UseCase
{
internal class GroupUseCase
{
private readonly GroupRepositoryImpl _repositoryGroupImpl;
public GroupUseCase(GroupRepositoryImpl repositoryGroupImpl)
{
_repositoryGroupImpl = repositoryGroupImpl;
}
// Вывести все группы
public List<Group> GetAllGroups()
{
return _repositoryGroupImpl.GetAllGroups().Select(it => new Group { Id = it.Id, Name = it.Name }).ToList();
}
// Создать новую группу
public void AddGroup(string groupName)
{
var newId = _repositoryGroupImpl.GetAllGroups().Max(g => g.Id) + 1;
GroupLocalEntity newGroup = new GroupLocalEntity
{
Id = newId,
Name = groupName
};
_repositoryGroupImpl.AddGroup(newGroup);
}
// Изменить название группы по ID
public void UpdateGroup(int groupId, string newGroupName)
{
GroupLocalEntity existingGroup = _repositoryGroupImpl.GetAllGroups().FirstOrDefault(g => g.Id == groupId);
existingGroup.Name = newGroupName;
_repositoryGroupImpl.UpdateGroup(existingGroup);
}
}
}

View File

@ -20,27 +20,64 @@ namespace Demo.Domain.UseCase
}
public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups()
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList();
.Select(it => new Group { Id = it.Id, Name = it.Name }).ToList();
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers
.Join(_repositoryGroupImpl.GetAllGroups(),
user => user.GroupID,
group => group.Id,
(user, group) =>
new User { FIO = user.FIO,
new User
{
FIO = user.FIO,
Guid = user.Guid,
Group = new Group {Id = group.Id, Name = group.Name } }
Group = new Group { Id = group.Id, Name = group.Name }
}
).ToList();
public bool RemoveUserByGuid(Guid userGuid) {
// удалить пользователя по Giud
public bool RemoveUserByGuid(Guid userGuid)
{
return _repositoryUserImpl.RemoveUserByGuid(userGuid);
}
public User UpdateUser(User user) {
// Обновить пользователя по Guid
public User UpdateUser(User user)
{
UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity);
if (result == null) throw new Exception("");
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID);
if (group == null) throw new Exception("");
return new User { FIO = user.FIO, Guid = user.Guid, Group = group};
if (result == null)
{
throw new Exception("");
}
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID);
if (group == null)
{
throw new Exception("");
}
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
}
// Найти пользователя по Guid
public User FindUserByGuid(Guid userGuid)
{
var user = _repositoryUserImpl.GetAllUsers
.FirstOrDefault(u => u.Guid == userGuid);
if (user == null) throw new Exception("Пользователь не найден");
var group = _repositoryGroupImpl.GetAllGroups()
.FirstOrDefault(g => g.Id == user.GroupID);
return new User
{
FIO = user.FIO,
Guid = user.Guid,
Group = new Group { Id = group.Id, Name = group.Name }
};
}
//public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups().Select(it => new Group { Id = it.Id, Name = it.Name }).ToList();
}
}