ArticleBusinessImpl.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using Common;
  2. using Common.Constant;
  3. using Common.Http;
  4. using Common.Logic;
  5. using Esim.Apis.Singleton;
  6. using Database.Database;
  7. using log4net;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Newtonsoft.Json;
  10. namespace Esim.Apis.Business
  11. {
  12. public class ArticleBusinessImpl : IArticleBusiness
  13. {
  14. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
  15. typeof(ArticleBusinessImpl)
  16. );
  17. private ModelContext dbContext;
  18. IConfiguration configuration;
  19. public ArticleBusinessImpl(ModelContext _dbContext, IConfiguration _configuration)
  20. {
  21. dbContext = _dbContext;
  22. configuration = _configuration;
  23. }
  24. private string GetParameter(string key)
  25. {
  26. return configuration.GetSection(key).Value ?? "";
  27. }
  28. /// <summary>
  29. /// Load article categories with pagination
  30. /// </summary>
  31. public async Task<IActionResult> ArticleCategory(HttpRequest httpRequest, ArticleCategoryReq request)
  32. {
  33. var url = httpRequest.Path;
  34. var json = JsonConvert.SerializeObject(request);
  35. log.Debug("URL: " + url + " => Request: " + json);
  36. try
  37. {
  38. string lang = CommonLogic.GetLanguage(httpRequest, request.lang);
  39. int pageNumber = request.pageNumber < 0 ? 0 : request.pageNumber;
  40. int pageSize = request.pageSize <= 0 ? 10 : request.pageSize;
  41. // Query categories
  42. var query = dbContext.ArticleCategories
  43. .Where(c => c.Status == true);
  44. // Filter by parent category
  45. if (request.parentId.HasValue)
  46. {
  47. query = query.Where(c => c.ParentId == request.parentId);
  48. }
  49. else
  50. {
  51. query = query.Where(c => c.ParentId == null); // Root categories
  52. }
  53. // Get total count for pagination
  54. int totalCount = query.Count();
  55. int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
  56. // Apply pagination and ordering
  57. var categories = query
  58. .OrderBy(c => c.DisplayOrder)
  59. .ThenBy(c => c.Id)
  60. .Skip(pageNumber * pageSize)
  61. .Take(pageSize)
  62. .Select(c => new
  63. {
  64. c.Id,
  65. categoryName = lang == "en"
  66. ? (c.CategoryNameEn ?? c.CategoryName)
  67. : (c.CategoryNameLo ?? c.CategoryName),
  68. c.CategorySlug,
  69. description = lang == "en"
  70. ? (c.DescriptionEn ?? c.Description)
  71. : (c.DescriptionLo ?? c.Description),
  72. c.IconUrl,
  73. c.ParentId,
  74. c.DisplayOrder
  75. })
  76. .ToList();
  77. return DotnetLib.Http.HttpResponse.BuildResponse(
  78. log,
  79. url,
  80. json,
  81. CommonErrorCode.Success,
  82. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  83. new
  84. {
  85. items = categories,
  86. pagination = new
  87. {
  88. pageNumber,
  89. pageSize,
  90. totalCount,
  91. totalPages
  92. }
  93. }
  94. );
  95. }
  96. catch (Exception exception)
  97. {
  98. log.Error("Exception: ", exception);
  99. }
  100. return DotnetLib.Http.HttpResponse.BuildResponse(
  101. log,
  102. url,
  103. json,
  104. CommonErrorCode.SystemError,
  105. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  106. new { }
  107. );
  108. }
  109. /// <summary>
  110. /// Load articles with pagination and filters
  111. /// </summary>
  112. public async Task<IActionResult> ArticleLoad(HttpRequest httpRequest, ArticleLoadReq request)
  113. {
  114. var url = httpRequest.Path;
  115. var json = JsonConvert.SerializeObject(request);
  116. log.Debug("URL: " + url + " => Request: " + json);
  117. try
  118. {
  119. string lang = CommonLogic.GetLanguage(httpRequest, request.lang);
  120. int pageNumber = request.pageNumber < 0 ? 0 : request.pageNumber;
  121. int pageSize = request.pageSize <= 0 ? 10 : request.pageSize;
  122. // If slug is provided, return single article detail
  123. if (!string.IsNullOrEmpty(request.slug))
  124. {
  125. var article = dbContext.Articles
  126. .Where(a => a.Slug == request.slug && a.Status == true)
  127. .Select(a => new
  128. {
  129. a.Id,
  130. title = lang == "en"
  131. ? (a.TitleEn ?? a.Title)
  132. : (a.TitleLo ?? a.Title),
  133. a.Slug,
  134. summary = lang == "en"
  135. ? (a.SummaryEn ?? a.Summary)
  136. : (a.SummaryLo ?? a.Summary),
  137. content = lang == "en"
  138. ? (a.ContentEn ?? a.Content)
  139. : (a.ContentLo ?? a.Content),
  140. a.ThumbnailUrl,
  141. a.CoverImageUrl,
  142. metaDescription = lang == "en"
  143. ? (a.MetaDescriptionEn ?? a.MetaDescription)
  144. : (a.MetaDescriptionLo ?? a.MetaDescription),
  145. a.MetaKeywords,
  146. a.CategoryId,
  147. a.ViewCount,
  148. a.IsFeatured,
  149. a.PublishedDate,
  150. a.CreatedDate
  151. })
  152. .FirstOrDefault();
  153. if (article == null)
  154. {
  155. return DotnetLib.Http.HttpResponse.BuildResponse(
  156. log,
  157. url,
  158. json,
  159. CommonErrorCode.Error,
  160. ConfigManager.Instance.GetConfigWebValue("ARTICLE_NOT_FOUND", lang),
  161. new { }
  162. );
  163. }
  164. // Increment view count
  165. var articleEntity = dbContext.Articles.FirstOrDefault(a => a.Slug == request.slug);
  166. if (articleEntity != null)
  167. {
  168. articleEntity.ViewCount = (articleEntity.ViewCount ?? 0) + 1;
  169. await dbContext.SaveChangesAsync();
  170. }
  171. return DotnetLib.Http.HttpResponse.BuildResponse(
  172. log,
  173. url,
  174. json,
  175. CommonErrorCode.Success,
  176. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  177. new { article }
  178. );
  179. }
  180. // Query articles list
  181. var query = dbContext.Articles
  182. .Where(a => a.Status == true);
  183. // Filter by category
  184. if (request.categoryId.HasValue)
  185. {
  186. query = query.Where(a => a.CategoryId == request.categoryId);
  187. }
  188. // Filter by featured
  189. if (request.isFeatured.HasValue && request.isFeatured.Value)
  190. {
  191. query = query.Where(a => a.IsFeatured == true);
  192. }
  193. // Get total count for pagination
  194. int totalCount = query.Count();
  195. int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
  196. // Apply pagination and ordering (pinned first, then by published date)
  197. var articles = query
  198. .OrderByDescending(a => a.IsPinned)
  199. .ThenByDescending(a => a.PublishedDate)
  200. .ThenByDescending(a => a.CreatedDate)
  201. .Skip(pageNumber * pageSize)
  202. .Take(pageSize)
  203. .Select(a => new
  204. {
  205. a.Id,
  206. title = lang == "en"
  207. ? (a.TitleEn ?? a.Title)
  208. : (a.TitleLo ?? a.Title),
  209. a.Slug,
  210. summary = lang == "en"
  211. ? (a.SummaryEn ?? a.Summary)
  212. : (a.SummaryLo ?? a.Summary),
  213. a.ThumbnailUrl,
  214. a.CategoryId,
  215. a.ViewCount,
  216. a.IsFeatured,
  217. a.IsPinned,
  218. a.PublishedDate
  219. })
  220. .ToList();
  221. return DotnetLib.Http.HttpResponse.BuildResponse(
  222. log,
  223. url,
  224. json,
  225. CommonErrorCode.Success,
  226. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  227. new
  228. {
  229. items = articles,
  230. pagination = new
  231. {
  232. pageNumber,
  233. pageSize,
  234. totalCount,
  235. totalPages
  236. }
  237. }
  238. );
  239. }
  240. catch (Exception exception)
  241. {
  242. log.Error("Exception: ", exception);
  243. }
  244. return DotnetLib.Http.HttpResponse.BuildResponse(
  245. log,
  246. url,
  247. json,
  248. CommonErrorCode.SystemError,
  249. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  250. new { }
  251. );
  252. }
  253. }
  254. }