add groups repos, service, migration, domain layer

This commit is contained in:
1eG0ist 2024-11-17 00:45:57 +03:00
parent d1b46637e2
commit 78776706a7
20 changed files with 249 additions and 15 deletions

24
console_ui/GroupUI.cs Normal file
View File

@ -0,0 +1,24 @@
using domain.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace console_ui
{
class GroupUI
{
private readonly GroupService _groupService;
public GroupUI(GroupService groupService)
{
_groupService = groupService;
}
public void AddGroup()
{
Console.WriteLine("Enter group name: ");
_groupService.AddGroup(new domain.Request.AddGroupRequest { Name = Console.ReadLine() });
}
}
}

View File

@ -1,2 +1,25 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using console_ui;
using data;
using data.DAO;
using data.Repository;
using domain.Service;
void printAllGroups(IGroupRepository groupRepository)
{
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);
group.AddGroup();
printAllGroups(groupRepository);

View File

@ -2,9 +2,14 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\data\data.csproj" />
<ProjectReference Include="..\domain\domain.csproj" />
</ItemGroup>
</Project>

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace data.DAO
{
internal class GroupDAO
public class GroupDAO
{
public int Id { get; set; }
public String Name { get; set; }

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace data.DAO
{
internal class UserDAO
public class UserDAO
{
public Guid Guid { get; set; }
public string Name { get; set; }

View File

@ -11,8 +11,8 @@ using data;
namespace data.Migrations
{
[DbContext(typeof(RemoteDatabseContext))]
[Migration("20241115163726_InitialCreate")]
[DbContext(typeof(RemoteDatabaseContext))]
[Migration("20241116153223_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />

View File

@ -10,8 +10,8 @@ using data;
namespace data.Migrations
{
[DbContext(typeof(RemoteDatabseContext))]
partial class RemoteDatabseContextModelSnapshot : ModelSnapshot
[DbContext(typeof(RemoteDatabaseContext))]
partial class RemoteDatabaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{

View File

@ -8,16 +8,16 @@ using System.Threading.Tasks;
namespace data
{
internal class RemoteDatabseContext: DbContext
public class RemoteDatabaseContext: DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Host=localhost;port=5432;Password=admin;Username=postgres;Database=semesterWork");
}
DbSet<GroupDAO> groups { get; set; }
public DbSet<GroupDAO> groups { get; set; }
DbSet<UserDAO> users { get; set; }
public DbSet<UserDAO> users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{

View File

@ -0,0 +1,15 @@
using data.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace data.Repository
{
public interface IGroupRepository
{
public IEnumerable<GroupDAO> getAllGroup();
public bool addGroup(GroupDAO group);
}
}

View File

@ -0,0 +1,29 @@
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

@ -0,0 +1,34 @@
using data.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace data.Repository
{
public class SQLGroupRepository : IGroupRepository
{
public readonly RemoteDatabaseContext dbContext;
public SQLGroupRepository(RemoteDatabaseContext remoteDatabaseContext)
{
dbContext = remoteDatabaseContext;
}
public bool addGroup(GroupDAO group)
{
try
{
dbContext.groups.Add(group);
return dbContext.SaveChanges() > 1;
}
catch (Exception ex) {
return false;
}
}
public IEnumerable<GroupDAO> getAllGroup()
{
return dbContext.groups.ToList();
}
}
}

View File

@ -6,6 +6,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Migrations\**" />
<EmbeddedResource Remove="Migrations\**" />
<None Remove="Migrations\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.10">

7
domain/Class1.cs Normal file
View File

@ -0,0 +1,7 @@
namespace domain
{
public class Class1
{
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace domain.Entity
{
internal class GroupEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}

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 AddGroupRequest
{
public string Name { get; set; }
}
}

View File

@ -0,0 +1,24 @@
using data.Repository;
using domain.Request;
using domain.UseCase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace domain.Service
{
public class GroupService : IGroupUseCase
{
private readonly IGroupRepository _groupRepository;
public GroupService(IGroupRepository groupRepository)
{
_groupRepository = groupRepository;
}
public void AddGroup(AddGroupRequest addGroupRequest)
{
_groupRepository.addGroup(new data.DAO.GroupDAO { Name = addGroupRequest.Name });
}
}
}

View File

@ -0,0 +1,14 @@
using domain.Request;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace domain.UseCase
{
public interface IGroupUseCase
{
public void AddGroup(AddGroupRequest addGroupRequest);
}
}

17
domain/domain.csproj Normal file
View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="Response\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\data\data.csproj" />
</ItemGroup>
</Project>

View File

@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "console_ui", "console_ui\console_ui.csproj", "{C66F54DD-3684-4790-87A7-A36355AB0BC7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "console_ui", "console_ui\console_ui.csproj", "{C66F54DD-3684-4790-87A7-A36355AB0BC7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "data", "data\data.csproj", "{28FE5F1E-5D2B-4750-AE24-A15BB8A5972D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "data", "data\data.csproj", "{28FE5F1E-5D2B-4750-AE24-A15BB8A5972D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "domain", "domain\domain.csproj", "{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -21,6 +23,10 @@ Global
{28FE5F1E-5D2B-4750-AE24-A15BB8A5972D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28FE5F1E-5D2B-4750-AE24-A15BB8A5972D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28FE5F1E-5D2B-4750-AE24-A15BB8A5972D}.Release|Any CPU.Build.0 = Release|Any CPU
{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE