public async Task <BlogElement> GetBlogElementAsync(int blogId) { var blogElement = new BlogElement(); using (var connection = await this.databaseConnection.GetConnectionAsync()) { using (var command = new MySqlCommand()) { command.CommandText = "SELECT id, created, updated, title, content" + " FROM blog WHERE id = ?id"; command.Parameters.Add(new MySqlParameter("?id", blogId)); command.Connection = connection; var reader = await command.ExecuteReaderAsync(); if (reader != null) { while (await reader.ReadAsync()) { blogElement = this.ReadBlogElement(reader); } } } } return(blogElement); }
public async Task <bool> SaveBlogElementAsync(BlogElement blogElement) { if (blogElement?.Article == null || blogElement?.ArticleTitle == null) { return(false); } // Should update to allow only specific HTML tags var safeArticle = blogElement.Article; var safeArticleTitle = blogElement.ArticleTitle; var safeBlogElement = new BlogElement() { Article = safeArticle, ArticleTitle = safeArticleTitle, BlogDate = blogElement.BlogDate, UpdateDate = blogElement.UpdateDate, Id = blogElement.Id }; if (blogElement.Id <= 0) { return(await this.dbBlogRepository.InsertBlogElementAsync(safeBlogElement)); } return(await this.dbBlogRepository.UpdateBlogElementAsync(safeBlogElement)); }
private BlogElement ReadBlogElement(DbDataReader reader) { var blogElement = new BlogElement(); blogElement.Id = reader.GetInt32(0); blogElement.BlogDate = reader.GetDateTime(1); blogElement.UpdateDate = reader.GetDateTime(2); blogElement.ArticleTitle = reader.GetString(3); blogElement.Article = reader.GetString(4); return(blogElement); }
public async Task <bool> UpdateBlogElementAsync(BlogElement blogElement) { var success = false; using (var connection = await this.databaseConnection.GetConnectionAsync()) { using (var command = new MySqlCommand()) { command.CommandText = "UPDATE blog set title = ?title, content = ?content," + "updated = ?updated WHERE id = ?id"; command.Parameters.Add(new MySqlParameter("?title", blogElement.ArticleTitle)); command.Parameters.Add(new MySqlParameter("?content", blogElement.Article)); command.Parameters.Add(new MySqlParameter("?updated", DateTime.Now)); command.Parameters.Add(new MySqlParameter("?id", blogElement.Id)); command.Connection = connection; var inserted = await command.ExecuteNonQueryAsync(); success = inserted == 1; } } return(success); }
public async Task <bool> InsertBlogElementAsync(BlogElement blogElement) { var success = false; using (var connection = await this.databaseConnection.GetConnectionAsync()) { using (var command = new MySqlCommand()) { command.CommandText = "INSERT INTO blog (title, content, created, updated) VALUES" + "(?title, ?content, ?created, ?updated)"; command.Parameters.Add(new MySqlParameter("?title", blogElement.ArticleTitle)); command.Parameters.Add(new MySqlParameter("?content", blogElement.Article)); command.Parameters.Add(new MySqlParameter("?created", DateTime.Now)); command.Parameters.Add(new MySqlParameter("?updated", DateTime.Now)); command.Connection = connection; var inserted = await command.ExecuteNonQueryAsync(); success = inserted == 1; } } return(success); }
public DbBlogRepositoryBuilder WithBlogElement(BlogElement blogElement) { this.blogElement = blogElement; return(this); }
public BlogContentServiceBuilder WithBlogElement(BlogElement blogElement) { this.blogElement = blogElement; return(this); }