From 78776706a7525b34aa9abd1359532b35841b5937 Mon Sep 17 00:00:00 2001 From: 1eG0ist Date: Sun, 17 Nov 2024 00:45:57 +0300 Subject: [PATCH] add groups repos, service, migration, domain layer --- console_ui/GroupUI.cs | 24 +++++++++++++ console_ui/Program.cs | 27 +++++++++++++-- console_ui/console_ui.csproj | 7 +++- data/DAO/Group.cs | 2 +- data/DAO/User.cs | 2 +- ... 20241116153223_InitialCreate.Designer.cs} | 4 +-- ...ate.cs => 20241116153223_InitialCreate.cs} | 0 ... => RemoteDatabaseContextModelSnapshot.cs} | 4 +-- ...bseContext.cs => RemoteDatabaseContext.cs} | 6 ++-- data/Repository/IGroupRepository.cs | 15 ++++++++ data/Repository/LocalGroupRepository.cs | 29 ++++++++++++++++ data/Repository/SQLGroupRepository.cs | 34 +++++++++++++++++++ data/data.csproj | 8 ++++- domain/Class1.cs | 7 ++++ domain/Entity/GroupEntity.cs | 16 +++++++++ domain/Request/AddGroupRequest.cs | 14 ++++++++ domain/Service/GroupService.cs | 24 +++++++++++++ domain/UseCase/IGroupUseCase.cs | 14 ++++++++ domain/domain.csproj | 17 ++++++++++ semesterWork.sln | 10 ++++-- 20 files changed, 249 insertions(+), 15 deletions(-) create mode 100644 console_ui/GroupUI.cs rename data/Migrations/{20241115163726_InitialCreate.Designer.cs => 20241116153223_InitialCreate.Designer.cs} (96%) rename data/Migrations/{20241115163726_InitialCreate.cs => 20241116153223_InitialCreate.cs} (100%) rename data/Migrations/{RemoteDatabseContextModelSnapshot.cs => RemoteDatabaseContextModelSnapshot.cs} (95%) rename data/{RemoteDatabseContext.cs => RemoteDatabaseContext.cs} (84%) create mode 100644 data/Repository/IGroupRepository.cs create mode 100644 data/Repository/LocalGroupRepository.cs create mode 100644 data/Repository/SQLGroupRepository.cs create mode 100644 domain/Class1.cs create mode 100644 domain/Entity/GroupEntity.cs create mode 100644 domain/Request/AddGroupRequest.cs create mode 100644 domain/Service/GroupService.cs create mode 100644 domain/UseCase/IGroupUseCase.cs create mode 100644 domain/domain.csproj diff --git a/console_ui/GroupUI.cs b/console_ui/GroupUI.cs new file mode 100644 index 0000000..da738e6 --- /dev/null +++ b/console_ui/GroupUI.cs @@ -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() }); + } + } +} diff --git a/console_ui/Program.cs b/console_ui/Program.cs index 3751555..6e6011c 100644 --- a/console_ui/Program.cs +++ b/console_ui/Program.cs @@ -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); + + diff --git a/console_ui/console_ui.csproj b/console_ui/console_ui.csproj index 74abf5c..e77a655 100644 --- a/console_ui/console_ui.csproj +++ b/console_ui/console_ui.csproj @@ -2,9 +2,14 @@ Exe - net6.0 + net8.0 enable enable + + + + + diff --git a/data/DAO/Group.cs b/data/DAO/Group.cs index b7e3dab..10a1d0f 100644 --- a/data/DAO/Group.cs +++ b/data/DAO/Group.cs @@ -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; } diff --git a/data/DAO/User.cs b/data/DAO/User.cs index cf41fb9..385afa6 100644 --- a/data/DAO/User.cs +++ b/data/DAO/User.cs @@ -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; } diff --git a/data/Migrations/20241115163726_InitialCreate.Designer.cs b/data/Migrations/20241116153223_InitialCreate.Designer.cs similarity index 96% rename from data/Migrations/20241115163726_InitialCreate.Designer.cs rename to data/Migrations/20241116153223_InitialCreate.Designer.cs index a5fc96b..84632d7 100644 --- a/data/Migrations/20241115163726_InitialCreate.Designer.cs +++ b/data/Migrations/20241116153223_InitialCreate.Designer.cs @@ -11,8 +11,8 @@ using data; namespace data.Migrations { - [DbContext(typeof(RemoteDatabseContext))] - [Migration("20241115163726_InitialCreate")] + [DbContext(typeof(RemoteDatabaseContext))] + [Migration("20241116153223_InitialCreate")] partial class InitialCreate { /// diff --git a/data/Migrations/20241115163726_InitialCreate.cs b/data/Migrations/20241116153223_InitialCreate.cs similarity index 100% rename from data/Migrations/20241115163726_InitialCreate.cs rename to data/Migrations/20241116153223_InitialCreate.cs diff --git a/data/Migrations/RemoteDatabseContextModelSnapshot.cs b/data/Migrations/RemoteDatabaseContextModelSnapshot.cs similarity index 95% rename from data/Migrations/RemoteDatabseContextModelSnapshot.cs rename to data/Migrations/RemoteDatabaseContextModelSnapshot.cs index 948cdc9..c5848d9 100644 --- a/data/Migrations/RemoteDatabseContextModelSnapshot.cs +++ b/data/Migrations/RemoteDatabaseContextModelSnapshot.cs @@ -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) { diff --git a/data/RemoteDatabseContext.cs b/data/RemoteDatabaseContext.cs similarity index 84% rename from data/RemoteDatabseContext.cs rename to data/RemoteDatabaseContext.cs index e338471..b8ba21e 100644 --- a/data/RemoteDatabseContext.cs +++ b/data/RemoteDatabaseContext.cs @@ -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 groups { get; set; } + public DbSet groups { get; set; } - DbSet users { get; set; } + public DbSet users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/data/Repository/IGroupRepository.cs b/data/Repository/IGroupRepository.cs new file mode 100644 index 0000000..26a555e --- /dev/null +++ b/data/Repository/IGroupRepository.cs @@ -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 getAllGroup(); + public bool addGroup(GroupDAO group); + } +} diff --git a/data/Repository/LocalGroupRepository.cs b/data/Repository/LocalGroupRepository.cs new file mode 100644 index 0000000..e1ffd6e --- /dev/null +++ b/data/Repository/LocalGroupRepository.cs @@ -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 _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 new file mode 100644 index 0000000..b574e39 --- /dev/null +++ b/data/Repository/SQLGroupRepository.cs @@ -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 getAllGroup() + { + return dbContext.groups.ToList(); + } + } +} diff --git a/data/data.csproj b/data/data.csproj index b3a10f4..e70a846 100644 --- a/data/data.csproj +++ b/data/data.csproj @@ -7,7 +7,13 @@ - + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/domain/Class1.cs b/domain/Class1.cs new file mode 100644 index 0000000..855623e --- /dev/null +++ b/domain/Class1.cs @@ -0,0 +1,7 @@ +namespace domain +{ + public class Class1 + { + + } +} diff --git a/domain/Entity/GroupEntity.cs b/domain/Entity/GroupEntity.cs new file mode 100644 index 0000000..786c70b --- /dev/null +++ b/domain/Entity/GroupEntity.cs @@ -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; } + + } +} diff --git a/domain/Request/AddGroupRequest.cs b/domain/Request/AddGroupRequest.cs new file mode 100644 index 0000000..16e6e35 --- /dev/null +++ b/domain/Request/AddGroupRequest.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 AddGroupRequest + { + public string Name { get; set; } + + } +} diff --git a/domain/Service/GroupService.cs b/domain/Service/GroupService.cs new file mode 100644 index 0000000..8e06164 --- /dev/null +++ b/domain/Service/GroupService.cs @@ -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 }); + } + } +} diff --git a/domain/UseCase/IGroupUseCase.cs b/domain/UseCase/IGroupUseCase.cs new file mode 100644 index 0000000..f0f7096 --- /dev/null +++ b/domain/UseCase/IGroupUseCase.cs @@ -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); + } +} diff --git a/domain/domain.csproj b/domain/domain.csproj new file mode 100644 index 0000000..f2296ed --- /dev/null +++ b/domain/domain.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/semesterWork.sln b/semesterWork.sln index 5cc396a..0ad426e 100644 --- a/semesterWork.sln +++ b/semesterWork.sln @@ -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