-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DARKMILK-6: Реализовать EF и подключиться к бд
- Loading branch information
Showing
14 changed files
with
159 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace ServerTCP | ||
{ | ||
public static class ConsoleExtension | ||
{ | ||
public static void WriteLineColor(string text, ConsoleColor foregroundColor = System.ConsoleColor.Gray, ConsoleColor backgroundColor = System.ConsoleColor.Black) | ||
{ | ||
Console.ForegroundColor = foregroundColor; | ||
Console.BackgroundColor = backgroundColor; | ||
Console.WriteLine(text); | ||
Console.ResetColor(); | ||
} | ||
|
||
public static void WriteColor(string text, ConsoleColor foregroundColor = System.ConsoleColor.Gray, ConsoleColor backgroundColor = System.ConsoleColor.Black) | ||
{ | ||
Console.ForegroundColor = foregroundColor; | ||
Console.BackgroundColor = backgroundColor; | ||
Console.Write(text); | ||
Console.ResetColor(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Threading.Channels; | ||
using ServerTCP.Models; | ||
using ServerTCP.Models.Data; | ||
|
||
namespace ServerTCP.DataBase | ||
{ | ||
internal class DataBaseCommands | ||
{ | ||
public static void Insert(object table, MessageHeader.MessageType messageType) | ||
{ | ||
using (var db = new ApplicationContext()) | ||
{ | ||
if (db.Database.CanConnect()) | ||
ConsoleExtension.WriteLineColor("База данных доступна.", ConsoleColor.DarkGreen); | ||
else | ||
ConsoleExtension.WriteLineColor("База данных не доступна!", ConsoleColor.DarkRed); | ||
|
||
switch (messageType) | ||
{ | ||
case MessageHeader.MessageType.Registration: | ||
if (table is User user) | ||
db.users.Add(user); | ||
break; | ||
} | ||
db.SaveChanges(); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Options; | ||
using System.Collections.Generic; | ||
|
||
namespace ServerTCP.Models.Data | ||
{ | ||
internal class ApplicationContext : DbContext | ||
{ | ||
public DbSet<User> users { get; set; } | ||
|
||
public ApplicationContext() | ||
{ | ||
//Database.EnsureCreated(); // гарантирует, что БД существует или создаст её и таблицы там | ||
} | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) // подключение к бд | ||
{ | ||
optionsBuilder.UseNpgsql("Host=81.31.246.203;Port=5432;Database=DarkMilkBD;Username=gen_user;Password=lY8\\b@slc^g\\xJ"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace ServerTCP.Models | ||
{ | ||
internal class User | ||
{ | ||
public int id { get; set; } | ||
public string username { get; set; } | ||
public string login { get; set; } | ||
public string email { get; set; } | ||
public string password { get; set; } | ||
|
||
public User() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
using ServerTCP.DataBase; | ||
using ServerTCP.Models; | ||
|
||
namespace ServerTCP | ||
{ | ||
|
@@ -34,20 +35,49 @@ static void Main(string[] args) | |
size = listener.Receive(buffer); // получение данных, количество, значение | ||
//buffer = buffer.Where(x => x != 0).ToArray(); | ||
var header = MessageHeader.FromArray(buffer); | ||
|
||
MessageHeader headerRequest = null; | ||
byte[] headerRequestBytes; | ||
switch (header.Type) | ||
{ | ||
case MessageHeader.MessageType.Check: | ||
var headerRequest = new MessageHeader("1", MessageHeader.MessageType.Check, 1); | ||
byte[] headerBytes = headerRequest.MessageToArray(); | ||
listener.Send(headerBytes); | ||
headerRequest = new MessageHeader("1", MessageHeader.MessageType.Check, 1); | ||
headerRequestBytes = headerRequest.MessageToArray(); | ||
listener.Send(headerRequestBytes); | ||
break; | ||
case MessageHeader.MessageType.Session: | ||
break; | ||
case MessageHeader.MessageType.Token: | ||
break; | ||
case MessageHeader.MessageType.Registration: | ||
var user = new User | ||
{ | ||
id = 1, login = "Gustaf", email = "[email protected]", username = "Gustavo", password = "sjafhfjhlkj12pje12j31kl23j1l123j" | ||
}; | ||
DataBaseCommands.Insert(user, MessageHeader.MessageType.Registration); | ||
break; | ||
case MessageHeader.MessageType.Log: | ||
break; | ||
case MessageHeader.MessageType.File: | ||
break; | ||
case MessageHeader.MessageType.Photo: | ||
break; | ||
case MessageHeader.MessageType.Data: | ||
break; | ||
case MessageHeader.MessageType.Title: | ||
break; | ||
case MessageHeader.MessageType.TitleLoading: | ||
headerRequest = new MessageHeader("1", MessageHeader.MessageType.TitleLoading, 1); | ||
headerRequestBytes = headerRequest.MessageToArray(); | ||
listener.Send(headerRequestBytes); | ||
break; | ||
|
||
//case MessageHeader<string>.MessageType.Title: | ||
// string messageRequest = "Привет, пупс!"; | ||
// var header1 = new MessageHeader<string>(messageRequest, MessageHeader<string>.MessageType.Check, Encoding.UTF8.GetBytes(messageRequest).Length); | ||
// byte[] headerBytes = header1.MessageToArray(); | ||
// listener.Send(headerBytes); | ||
// break; | ||
//case MessageHeader<string>.MessageType.Title: | ||
// string messageRequest = "Привет, пупс!"; | ||
// var header1 = new MessageHeader<string>(messageRequest, MessageHeader<string>.MessageType.Check, Encoding.UTF8.GetBytes(messageRequest).Length); | ||
// byte[] headerBytes = header1.MessageToArray(); | ||
// listener.Send(headerBytes); | ||
// break; | ||
} | ||
//data.Append(Encoding.UTF8.GetString(buffer, 0, size)); // сохраняем, добавляем данные. Данные передаются в кодированном формате, будем использовать кодировку UTF8, раскодируем байты | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |