Skip to content

Commit 3dc7964

Browse files
Added \'build scripts\'
1 parent ce791ce commit 3dc7964

File tree

10 files changed

+369
-13
lines changed

10 files changed

+369
-13
lines changed

Blog(Dirty)/BlogInterface.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,27 @@
99

1010
namespace Blog_Dirty_
1111
{
12-
12+
/// <summary>
13+
/// Class <c>BlogInterface</c> creates a user interaction with program
14+
/// </summary>
1315
sealed class BlogInterface
1416
{
1517
public BlogInterface()
1618
{
1719

1820
}
1921

22+
/// <summary>
23+
/// This method changes text color to green and writes W: before writing
24+
/// </summary>
2025
private void writerMode()
2126
{
2227
Console.ForegroundColor = ConsoleColor.Green;
2328
Console.Write("W: ");
2429
}
25-
30+
/// <summary>
31+
/// This method changes text color to red and writes R: before writing
32+
/// </summary>
2633
private void readerMode()
2734
{
2835
Console.ForegroundColor = ConsoleColor.Red;
@@ -33,7 +40,9 @@ private void defaultMode()
3340
{
3441
Console.ForegroundColor = ConsoleColor.White;
3542
}
36-
43+
/// <summary>
44+
/// This method writes instructions for writer and saves content to database
45+
/// </summary>
3746
public void writeBlogPost()
3847
{
3948

@@ -64,7 +73,12 @@ public void writeBlogPost()
6473
}
6574
defaultMode();
6675
}
67-
76+
/// <summary>
77+
/// Outputs blog post
78+
/// </summary>
79+
/// <returns>
80+
/// string with usernames and posts which their owners wrote
81+
/// </returns>
6882
public void readBlogPost()
6983
{
7084
readerMode();
@@ -73,7 +87,9 @@ public void readBlogPost()
7387

7488
defaultMode();
7589
}
76-
90+
/// <summary>
91+
/// This method welcome our users
92+
/// </summary>
7793
public void menu()
7894
{
7995
ConsoleKeyInfo option;

Blog(Dirty)/Posts.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ namespace Blog_Dirty_
88
{
99
public class Posts
1010
{
11+
public Posts(string postName, string postData)
12+
{
13+
this.postName = postName;
14+
this.postData = postData;
15+
}
16+
1117
public string postName { get; set; }
1218
public string postData { get; set; }
19+
public string username { get; set; }
1320
}
1421
}

Blog(Dirty)/PostsManager.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
namespace Blog_Dirty_
1010
{
11-
sealed class PostsManager
11+
public class PostsManager
1212
{
1313
private PostsRepository _repository;
1414

15-
PostsManager()
15+
public PostsManager()
1616
{
1717
_repository = new PostsRepository();
1818
_repository.createDatabase();
@@ -26,7 +26,7 @@ public bool isPostExists(string postName)
2626
{
2727
try
2828
{
29-
SqlCommand checkIfExists = new SqlCommand("select count(*) from users where PostName= @postName"); //connection at end
29+
SqlCommand checkIfExists = new SqlCommand("select count(*) from posts where PostName= @postName"); //connection at end
3030
checkIfExists.Parameters.AddWithValue("postName", postName);
3131

3232
_repository.executeQuery(checkIfExists);
@@ -47,7 +47,7 @@ public void addPost(User user, string postName, string postData)
4747
{
4848
string username = user.UserName;
4949

50-
SqlCommand addToDatabase = new SqlCommand("insert into Users(username, postName, postData) values(@username, @postName, @postData)");
50+
SqlCommand addToDatabase = new SqlCommand("insert into posts(username, postName, postData) values(@username, @postName, @postData)");
5151

5252
addToDatabase.Parameters.AddWithValue("username", username);
5353
addToDatabase.Parameters.AddWithValue("postName", postName);
@@ -65,9 +65,11 @@ public void removePost(Posts posts)
6565
}
6666
else
6767
{
68-
SqlCommand deleteFromDatabase = new SqlCommand("DELETE from Posts Where PostName = '" + postName + "'");
68+
string username = posts.username;
69+
SqlCommand deleteFromDatabase = new SqlCommand("DELETE from Posts Where PostName = '" + postName + "'" + "AND username = " + "'username'");
6970

7071
deleteFromDatabase.Parameters.AddWithValue("PostName", postName);
72+
deleteFromDatabase.Parameters.AddWithValue("username", username);
7173

7274
_repository.executeQuery(deleteFromDatabase);
7375
}
@@ -76,9 +78,9 @@ public void removePost(Posts posts)
7678
public void searchForUserPosts(string username)
7779
{
7880
string query = "SELECT * from Posts Where username = '" + username + "'";
79-
SqlCommand searchInDatabase = new SqlCommand("SELECT * from Posts Where username = '" + username + "'");
81+
//SqlCommand searchInDatabase = new SqlCommand("SELECT * from Posts Where username = '" + username + "'");
8082

81-
searchInDatabase.Parameters.AddWithValue("username", username);
83+
//searchInDatabase.Parameters.AddWithValue("username", username);
8284

8385
DataTable dataTable = new DataTable();
8486

Blog(Dirty)/PostsRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Blog_Dirty_
1111
public class PostsRepository
1212
{
1313
private static SqlConnection connection = new SqlConnection();
14-
private static string databaseSource = "Data Source=(local)\\POSTSDATABASE;Initial Catalog=UserRepository;Integrated Security=True";
14+
private static string databaseSource = "Data Source=(local)\\POSTSDATABASE;Initial Catalog=PostsRepository;Integrated Security=True";
1515
public void createDatabase()
1616
{
1717
connection.ConnectionString = databaseSource;

Blog.Tests/PostsManagerTest.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Blog_Dirty_;
7+
8+
namespace Blog.Tests
9+
{
10+
[TestFixture]
11+
public class PostsManagerTest
12+
{
13+
private string postName = "Post";
14+
private string postData = "DataPost";
15+
private string username = "admin";
16+
private string password = "password";
17+
private User user;
18+
private PostsManager postsManager;
19+
private void createManager()
20+
{
21+
if (postsManager == null)
22+
{
23+
postsManager = new PostsManager();
24+
}
25+
else
26+
{
27+
return;
28+
}
29+
30+
}
31+
[Test]
32+
public void isPostExistsTest()
33+
{
34+
35+
string example = "Mike";
36+
createManager();
37+
38+
Assert.IsTrue(postsManager.isPostExists(example));
39+
}
40+
41+
[Test]
42+
public void addPostTest()
43+
{
44+
createManager();
45+
46+
user = new User(username, password);
47+
48+
postsManager.addPost(user, postName, postData);
49+
50+
Assert.IsFalse(postsManager.isPostExists(postName));
51+
}
52+
53+
[Test]
54+
public void removePostTest()
55+
{
56+
createManager();
57+
User user = new User(username, password);
58+
59+
Posts posts = new Posts(postName, postData);
60+
61+
62+
Assert.Throws<Exception>(() => postsManager.removePost(posts));
63+
64+
Assert.IsFalse(postsManager.isPostExists(postName));
65+
}
66+
}
67+
}

Blog.Tests/PostsTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Blog_Dirty_;
7+
8+
namespace Blog.Tests
9+
{
10+
[TestFixture]
11+
public class PostsTest
12+
{
13+
[Test]
14+
public void postNameTest()
15+
{
16+
string postName = "Jhon";
17+
string postData = "";
18+
Posts posts = new Posts(postName, postData);
19+
20+
Assert.Pass(posts.postName = postName);
21+
}
22+
[Test]
23+
public void postDataTest()
24+
{
25+
string postName = "";
26+
string postData = "Data";
27+
Posts posts = new Posts(postName, postData);
28+
29+
Assert.Pass(posts.postData = postData);
30+
}
31+
}
32+
}

scripts/Setup.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os
2+
import subprocess
3+
4+
from SetupPython import PythonConfiguration as PythonRequirements
5+
6+
# Make sure everything we need for the setup is installed
7+
PythonRequirements.Validate()
8+
9+
print("\nSetup completed!")

scripts/SetupPython.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
import subprocess
3+
import importlib.util as importlib_util
4+
5+
class PythonConfiguration:
6+
@classmethod
7+
def Validate(cls):
8+
if not cls.__ValidatePython():
9+
return # cannot validate further
10+
11+
for packageName in ["requests"]:
12+
if not cls.__ValidatePackage(packageName):
13+
return # cannot validate further
14+
15+
@classmethod
16+
def __ValidatePython(cls, versionMajor = 3, versionMinor = 3):
17+
if sys.version is not None:
18+
print("Python version {0:d}.{1:d}.{2:d} detected.".format( \
19+
sys.version_info.major, sys.version_info.minor, sys.version_info.micro))
20+
if sys.version_info.major < versionMajor or (sys.version_info.major == versionMajor and sys.version_info.minor < versionMinor):
21+
print("Python version too low, expected version {0:d}.{1:d} or higher.".format( \
22+
versionMajor, versionMinor))
23+
return False
24+
return True
25+
26+
@classmethod
27+
def __ValidatePackage(cls, packageName):
28+
if importlib_util.find_spec(packageName) is None:
29+
return cls.__InstallPackage(packageName)
30+
return True
31+
32+
@classmethod
33+
def __InstallPackage(cls, packageName):
34+
permissionGranted = False
35+
while not permissionGranted:
36+
reply = str(input("Would you like to install Python package '{0:s}'? [Y/N]: ".format(packageName))).lower().strip()[:1]
37+
if reply == 'n':
38+
return False
39+
permissionGranted = (reply == 'y')
40+
41+
print(f"Installing {packageName} module...")
42+
subprocess.check_call(['python', '-m', 'pip', 'install', packageName])
43+
44+
return cls.__ValidatePackage(packageName)
45+
46+
if __name__ == "__main__":
47+
PythonConfiguration.Validate()

scripts/SetupSQL.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os.path
2+
import sys
3+
from pathlib import Path
4+
5+
class SQLConfiguration:
6+
SQLVersion = ""
7+
SQLExeUrl = ""
8+
SQLLicenseUrl = ""
9+
SQLDirectory = "./database"
10+
11+
@classmethod
12+
def Validate(cls):
13+
if (not cls.CheckIfSQLInstalled()):
14+
print("SQL is not installed")
15+
return False
16+
print(f"Correct SQL located at {os.path.abspath(cls.SQLDirectory)}")
17+
return True
18+
19+
@classmethod
20+
def CheckIfSQLInstalled(cls):
21+
sqlExe = Path(f"{cls.SQLDirectory}")
22+
if (not sqlExe.exists()):
23+
return cls.InstallSQL()
24+
25+
return True
26+
27+
@classmethod
28+
def InstallSQL(cls):
29+
permissionGranted = False
30+
while not permissionGranted:
31+
reply = str(input("SQL not found. Would you like to download SQL {0:s}? [Y/N]: ".format(cls.SQLVersion))).lower().strip()[:1]
32+
if reply == 'n':
33+
return False
34+
permissionGranted = (reply == 'y')
35+
36+
sqlPath = f"{cls.SQLDirectory}"
37+
sqlURLPath = "https://download.microsoft.com/download/5/E/9/5E9B18CC-8FD5-467E-B5BF-BADE39C51F73/SQLServer2017-SSEI-Expr.exe"
38+
print("Downloading {0:s} to {1:s}".format(sqlURLPath, sqlPath))
39+
40+
#subprocess.call(["your code here", "nopause"])
41+
#./SQL ENU INSTALLPATH SQLDirectory
42+
return True
43+
44+
#.\SQLServer2017-SSEI-Expr.exe /ENU /INSTALLPATH="C:\Users\ASUS\Desktop\test"
45+
#Winget install "Microsoft SQL Server 2017 Express" "Microsoft SQL Server Management"
46+
#https://download.microsoft.com/download/5/E/9/5E9B18CC-8FD5-467E-B5BF-BADE39C51F73/SQLServer2017-SSEI-Expr.exe
47+
#https://aka.ms/ssmsfullsetup
48+
#.\SSMS-Setup-ENU.exe /Install /SSMSInstallRoot="C:\Users\ASUS\Desktop\test"
49+
#.\DCEXEC.EXE -?

0 commit comments

Comments
 (0)