Compare commits

..

3 Commits

Author SHA1 Message Date
77051bcf84 add error catching 2024-11-17 00:56:50 +03:00
78776706a7 add groups repos, service, migration, domain layer 2024-11-17 00:45:57 +03:00
d1b46637e2 add dao 2024-11-16 15:34:29 +03:00
21 changed files with 530 additions and 9 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 using console_ui;
Console.WriteLine("Hello, World!"); 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> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\data\data.csproj" />
<ProjectReference Include="..\domain\domain.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -6,9 +6,11 @@ using System.Threading.Tasks;
namespace data.DAO namespace data.DAO
{ {
internal class GroupDAO public class GroupDAO
{ {
public int Id { get; set; } public int Id { get; set; }
public String Name { get; set; } public String Name { get; set; }
public virtual IEnumerable<UserDAO> Users { get; set; }
} }
} }

12
data/DAO/Presence.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace data.DAO
{
internal class Presence
{
}
}

View File

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

View File

@ -0,0 +1,83 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using data;
#nullable disable
namespace data.Migrations
{
[DbContext(typeof(RemoteDatabaseContext))]
[Migration("20241116153223_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("data.DAO.GroupDAO", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("groups");
});
modelBuilder.Entity("data.DAO.UserDAO", b =>
{
b.Property<Guid>("Guid")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<int>("GroupId")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.HasKey("Guid");
b.HasIndex("GroupId");
b.ToTable("users");
});
modelBuilder.Entity("data.DAO.UserDAO", b =>
{
b.HasOne("data.DAO.GroupDAO", "Group")
.WithMany("Users")
.HasForeignKey("GroupId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Group");
});
modelBuilder.Entity("data.DAO.GroupDAO", b =>
{
b.Navigation("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,63 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace data.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "groups",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_groups", x => x.Id);
});
migrationBuilder.CreateTable(
name: "users",
columns: table => new
{
Guid = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
GroupId = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_users", x => x.Guid);
table.ForeignKey(
name: "FK_users_groups_GroupId",
column: x => x.GroupId,
principalTable: "groups",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_users_GroupId",
table: "users",
column: "GroupId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "users");
migrationBuilder.DropTable(
name: "groups");
}
}
}

View File

@ -0,0 +1,80 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using data;
#nullable disable
namespace data.Migrations
{
[DbContext(typeof(RemoteDatabaseContext))]
partial class RemoteDatabaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("data.DAO.GroupDAO", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("groups");
});
modelBuilder.Entity("data.DAO.UserDAO", b =>
{
b.Property<Guid>("Guid")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<int>("GroupId")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.HasKey("Guid");
b.HasIndex("GroupId");
b.ToTable("users");
});
modelBuilder.Entity("data.DAO.UserDAO", b =>
{
b.HasOne("data.DAO.GroupDAO", "Group")
.WithMany("Users")
.HasForeignKey("GroupId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Group");
});
modelBuilder.Entity("data.DAO.GroupDAO", b =>
{
b.Navigation("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,32 @@
using data.DAO;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace data
{
public class RemoteDatabaseContext: DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Host=localhost;port=5432;Password=admin;Username=postgres;Database=semesterWork");
}
public DbSet<GroupDAO> groups { get; set; }
public DbSet<UserDAO> users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserDAO>().HasKey(it => it.Guid);
modelBuilder.Entity<GroupDAO>().HasKey(it => it.Id);
modelBuilder.Entity<UserDAO>().Property(it => it.Guid).ValueGeneratedOnAdd();
modelBuilder.Entity<GroupDAO>().Property(it => it.Id).ValueGeneratedOnAdd();
}
}
}

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,40 @@
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()
{
try
{
return dbContext.groups.ToList();
}
catch (Exception ex) {
return new List<GroupDAO>();
}
}
}
}

View File

@ -1,9 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </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">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.10" />
</ItemGroup>
</Project> </Project>

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 # Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59 VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1 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 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{28FE5F1E-5D2B-4750-AE24-A15BB8A5972D}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE