/// <summary> /// 記事を取得します /// </summary> /// <param name="id"></param> /// <returns></returns> public bool SearchArticle(int id) { bool result = false; if (id < 1) { return result; } try { using (HNKArticleResource resource = new HNKArticleResource(AppSettings.GetDbConnectionString())) { Article article = resource.GetArticle(id, PublicStatus.Private); if (article != null && article.Id > 0) { this.Id = article.Id; this.OpenDateTime.Value = article.OpenDate; this.CloseDateTime.Value = article.CloseDate; this.Title = article.Title; this.Body = article.BodyRaw; this.PublicStatus = article.PublicStatus; this.Format = article.Format; this.CategoryId = article.Category.Id; result = true; } } } catch (Exception e) { AzureLog.WriteToTable(e); } return result; }
/// <summary> /// 公開中の記事を取得します /// </summary> /// <param name="dbConnectionString">データベース接続文字列</param> /// <param name="pageSize">ページサイズ</param> /// <param name="pageNumber">ページ番号</param> /// <param name="totalHitCount">ヒットした記事の総数</param> /// <param name="publicStatus">Publicだけの場合は指定しないでよい。Public, Privateの両方をとる場合はPublicStatus.Privateを指定する</param> /// <returns></returns> public static List<Article> GetArticles(string dbConnectionString, int pageSize, int pageNumber, out int totalHitCount, PublicStatus publicStatus = PublicStatus.Public) { totalHitCount = 0; List<Article> articles = new List<Article> { }; using (HNKArticleResource resource = new HNKArticleResource(dbConnectionString)) { articles = resource.GetArticles(pageSize, pageNumber, out totalHitCount, publicStatus); } return articles; }
/// <summary> /// 公開中の記事を取得します /// </summary> /// <param name="dbConnectionString">データベース接続文字列</param> /// <param name="pageSize">ページサイズ</param> /// <param name="pageNumber">ページ番号</param> /// <param name="totalHitCount">ヒットした記事の総数</param> /// <param name="publicStatus">Publicだけの場合は指定しないでよい。Public, Privateの両方をとる場合はPublicStatus.Privateを指定する</param> /// <returns></returns> public static List<Article> GetArticles(string dbConnectionString, int pageSize, int pageNumber, out int totalHitCount, string keyword, PublicStatus publicStatus = PublicStatus.Public) { totalHitCount = 0; List<Article> articles = new List<Article> { }; using (HNKArticleResource resource = new HNKArticleResource(dbConnectionString)) { string[] words = keyword.Split(' ', ' '); List<string> keywords = words.ToList(); articles = resource.GetArticles(pageSize, pageNumber, out totalHitCount, keywords, publicStatus); } return articles; }
public void ID指定で特定のブログ記事を取得する() { try { this.TestContext.WriteLine("データベースにテストデータをインサートします"); using (DataResource resource = new DataResource(TestConfig.GetDbConnectionString())) { string filePath = Path.Combine(TestConfig.SqlDir, "GetPublicArticles", "insert1.sql"); string sql = string.Empty; using (StreamReader reader = new StreamReader(filePath)) { sql = reader.ReadToEnd(); } if (!string.IsNullOrEmpty(sql)) { using (SqlCommand cmd = new SqlCommand(sql, resource.SqlConnection)) { cmd.ExecuteNonQuery(); } } } } catch (Exception e) { this.TestContext.WriteLine(e.ToString()); Assert.Fail(e.ToString()); } int id = 1; Article article = null; try { using (HNKArticleResource resource = new HNKArticleResource(TestConfig.GetDbConnectionString())) { article = resource.GetArticle(id); } } catch (Exception e) { Assert.Fail(e.ToString()); } Assert.IsNotNull(article, "データベースから記事を取得できていない"); Assert.IsTrue(article.PublicStatus == PublicStatus.Public, "未公開記事を取得している"); }
/// <summary> /// 公開中の記事を取得します /// </summary> /// <param name="dbConnectionString">データベース接続文字列</param> /// <param name="pageSize">ページサイズ</param> /// <param name="pageNumber">ページ番号</param> /// <param name="totalHitCount">ヒットした記事の総数</param> /// <param name="publicStatus">Publicだけの場合は指定しないでよい。Public, Privateの両方をとる場合はPublicStatus.Privateを指定する</param> /// <returns></returns> public static List<Article> GetArticlesWithDate(string dbConnectionString, int pageSize, int pageNumber, out int totalHitCount, int year, int month, PublicStatus publicStatus = PublicStatus.Public) { totalHitCount = 0; List<Article> articles = new List<Article> { }; try { using (HNKArticleResource resource = new HNKArticleResource(dbConnectionString)) { articles = resource.GetPublicArticlesByDate(year, month, pageSize, pageNumber, out totalHitCount); } } catch (Exception e) { } return articles; }
/// <summary> /// 公開可能な記事を取得します /// </summary> /// <param name="id"></param> /// <returns></returns> private Article GetPublicArticle(int id) { Article article = new Article(); if (id < 1) { return article; } try { using (HNKArticleResource resource = new HNKArticleResource(AppSettings.GetDbConnectionString())) { article = resource.GetArticle(id); } } catch (Exception e) { AzureLog.WriteToTable(e); } string[] delimiter = { "," }; string[] tagIdsStr = article.TagIds.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); if (tagIdsStr.Length > 0) { List<int> tagIdsInt = new List<int> { }; foreach (string tagIdStr in tagIdsStr) { int tagIdInt = 0; bool ret = int.TryParse(tagIdStr, out tagIdInt); if (ret) { tagIdsInt.Add(tagIdInt); } } try { using (HNKTagResource resource = new HNKTagResource(AppSettings.GetDbConnectionString())) { List<HNKTag> tags = resource.GetTags(tagIdsInt); if (tags != null && tags.Count > 0) { article.Tags = tags; } } } catch (Exception e) { AzureLog.WriteToTable(e); } } return article; }
/// <summary> /// 記事を追加します /// </summary> /// <param name="dbConnectionString">接続文字列</param> /// <param name="article">更新後のカテゴリ</param> /// <returns>成功した場合に真を返す</returns> public static bool InsertArticle(string dbConnectionString, Article article) { if (article == null || article.Id > 0) { return false; } bool result = false; using (HNKArticleResource resource = new HNKArticleResource(dbConnectionString)) { result = resource.InsertArticle(article); } return result; }
/// <summary> /// 公開中の記事数を月単位で取得します /// </summary> /// <returns></returns> public List<ArticleCount> GetPublicArticleCount() { List<ArticleCount> articles = new List<ArticleCount> { }; try { using (HNKArticleResource resource = new HNKArticleResource(AppSettings.GetDbConnectionString())) { articles = resource.GetPublicArticleCount(); } } catch (Exception e) { AzureLog.WriteToTable(e); } return articles; }
public void 日付範囲2014年8月1日から2014年8月末までのブログ記事を取得する() { try { this.TestContext.WriteLine("データベースにテストデータをインサートします"); using (DataResource resource = new DataResource(TestConfig.GetDbConnectionString())) { string filePath = Path.Combine(TestConfig.SqlDir, "GetPublicArticles", "insert3.sql"); string sql = string.Empty; using (StreamReader reader = new StreamReader(filePath)) { sql = reader.ReadToEnd(); } if (!string.IsNullOrEmpty(sql)) { using (SqlCommand cmd = new SqlCommand(sql, resource.SqlConnection)) { cmd.ExecuteNonQuery(); } } } } catch (Exception e) { this.TestContext.WriteLine(e.ToString()); Assert.Fail(e.ToString()); } List<Article> articles = null; try { using (HNKArticleResource resource = new HNKArticleResource(TestConfig.GetDbConnectionString())) { //articles = resource.GetPublicArticlesByDate(2014, 8); } } catch (Exception e) { Assert.Fail(e.ToString()); } Assert.IsNotNull(articles, "データベースから記事を取得できていない"); Assert.IsTrue(IsEqulPublicArticles(articles), "未公開記事を取得している"); Article article1 = articles[0]; Assert.IsTrue(article1.OpenDate == new DateTime(2014, 8, 31, 0, 0, 0), "公開日が昇順になっている"); Article article8 = articles[8]; Assert.IsTrue(article8.OpenDate == new DateTime(2014, 8, 1, 0, 0, 0), "公開日が昇順になっている"); }
/// <summary> /// 指定したカテゴリIDに属している公開中の記事を取得します /// </summary> /// <param name="categoryId">カテゴリID</param> /// <returns></returns> public List<Article> GetPublicArticles(int categoryId, int pageNumber) { if (pageNumber < 0) { pageNumber = 1; } List<Article> articles = new List<Article> { }; try { using (HNKArticleResource resource = new HNKArticleResource(AppSettings.GetDbConnectionString())) { articles = resource.GetPublicArticlesByCategory(categoryId, this._perPageLines, pageNumber, out this._totalArticleCount); } } catch (Exception e) { AzureLog.WriteToTable(e); } return articles; }