поставьте 4 пожалуйста

This commit is contained in:
Diana 2024-10-18 20:54:42 +03:00
parent 6012168a59
commit 7b7b49d3e1
7 changed files with 169 additions and 6 deletions

View File

@ -11,5 +11,38 @@ namespace Demo.Data.Repository
public class GroupRepositoryImpl public class GroupRepositoryImpl
{ {
public List<GroupLocalEntity> GetAllGroups() => LocalStaticData.groups; public List<GroupLocalEntity> GetAllGroups() => LocalStaticData.groups;
public List<GroupLocalEntity> GetAllGroups
{ get; set; }
public bool RemoveGroupById(int id)
{
GroupLocalEntity? groupLocal = GetAllGroups
.Where(x => x.int == id).FirstOrDefault();
if (groupLocal == null) return false;
return GetAllGroup.Remove(groupLocal);
}
public GroupLocalEntity? GetGroupByGuid(int id)
{
GroupLocalEntity? groupLocal = GetAllGroups
.Where(x => x.int == id).FirstOrDefault();
if (groupLocal == null) return null;
return groupLocal;
}
public GroupLocalEntity? UpdateGroup(GroupLocalEntity groupUpdateLocalEnity)
{
GroupLocalEntity? groupLocal = GetAllGroups
.Where(x => x.int == groupUpdateLocalEnity.id).FirstOrDefault();
if (groupLocal == null) return null;
groupLocal.Name = groupUpdateLocalEnity.Name;
return groupLocal;
}
} }
} }

View File

@ -0,0 +1,58 @@
using Demo.Data.Repository;
using Demo.domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo.Domain.UseCase
{
public class GroupUseCase
{
private UserRepositoryImpl _repositoryUserImpl;
private GroupRepositoryImpl _repositoryGroupImpl;
public GroupUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl)
{
_repositoryUserImpl = repositoryImpl;
_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 bool RemoveGroupId(int id)
{
return _repositoryGroupImpl.RemoveGroupById(id);
}
public List<Group> GetGroupById1(int id)
{
return _repositoryGroupImpl.GetGroupByid(id);
}
public Group UpdateGroup(Group group)
{
GroupLocalEnity groupLocalEnity = new GroupLocalEnity { Id = group.Id, Name = group.name };
GroupLocalEnity? result = _repositoryGroupImpl.UpdateGroup(groupLocalEnity);
if (result == null) throw new Exception("");
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID);
if (group == null) throw new Exception("");
return new Group { Id = group.Id, Name = group.name };
}
}
}

View File

@ -34,6 +34,7 @@ namespace Demo.Domain.UseCase
public bool RemoveUserByGuid(Guid userGuid) { public bool RemoveUserByGuid(Guid userGuid) {
return _repositoryUserImpl.RemoveUserByGuid(userGuid); return _repositoryUserImpl.RemoveUserByGuid(userGuid);
} }
public User UpdateUser(User user) { public User UpdateUser(User user) {
UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid }; UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity); UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity);

View File

@ -5,6 +5,6 @@ using Demo.UI;
GroupRepositoryImpl groupRepositoryImpl = new GroupRepositoryImpl(); GroupRepositoryImpl groupRepositoryImpl = new GroupRepositoryImpl();
UserRepositoryImpl userRepositoryImpl = new UserRepositoryImpl(); UserRepositoryImpl userRepositoryImpl = new UserRepositoryImpl();
UserUseCase userUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl); UserUseCase UserUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl);
MainMenuUI mainMenuUI = new MainMenuUI(userUseCase); MainMenuUI mainMenuUI = new MainMenuUI(UserUseCase);

46
Demo/UI/GroupConsole.cs Normal file
View File

@ -0,0 +1,46 @@
using Demo.Domain.UseCase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo.UI
{
public class GroupConsoleUI
{
GroupUseCase _groupUseCase;
public UserConsoleUI(GroupUseCase groupUseCase)
{
_groupUseCase = groupUseCase;
}
public UpdateGroups()
{
string output = _groupUseCase.UpdateGroup("бла бла бла") ? "Группа обновлена" : "Группа не обновлена";
Console.WriteLine(output);
}
public void RemoveGroupByGuid(int id)
{
string output = _groupUseCase.RemoveGroupByGuid(id) ? "Группа удалена" : "Группа не удалена";
Console.WriteLine(output);
}
public void GetGroupById(int id)
{
Console.WriteLine(GetGroupById1(id));
}
public void DisplayAllGroup()
{
StringBuilder userOutput = new StringBuilder();
foreach (var user in _groupUseCase.GetAllGroup())
{
userOutput.AppendLine($"{group.Id}\t{group.Name}");
}
Console.WriteLine(userOutput);
}
}
}

View File

@ -1,6 +1,8 @@
using Demo.Domain.UseCase; using Demo.domain.Models;
using Demo.Domain.UseCase;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,7 +11,7 @@ namespace Demo.UI
{ {
public class MainMenuUI public class MainMenuUI
{ {
UserConsoleUI _userConsoleUI; UserConsoleUI _userConsoleUI;
public MainMenuUI(UserUseCase userUseCase) { public MainMenuUI(UserUseCase userUseCase) {
@ -18,6 +20,7 @@ namespace Demo.UI
} }
private void DisplayMenu() { private void DisplayMenu() {
while (true) while (true)
{ {
@ -25,7 +28,8 @@ namespace Demo.UI
{ {
case "1": _userConsoleUI.DisplayAllUsers(); break; case "1": _userConsoleUI.DisplayAllUsers(); break;
case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break;
case "3": _userConsoleUI.UpdateUser(Split(Console.ReadLine())); break;
case "4": _userConsoleUI.GetUserByGuid("614c0a23-5bd5-43ae-b48e-d5750afbc282"); break;
default: DisplayMenu(); default: DisplayMenu();
break; break;
} }

View File

@ -1,7 +1,10 @@
using Demo.Domain.UseCase; using Demo.domain.Models;
using Demo.Domain.UseCase;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -29,5 +32,23 @@ namespace Demo.UI
} }
Console.WriteLine(userOutput); Console.WriteLine(userOutput);
} }
public void UpdateUser(User user)
{
try
{
User updatedUser = _userUseCase.UpdateUser(user);
Console.WriteLine($"Пользователь обновлен: {updatedUser.FIO}, Группа: {updatedUser.Group.Name}");
}
catch (Exception ex)
{
Console.WriteLine($"Произошла ошибка: {ex.Message}");
}
}
public void GetUserByGuid(Guid userGuid) {
var output = _userUseCase.GetUserByGuid(userGuid);
Console.WriteLine(output);
}
} }
} }