Skip to content

Commit ce791ce

Browse files
Added posts
1 parent ef605a1 commit ce791ce

14 files changed

+699
-52
lines changed

Blog(Dirty)/BlogInterface.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.CompilerServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
9+
10+
namespace Blog_Dirty_
11+
{
12+
13+
sealed class BlogInterface
14+
{
15+
public BlogInterface()
16+
{
17+
18+
}
19+
20+
private void writerMode()
21+
{
22+
Console.ForegroundColor = ConsoleColor.Green;
23+
Console.Write("W: ");
24+
}
25+
26+
private void readerMode()
27+
{
28+
Console.ForegroundColor = ConsoleColor.Red;
29+
Console.Write("R: ");
30+
}
31+
32+
private void defaultMode()
33+
{
34+
Console.ForegroundColor = ConsoleColor.White;
35+
}
36+
37+
public void writeBlogPost()
38+
{
39+
40+
string blogName;
41+
bool exit = false;
42+
string blogData;
43+
44+
Console.WriteLine();
45+
46+
Console.WriteLine("To end writing type exit in new line");
47+
Console.Write("Write the name of your new blog: ");
48+
blogName = Console.ReadLine();
49+
50+
Console.Clear();
51+
52+
Console.WriteLine("\t" + blogName + "\n\n\n\n\n");
53+
54+
while (!exit)
55+
{
56+
writerMode();
57+
blogData = Console.ReadLine();
58+
59+
if (blogData == "exit")
60+
{
61+
exit = true;
62+
}
63+
64+
}
65+
defaultMode();
66+
}
67+
68+
public void readBlogPost()
69+
{
70+
readerMode();
71+
72+
73+
74+
defaultMode();
75+
}
76+
77+
public void menu()
78+
{
79+
ConsoleKeyInfo option;
80+
81+
Console.WriteLine("Welcome in BlogApp!!!\n\n\n");
82+
83+
Console.WriteLine("Select the option from below");
84+
85+
Console.WriteLine("a. Write a blog post");
86+
Console.WriteLine("b. Read your blog posts");
87+
Console.WriteLine("c. Read specified post");
88+
Console.Write("Your option: ");
89+
90+
option = Console.ReadKey();
91+
92+
if (ConsoleKey.A == option.Key)
93+
{
94+
writeBlogPost();
95+
}
96+
else if(ConsoleKey.B == option.Key)
97+
{
98+
99+
}
100+
else if(ConsoleKey.C == option.Key)
101+
{
102+
103+
}
104+
}
105+
}
106+
}

Blog(Dirty)/Posts.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Blog_Dirty_
8+
{
9+
public class Posts
10+
{
11+
public string postName { get; set; }
12+
public string postData { get; set; }
13+
}
14+
}

Blog(Dirty)/PostsManager.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Data.SqlClient;
7+
using System.Data;
8+
9+
namespace Blog_Dirty_
10+
{
11+
sealed class PostsManager
12+
{
13+
private PostsRepository _repository;
14+
15+
PostsManager()
16+
{
17+
_repository = new PostsRepository();
18+
_repository.createDatabase();
19+
}
20+
~PostsManager()
21+
{
22+
_repository.closeDatabase();
23+
}
24+
25+
public bool isPostExists(string postName)
26+
{
27+
try
28+
{
29+
SqlCommand checkIfExists = new SqlCommand("select count(*) from users where PostName= @postName"); //connection at end
30+
checkIfExists.Parameters.AddWithValue("postName", postName);
31+
32+
_repository.executeQuery(checkIfExists);
33+
34+
object result = checkIfExists.ExecuteScalar();
35+
if ((int)result == 0)
36+
{
37+
return true;
38+
}
39+
return false;
40+
}
41+
catch (Exception ex)
42+
{
43+
return true;
44+
}
45+
}
46+
public void addPost(User user, string postName, string postData)
47+
{
48+
string username = user.UserName;
49+
50+
SqlCommand addToDatabase = new SqlCommand("insert into Users(username, postName, postData) values(@username, @postName, @postData)");
51+
52+
addToDatabase.Parameters.AddWithValue("username", username);
53+
addToDatabase.Parameters.AddWithValue("postName", postName);
54+
addToDatabase.Parameters.AddWithValue("postData", postData);
55+
56+
_repository.executeQuery(addToDatabase);
57+
}
58+
59+
public void removePost(Posts posts)
60+
{
61+
string postName = posts.postName;
62+
if (!isPostExists(postName))
63+
{
64+
throw new Exception("Post not exists");
65+
}
66+
else
67+
{
68+
SqlCommand deleteFromDatabase = new SqlCommand("DELETE from Posts Where PostName = '" + postName + "'");
69+
70+
deleteFromDatabase.Parameters.AddWithValue("PostName", postName);
71+
72+
_repository.executeQuery(deleteFromDatabase);
73+
}
74+
}
75+
76+
public void searchForUserPosts(string username)
77+
{
78+
string query = "SELECT * from Posts Where username = '" + username + "'";
79+
SqlCommand searchInDatabase = new SqlCommand("SELECT * from Posts Where username = '" + username + "'");
80+
81+
searchInDatabase.Parameters.AddWithValue("username", username);
82+
83+
DataTable dataTable = new DataTable();
84+
85+
_repository.readDataFromCommands(query, dataTable);
86+
87+
}
88+
}
89+
}

Blog(Dirty)/PostsRepository.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Data.SqlClient;
7+
using System.Data;
8+
9+
namespace Blog_Dirty_
10+
{
11+
public class PostsRepository
12+
{
13+
private static SqlConnection connection = new SqlConnection();
14+
private static string databaseSource = "Data Source=(local)\\POSTSDATABASE;Initial Catalog=UserRepository;Integrated Security=True";
15+
public void createDatabase()
16+
{
17+
connection.ConnectionString = databaseSource;
18+
connection.Open();
19+
}
20+
21+
public void closeDatabase()
22+
{
23+
connection.Close();
24+
}
25+
26+
27+
28+
private static SqlDataAdapter adapter = new SqlDataAdapter();
29+
public int executeDataAdapter(DataTable tblName, string databaseCommands)
30+
{
31+
if (connection.State == 0)
32+
{
33+
createDatabase();
34+
}
35+
36+
adapter.SelectCommand.CommandText = databaseCommands;
37+
adapter.SelectCommand.CommandType = CommandType.Text;
38+
SqlCommandBuilder DbCommandBuilder = new SqlCommandBuilder(adapter);
39+
40+
string insert = DbCommandBuilder.GetInsertCommand().CommandText.ToString();
41+
string update = DbCommandBuilder.GetUpdateCommand().CommandText.ToString();
42+
string delete = DbCommandBuilder.GetDeleteCommand().CommandText.ToString();
43+
44+
return adapter.Update(tblName);
45+
}
46+
47+
private static SqlCommand command = new SqlCommand();
48+
public void readDataFromCommands(string query, DataTable tblName)
49+
{
50+
command.Connection = connection;
51+
command.CommandText = query;
52+
command.CommandType = CommandType.Text;
53+
54+
adapter = new SqlDataAdapter(command);
55+
adapter.Fill(tblName);
56+
}
57+
58+
public static SqlDataReader readDataFromStream(string query)
59+
{
60+
SqlDataReader reader;
61+
62+
command.Connection = connection;
63+
command.CommandText = query;
64+
command.CommandType = CommandType.Text;
65+
66+
reader = command.ExecuteReader();
67+
return reader;
68+
}
69+
70+
public int executeQuery(SqlCommand dbCommand)
71+
{
72+
dbCommand.Connection = connection;
73+
dbCommand.CommandType = CommandType.Text;
74+
75+
return dbCommand.ExecuteNonQuery();
76+
}
77+
}
78+
}

Blog(Dirty)/Program.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@ namespace Blog_Dirty_
44
{
55
sealed class Program
66
{
7-
private static UserManager userManager = new UserManager();
87
static void Main(string[] args)
98
{
109
Console.Write("Write a username: ");
1110
string username = Console.ReadLine();
1211
Console.Write("Write a password: ");
1312
string password = Console.ReadLine();
13+
14+
User user = new User(username, password);
15+
16+
UserManager userManager = new UserManager();
17+
18+
userManager.addUser(username, password);
19+
Console.WriteLine("User created sucessfully");
20+
userManager.removeUser(user);
21+
Console.WriteLine("User removed successfully");
22+
23+
BlogInterface blogInterface = new BlogInterface();
24+
25+
blogInterface.menu();
26+
1427
}
1528
}
1629

Blog(Dirty)/RepositoryModel.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Blog_Dirty_
8+
{
9+
public class RepositoryModel
10+
{
11+
}
12+
}

0 commit comments

Comments
 (0)