|
| 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 | +} |
0 commit comments