implementation/add-data-classes #1

Merged
VsevolodKozlov123 merged 10 commits from implementation/add-data-classes into master 2024-12-12 16:11:43 +00:00
10 changed files with 103 additions and 41 deletions
Showing only changes of commit 189d95daa6 - Show all commits

View File

@ -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<AddStudentRequest> addStudentRequests = new List<AddStudentRequest>()
{
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);
}
}
}

View File

@ -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<RemoteDatabaseContext>()
.AddSingleton<IGroupRepository, SQLGroupRepository>()
.AddSingleton<IGroupUseCase, GroupService>()
.AddSingleton<GroupUI>();
printAllGroups(groupRepository);
var serviceProvider = serviceCollection.BuildServiceProvider();
var groupUI = serviceProvider.GetService<GroupUI>();
groupUI?.AddGroup();

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@ -7,6 +7,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\data\data.csproj" />
<ProjectReference Include="..\domain\domain.csproj" />

View File

@ -11,5 +11,6 @@ namespace data.Repository
{
public IEnumerable<GroupDAO> getAllGroup();
public bool addGroup(GroupDAO group);
public bool addGroupWithStudents(GroupDAO group, IEnumerable<UserDAO> userDAOs);
}
}

View File

@ -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<GroupDAO> _groups = new List<GroupDAO>()
{
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<GroupDAO> getAllGroup()
{
return _groups;
}
}
}

View File

@ -36,5 +36,27 @@ namespace data.Repository
return new List<GroupDAO>();
}
}
public bool addGroupWithStudents(GroupDAO groupDAO, IEnumerable<UserDAO> 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;
}
}
}

View File

@ -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<AddStudentRequest> addStudentRequests { get; set; }
}
}

View File

@ -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; }
}
}

View File

@ -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<UserDAO> users = addGroupWithStudents.addStudentRequests
.Select(it => new UserDAO { Name = it.StudentName })
.ToList();
_groupRepository.addGroupWithStudents(groupDAO, users);
}
}
}

View File

@ -10,5 +10,6 @@ namespace domain.UseCase
public interface IGroupUseCase
{
public void AddGroup(AddGroupRequest addGroupRequest);
public void AddGroupWithStudents(AddGroupWithStudentsRequest addGroupWithStudents);
}
}