public static PostJson Create(Post post, UrlHelper urlHelper) { return new PostJson { url = urlHelper.Link("PostApi", new { id = post._Qi_ID }), title = post._title, body = post._body, comment = post._comment, viewCount = post._viewCount, votes = post._votes }; }
public void CreatePostJsonModelTest() { var urlHelperMock = new Mock<UrlHelper>(); var post = new Post { _title = "Testing Posting a post", _Qi_ID = 123 }; var jsonModel = ModelFactory.Create(post, urlHelperMock.Object); Assert.AreEqual(post._title, jsonModel.title); urlHelperMock.Verify(m => m.Link("PostApi", It.IsAny<object>())); }
public Post find(int id) { var sql = string.Format("SELECT qi_ID, title, body, comment, viewcount, votes FROM Post WHERE qi_ID = {0}", id); using (var connection = new MySqlConnection(connectionString)) { connection.Open(); using (var cmd = new MySqlCommand(sql)) { cmd.Connection = connection; int idtemp = 0; string titletemp = "No title available"; string bodytemp = "This post has no body"; string commenttemp = "No comments to this post"; int viewcounttemp = 0; int votestemp = 0; using (var reader = cmd.ExecuteReader()) { if (reader.HasRows && reader.Read()) { if (!reader.IsDBNull(0)){ idtemp = reader.GetInt32(0); } else if (reader.IsDBNull(0)){ idtemp = 0; } if (!reader.IsDBNull(1)){ titletemp = reader.GetString(1); } else if (reader.IsDBNull(1)){ titletemp = "No title available"; } if (!reader.IsDBNull(2)) { bodytemp = reader.GetString(2); } else if (reader.IsDBNull(2)) { bodytemp = "This post has no body"; } if (!reader.IsDBNull(3)) { commenttemp = reader.GetString(3); } else if (reader.IsDBNull(3)) { commenttemp = "There is no comments to this post"; } if (!reader.IsDBNull(4)) { viewcounttemp = reader.GetInt32(4); } else if (reader.IsDBNull(4)) { viewcounttemp = 0; } if (!reader.IsDBNull(5)) { votestemp = reader.GetInt32(5); } else if (reader.IsDBNull(5)) { votestemp = 0; } var post = new Post() { _Qi_ID = idtemp, _title = titletemp, _body = bodytemp, _comment = commenttemp, _viewCount = viewcounttemp, _votes = votestemp, }; return post; } } } return null; } }
public Post find(string body) { var sql = string.Format("SELECT qi_ID, title, body, viewcount, votes FROM Post WHERE body like {0}", "\""+body+"\""); using (var connection = new MySqlConnection(connectionString)) { connection.Open(); using (var cmd = new MySqlCommand(sql)) { cmd.Connection = connection; using (var reader = cmd.ExecuteReader()) { if (reader.HasRows && reader.Read()) { var post = new Post() { _Qi_ID = reader.GetInt32(0), _title = reader.GetString(1), _body = reader.GetString(2), _viewCount = reader.GetInt32(3), _votes = reader.GetInt32(4), }; return post; } } } return null; } }