Skip to content

Commit 95af97c

Browse files
Added Repository model and Databases initializer
1 parent 75457af commit 95af97c

File tree

8 files changed

+264
-262
lines changed

8 files changed

+264
-262
lines changed

Blog(Dirty)/Blog(Dirty).csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17+
<PackageReference Include="Dapper" Version="2.1.35" />
18+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
19+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
1720
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
1821
</ItemGroup>
1922

Blog(Dirty)/DatabaseInitializer.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Data.SqlClient;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Dapper;
9+
10+
namespace Blog_Dirty_
11+
{
12+
public interface IDbConnectionFactory
13+
{
14+
public Task<IDbConnection> CreateConnectionAsync();
15+
}
16+
public class SqlConnectionFactory : IDbConnectionFactory
17+
{
18+
private readonly string _connectionString;
19+
20+
public SqlConnectionFactory(string connectionString)
21+
{
22+
_connectionString = connectionString;
23+
}
24+
25+
public async Task<IDbConnection> CreateConnectionAsync()
26+
{
27+
var connection = new SqlConnection(_connectionString);
28+
await connection.OpenAsync();
29+
return connection;
30+
}
31+
}
32+
33+
public class DatabaseInitializer
34+
{
35+
private readonly IDbConnectionFactory _connectionFactory;
36+
37+
public DatabaseInitializer(IDbConnectionFactory connectionFactory)
38+
{
39+
_connectionFactory = connectionFactory;
40+
}
41+
42+
public async Task InitializeAsync()
43+
{
44+
using var connection = await _connectionFactory.CreateConnectionAsync();
45+
await connection.ExecuteAsync(@"CREATE TABLE IF NOT EXISTS Users
46+
ID CHAR(36) PRIMARY KEY,
47+
Username TEXT NOT NULL,
48+
Password TEXT NOT NULL");
49+
50+
await connection.ExecuteAsync(@"CREATE TABLE IF NOT EXISTS Posts
51+
ID CHAR(36) PRIMARY KEY,
52+
username TEXT NOT NULL,
53+
postName TEXT NOT NULL,
54+
postData TEXT NOT NULL");
55+
}
56+
}
57+
}

Blog(Dirty)/PostsManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public string[] searchForUserPosts(string username)
116116
{
117117
while (reader.Read())
118118
{
119-
string postData = reader[3].ToString();
119+
string postData = reader[2].ToString();
120+
postData += reader[3].ToString();
120121
result.Add(postData);
121122
}
122123
}

Blog(Dirty)/PostsRepository.cs

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -11,117 +11,11 @@ namespace Blog_Dirty_
1111
/// <summary>
1212
/// Class <c>PostsRepository</c> stores all data about posts in sql
1313
/// </summary>
14-
public class PostsRepository
14+
public class PostsRepository : RepositoryModel
1515
{
16-
/// <summary>
17-
/// Represents opening of the database
18-
/// </summary>
19-
private static SqlConnection connection = new SqlConnection();
20-
/// <summary>
21-
/// defines where the database is
22-
/// </summary>
23-
private static string databaseSource = "Data Source=(local)\\POSTSDATABASE;Initial Catalog=PostsRepository;Integrated Security=True";
24-
/// <summary>
25-
/// opens/creates database
26-
/// </summary>
27-
public void createDatabase()
16+
public PostsRepository() : base("Data Source=(local)\\POSTSDATABASE;Initial Catalog=PostsRepository;Integrated Security=True")
2817
{
29-
connection.ConnectionString = databaseSource;
30-
connection.Open();
31-
}
32-
33-
/// <summary>
34-
/// closes database
35-
/// </summary>
36-
public void closeDatabase()
37-
{
38-
connection.Close();
39-
}
40-
41-
42-
/// <summary>
43-
/// executes commands as adapter
44-
/// </summary>
45-
private static SqlDataAdapter adapter = new SqlDataAdapter();
46-
/// <summary>
47-
/// assigns commands to adapter and run database through it
48-
/// </summary>
49-
/// <param name="tblName">Name of the table which will be changed</param>
50-
/// <param name="databaseCommands">strubg of sql commands that will be executed</param>
51-
/// <returns>Count of database</returns>
52-
public int executeDataAdapter(DataTable tblName, string databaseCommands)
53-
{
54-
if (connection.State == 0)
55-
{
56-
createDatabase();
57-
}
58-
59-
adapter.SelectCommand.CommandText = databaseCommands;
60-
adapter.SelectCommand.CommandType = CommandType.Text;
61-
SqlCommandBuilder DbCommandBuilder = new SqlCommandBuilder(adapter);
62-
63-
string insert = DbCommandBuilder.GetInsertCommand().CommandText.ToString();
64-
string update = DbCommandBuilder.GetUpdateCommand().CommandText.ToString();
65-
string delete = DbCommandBuilder.GetDeleteCommand().CommandText.ToString();
66-
67-
return adapter.Update(tblName);
68-
}
69-
70-
/// <summary>
71-
/// basic sql command
72-
/// </summary>
73-
private static SqlCommand command = new SqlCommand();
74-
/// <summary>
75-
/// reads data from commands that you wrote
76-
/// </summary>
77-
/// <param name="query">commands that read</param>
78-
/// <param name="tblName">Name of the table which will be read</param>
79-
public void readDataFromCommands(string query, DataTable tblName)
80-
{
81-
command.Connection = connection;
82-
command.CommandText = query;
83-
command.CommandType = CommandType.Text;
84-
85-
adapter = new SqlDataAdapter(command);
86-
adapter.Fill(tblName);
87-
}
88-
89-
/// <summary>
90-
/// read data from commands without specifing table name
91-
/// </summary>
92-
/// <param name="query">commands that read</param>
93-
/// <returns></returns>
94-
public static SqlDataReader readDataFromStream(string query)
95-
{
96-
SqlDataReader reader;
97-
98-
command.Connection = connection;
99-
command.CommandText = query;
100-
command.CommandType = CommandType.Text;
10118

102-
reader = command.ExecuteReader();
103-
return reader;
104-
}
105-
106-
/// <summary>
107-
/// execute premade commands
108-
/// </summary>
109-
/// <param name="dbCommand"></param>
110-
/// <returns></returns>
111-
public int executeQuery(SqlCommand dbCommand)
112-
{
113-
dbCommand.Connection = connection;
114-
dbCommand.CommandType = CommandType.Text;
115-
116-
return dbCommand.ExecuteNonQuery();
117-
}
118-
/// <summary>
119-
/// returns connection to database
120-
/// </summary>
121-
/// <returns></returns>
122-
public SqlConnection getConnection()
123-
{
124-
return connection;
12519
}
12620
}
12721
}

Blog(Dirty)/Program.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,20 @@ namespace Blog_Dirty_
44
{
55
sealed class Program
66
{
7+
static void initDatabases(string connectionString)
8+
{
9+
var connectionFactory = new SqlConnectionFactory(connectionString);
10+
11+
var databaseInitializer = new DatabaseInitializer(connectionFactory);
12+
13+
databaseInitializer.InitializeAsync();
14+
}
715
static void Main(string[] args)
816
{
17+
initDatabases("Data Source=(local)\\POSTSDATABASE;Initial Catalog=PostsRepository;Integrated Security=True");
18+
initDatabases("Data Source=(local)\\USERDATABASE;Initial Catalog=UserRepository;Integrated Security=True");
19+
20+
921
Console.Write("Write a username: ");
1022
string username = Console.ReadLine();
1123
Console.Write("Write a password: ");
@@ -15,11 +27,6 @@ static void Main(string[] args)
1527

1628
UserManager userManager = new UserManager();
1729

18-
userManager.addUser(username, password);
19-
Console.WriteLine("User created sucessfully");
20-
userManager.removeUser(user);
21-
Console.WriteLine("User removed successfully");
22-
2330
BlogInterface blogInterface = new BlogInterface(user);
2431

2532
blogInterface.menu();

Blog(Dirty)/RepositoryModel.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,153 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data.SqlClient;
4+
using System.Data;
35
using System.Linq;
46
using System.Text;
57
using System.Threading.Tasks;
8+
using System.Runtime.CompilerServices;
69

710
namespace Blog_Dirty_
811
{
12+
/// <summary>
13+
/// Class <c>PostsRepository</c> stores all data about posts in sql
14+
/// </summary>
915
public class RepositoryModel
1016
{
17+
/// <summary>
18+
/// basic sql command
19+
/// </summary>
20+
private SqlCommand command;
21+
22+
/// <summary>
23+
/// Represents opening of the database
24+
/// </summary>
25+
private SqlConnection connection;
26+
27+
/// <summary>
28+
/// defines where the database is
29+
/// </summary>
30+
private string databaseSource;
31+
32+
/// <summary>
33+
/// executes commands as adapter
34+
/// </summary>
35+
private SqlDataAdapter adapter = new SqlDataAdapter();
36+
37+
/// <summary>
38+
/// Constructor
39+
/// </summary>
40+
/// <param name="databaseSource"></param>
41+
public RepositoryModel(string databaseSource)
42+
{
43+
this.databaseSource = databaseSource;
44+
this.connection = new SqlConnection();
45+
this.command = new SqlCommand();
46+
this.adapter = new SqlDataAdapter();
47+
}
48+
49+
/// <summary>
50+
/// Destructor
51+
/// </summary>
52+
~RepositoryModel()
53+
{
54+
closeDatabase();
55+
}
56+
57+
/// <summary>
58+
/// returns connection to database
59+
/// </summary>
60+
/// <returns></returns>
61+
public SqlConnection getConnection()
62+
{
63+
return connection;
64+
}
65+
66+
/// <summary>
67+
/// opens/creates database
68+
/// </summary>
69+
public void createDatabase()
70+
{
71+
connection.ConnectionString = databaseSource;
72+
connection.Open();
73+
}
74+
75+
/// <summary>
76+
/// closes database
77+
/// </summary>
78+
public void closeDatabase()
79+
{
80+
connection.Close();
81+
}
82+
83+
/// <summary>
84+
/// assigns commands to adapter and run database through it
85+
/// </summary>
86+
/// <param name="tblName">Name of the table which will be changed</param>
87+
/// <param name="databaseCommands">strubg of sql commands that will be executed</param>
88+
/// <returns>Count of database</returns>
89+
public int executeDataAdapter(DataTable tblName, string databaseCommands)
90+
{
91+
if (connection.State == 0)
92+
{
93+
createDatabase();
94+
}
95+
96+
adapter.SelectCommand.CommandText = databaseCommands;
97+
adapter.SelectCommand.CommandType = CommandType.Text;
98+
SqlCommandBuilder DbCommandBuilder = new SqlCommandBuilder(adapter);
99+
100+
string insert = DbCommandBuilder.GetInsertCommand().CommandText.ToString();
101+
string update = DbCommandBuilder.GetUpdateCommand().CommandText.ToString();
102+
string delete = DbCommandBuilder.GetDeleteCommand().CommandText.ToString();
103+
104+
closeDatabase();
105+
return adapter.Update(tblName);
106+
}
107+
108+
/// <summary>
109+
/// reads data from commands that you wrote
110+
/// </summary>
111+
/// <param name="query">commands that read</param>
112+
/// <param name="tblName">Name of the table which will be read</param>
113+
public void readDataFromCommands(string query, DataTable tblName)
114+
{
115+
command.Connection = connection;
116+
command.CommandText = query;
117+
command.CommandType = CommandType.Text;
118+
119+
adapter = new SqlDataAdapter(command);
120+
adapter.Fill(tblName);
121+
}
122+
123+
/// <summary>
124+
/// read data from commands without specifing table name
125+
/// </summary>
126+
/// <param name="query">commands that read</param>
127+
/// <returns></returns>
128+
private SqlDataReader readDataFromStream(string query)
129+
{
130+
SqlDataReader reader;
131+
132+
command.Connection = connection;
133+
command.CommandText = query;
134+
command.CommandType = CommandType.Text;
135+
136+
reader = command.ExecuteReader();
137+
return reader;
138+
}
139+
140+
/// <summary>
141+
/// execute premade commands
142+
/// </summary>
143+
/// <param name="dbCommand"></param>
144+
/// <returns></returns>
145+
public int executeQuery(SqlCommand dbCommand)
146+
{
147+
dbCommand.Connection = connection;
148+
dbCommand.CommandType = CommandType.Text;
149+
150+
return dbCommand.ExecuteNonQuery();
151+
}
11152
}
12153
}

0 commit comments

Comments
 (0)