diff --git a/console_ui/GroupUI.cs b/console_ui/GroupUI.cs index da738e6..b3d948c 100644 --- a/console_ui/GroupUI.cs +++ b/console_ui/GroupUI.cs @@ -1,4 +1,6 @@ -using domain.Service; +using domain.Request; +using domain.Service; +using domain.UseCase; using System; using System.Collections.Generic; using System.Linq; @@ -9,8 +11,8 @@ namespace console_ui { class GroupUI { - private readonly GroupService _groupService; - public GroupUI(GroupService groupService) + private readonly IGroupUseCase _groupService; + public GroupUI(IGroupUseCase groupService) { _groupService = groupService; } @@ -20,5 +22,24 @@ namespace console_ui Console.WriteLine("Enter group name: "); _groupService.AddGroup(new domain.Request.AddGroupRequest { Name = Console.ReadLine() }); } + + public void AddGroupWithStudents() + { + Console.WriteLine("Enter group name: "); + AddGroupRequest addGroupRequest = new AddGroupRequest { Name = Console.ReadLine() }; + List addStudentRequests = new List() + { + new AddStudentRequest{ StudentName = "StudentName1"}, + new AddStudentRequest{ StudentName = "StudentName2"}, + new AddStudentRequest{ StudentName = "StudentName3"}, + new AddStudentRequest{ StudentName = "StudentName4"}, + }; + AddGroupWithStudentsRequest addGroupWithStudents = new AddGroupWithStudentsRequest + { + addGroupRequest = addGroupRequest, + addStudentRequests = addStudentRequests + }; + _groupService.AddGroupWithStudents(addGroupWithStudents); + } } } diff --git a/console_ui/Program.cs b/console_ui/Program.cs index 6e6011c..ae46a44 100644 --- a/console_ui/Program.cs +++ b/console_ui/Program.cs @@ -3,23 +3,28 @@ using data; using data.DAO; using data.Repository; using domain.Service; +using domain.UseCase; +using Microsoft.Extensions.DependencyInjection; void printAllGroups(IGroupRepository groupRepository) { + Console.WriteLine("Groups:"); foreach (var item in groupRepository.getAllGroup()) { Console.WriteLine(item.Name); } } -RemoteDatabaseContext remoteDatabaseContext = new RemoteDatabaseContext(); -SQLGroupRepository groupRepository = new SQLGroupRepository(remoteDatabaseContext); -LocalGroupRepository localGroupRepository = new LocalGroupRepository(); -GroupService groupService = new GroupService(groupRepository); -GroupUI group = new GroupUI(groupService); +IServiceCollection serviceCollection = new ServiceCollection(); -group.AddGroup(); +serviceCollection + .AddDbContext() + .AddSingleton() + .AddSingleton() + .AddSingleton(); -printAllGroups(groupRepository); +var serviceProvider = serviceCollection.BuildServiceProvider(); +var groupUI = serviceProvider.GetService(); +groupUI?.AddGroup(); diff --git a/console_ui/console_ui.csproj b/console_ui/console_ui.csproj index e77a655..8a0aa43 100644 --- a/console_ui/console_ui.csproj +++ b/console_ui/console_ui.csproj @@ -1,4 +1,4 @@ - + Exe @@ -7,6 +7,10 @@ enable + + + + diff --git a/data/Repository/IGroupRepository.cs b/data/Repository/IGroupRepository.cs index 26a555e..5be29fb 100644 --- a/data/Repository/IGroupRepository.cs +++ b/data/Repository/IGroupRepository.cs @@ -11,5 +11,6 @@ namespace data.Repository { public IEnumerable getAllGroup(); public bool addGroup(GroupDAO group); + public bool addGroupWithStudents(GroupDAO group, IEnumerable userDAOs); } } diff --git a/data/Repository/LocalGroupRepository.cs b/data/Repository/LocalGroupRepository.cs deleted file mode 100644 index e1ffd6e..0000000 --- a/data/Repository/LocalGroupRepository.cs +++ /dev/null @@ -1,29 +0,0 @@ -using data.DAO; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace data.Repository -{ - public class LocalGroupRepository : IGroupRepository - { - private IEnumerable _groups = new List() - { - new GroupDAO{ Id = 1, Name = "g1" }, - new GroupDAO{ Id = 2, Name = "g2" }, - new GroupDAO{ Id = 3, Name = "g3" }, - new GroupDAO{ Id = 4, Name = "g4" } - }; - public bool addGroup(GroupDAO group) - { - throw new NotImplementedException(); - } - - public IEnumerable getAllGroup() - { - return _groups; - } - } -} diff --git a/data/Repository/SQLGroupRepository.cs b/data/Repository/SQLGroupRepository.cs index c7e2fea..5b55eef 100644 --- a/data/Repository/SQLGroupRepository.cs +++ b/data/Repository/SQLGroupRepository.cs @@ -36,5 +36,27 @@ namespace data.Repository return new List(); } } + + public bool addGroupWithStudents(GroupDAO groupDAO, IEnumerable userDAOs) + { + using var transaction = dbContext.Database.BeginTransaction(); + try + { + dbContext.groups.Add(groupDAO); + dbContext.SaveChanges(); + foreach (var item in userDAOs) + { + item.Group = groupDAO; + dbContext.users.Add(item); + } + dbContext.SaveChanges(); + transaction.Commit(); + return true; + } catch + { + transaction.Rollback(); + } + return false; + } } } diff --git a/domain/Request/AddGroupWithStudentsRequest.cs b/domain/Request/AddGroupWithStudentsRequest.cs new file mode 100644 index 0000000..394a5b6 --- /dev/null +++ b/domain/Request/AddGroupWithStudentsRequest.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace domain.Request +{ + public class AddGroupWithStudentsRequest + { + public AddGroupRequest addGroupRequest { get; set; } + public IEnumerable addStudentRequests { get; set; } + } +} diff --git a/domain/Request/AddStudentRequest.cs b/domain/Request/AddStudentRequest.cs new file mode 100644 index 0000000..e75c61a --- /dev/null +++ b/domain/Request/AddStudentRequest.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace domain.Request +{ + public class AddStudentRequest + { + public string StudentName { get; set; } + } +} diff --git a/domain/Service/GroupService.cs b/domain/Service/GroupService.cs index 8e06164..c6b0600 100644 --- a/domain/Service/GroupService.cs +++ b/domain/Service/GroupService.cs @@ -1,4 +1,5 @@ -using data.Repository; +using data.DAO; +using data.Repository; using domain.Request; using domain.UseCase; using System; @@ -20,5 +21,14 @@ namespace domain.Service { _groupRepository.addGroup(new data.DAO.GroupDAO { Name = addGroupRequest.Name }); } + + public void AddGroupWithStudents(AddGroupWithStudentsRequest addGroupWithStudents) + { + GroupDAO groupDAO = new GroupDAO() { Name = addGroupWithStudents.addGroupRequest.Name }; + List users = addGroupWithStudents.addStudentRequests + .Select(it => new UserDAO { Name = it.StudentName }) + .ToList(); + _groupRepository.addGroupWithStudents(groupDAO, users); + } } } diff --git a/domain/UseCase/IGroupUseCase.cs b/domain/UseCase/IGroupUseCase.cs index f0f7096..e4811ba 100644 --- a/domain/UseCase/IGroupUseCase.cs +++ b/domain/UseCase/IGroupUseCase.cs @@ -10,5 +10,6 @@ namespace domain.UseCase public interface IGroupUseCase { public void AddGroup(AddGroupRequest addGroupRequest); + public void AddGroupWithStudents(AddGroupWithStudentsRequest addGroupWithStudents); } }