UseCase
This commit is contained in:
parent
fc811ba8d4
commit
685c86d2ec
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,28 +19,65 @@ namespace Demo.Domain.UseCase
|
||||
_repositoryGroupImpl = repositoryGroupImpl;
|
||||
}
|
||||
|
||||
public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups()
|
||||
.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,
|
||||
Guid = user.Guid,
|
||||
Group = new Group {Id = group.Id, Name = group.Name } }
|
||||
).ToList();
|
||||
public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups()
|
||||
.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,
|
||||
Guid = user.Guid,
|
||||
Group = new Group { Id = group.Id, Name = group.Name }
|
||||
}
|
||||
).ToList();
|
||||
|
||||
public bool RemoveUserByGuid(Guid userGuid) {
|
||||
return _repositoryUserImpl.RemoveUserByGuid(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("");
|
||||
|
||||
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 (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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user