ArticleBusinessImpl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. categories = 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. // Query articles list
  123. var query = dbContext.Articles
  124. .Where(a => a.Status == true);
  125. // Filter by category
  126. if (request.categoryId.HasValue)
  127. {
  128. query = query.Where(a => a.CategoryId == request.categoryId);
  129. }
  130. // Filter by featured
  131. if (request.isFeatured.HasValue && request.isFeatured.Value)
  132. {
  133. query = query.Where(a => a.IsFeatured == true);
  134. }
  135. // Get total count for pagination
  136. int totalCount = query.Count();
  137. int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
  138. // Apply pagination and ordering (pinned first, then by published date)
  139. var articles = query
  140. .OrderByDescending(a => a.IsPinned)
  141. .ThenByDescending(a => a.PublishedDate)
  142. .ThenByDescending(a => a.CreatedDate)
  143. .Skip(pageNumber * pageSize)
  144. .Take(pageSize)
  145. .Select(a => new
  146. {
  147. a.Id,
  148. title = lang == "en"
  149. ? (a.TitleEn ?? a.Title)
  150. : (a.TitleLo ?? a.Title),
  151. a.Slug,
  152. summary = lang == "en"
  153. ? (a.SummaryEn ?? a.Summary)
  154. : (a.SummaryLo ?? a.Summary),
  155. a.ThumbnailUrl,
  156. a.CategoryId,
  157. a.ViewCount,
  158. a.IsFeatured,
  159. a.IsPinned,
  160. a.PublishedDate
  161. })
  162. .ToList();
  163. return DotnetLib.Http.HttpResponse.BuildResponse(
  164. log,
  165. url,
  166. json,
  167. CommonErrorCode.Success,
  168. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  169. new
  170. {
  171. articles = articles,
  172. pagination = new
  173. {
  174. pageNumber,
  175. pageSize,
  176. totalCount,
  177. totalPages
  178. }
  179. }
  180. );
  181. }
  182. catch (Exception exception)
  183. {
  184. log.Error("Exception: ", exception);
  185. }
  186. return DotnetLib.Http.HttpResponse.BuildResponse(
  187. log,
  188. url,
  189. json,
  190. CommonErrorCode.SystemError,
  191. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  192. new { }
  193. );
  194. }
  195. /// <summary>
  196. /// Get article detail by ID or slug
  197. /// </summary>
  198. public async Task<IActionResult> ArticleDetail(HttpRequest httpRequest, ArticleDetailReq request)
  199. {
  200. var url = httpRequest.Path;
  201. var json = JsonConvert.SerializeObject(request);
  202. log.Debug("URL: " + url + " => Request: " + json);
  203. try
  204. {
  205. // Validate request - must have either id or slug
  206. if (!request.id.HasValue)
  207. {
  208. return DotnetLib.Http.HttpResponse.BuildResponse(
  209. log,
  210. url,
  211. json,
  212. CommonErrorCode.RequiredFieldMissing,
  213. ConfigManager.Instance.GetConfigWebValue("REQUIRED_FIELD_MISSING"),
  214. new { }
  215. );
  216. }
  217. string lang = CommonLogic.GetLanguage(httpRequest, request.lang);
  218. // Query by ID or slug
  219. var query = dbContext.Articles.Where(a => a.Status == true);
  220. if (request.id.HasValue)
  221. {
  222. query = query.Where(a => a.Id == request.id.Value);
  223. }
  224. else if (!string.IsNullOrEmpty(request.slug))
  225. {
  226. query = query.Where(a => a.Slug == request.slug);
  227. }
  228. var article = query
  229. .Select(a => new
  230. {
  231. a.Id,
  232. title = lang == "en"
  233. ? (a.TitleEn ?? a.Title)
  234. : (a.TitleLo ?? a.Title),
  235. a.Slug,
  236. summary = lang == "en"
  237. ? (a.SummaryEn ?? a.Summary)
  238. : (a.SummaryLo ?? a.Summary),
  239. content = lang == "en"
  240. ? (a.ContentEn ?? a.Content)
  241. : (a.ContentLo ?? a.Content),
  242. a.ThumbnailUrl,
  243. a.CoverImageUrl,
  244. metaDescription = lang == "en"
  245. ? (a.MetaDescriptionEn ?? a.MetaDescription)
  246. : (a.MetaDescriptionLo ?? a.MetaDescription),
  247. a.MetaKeywords,
  248. a.CategoryId,
  249. a.ViewCount,
  250. a.IsFeatured,
  251. a.PublishedDate,
  252. a.CreatedDate
  253. })
  254. .FirstOrDefault();
  255. if (article == null)
  256. {
  257. return DotnetLib.Http.HttpResponse.BuildResponse(
  258. log,
  259. url,
  260. json,
  261. CommonErrorCode.Error,
  262. ConfigManager.Instance.GetConfigWebValue("ARTICLE_NOT_FOUND", lang),
  263. new { }
  264. );
  265. }
  266. // Increment view count
  267. var articleEntity = request.id.HasValue
  268. ? dbContext.Articles.FirstOrDefault(a => a.Id == request.id.Value)
  269. : dbContext.Articles.FirstOrDefault(a => a.Slug == request.slug);
  270. if (articleEntity != null)
  271. {
  272. articleEntity.ViewCount = (articleEntity.ViewCount ?? 0) + 1;
  273. await dbContext.SaveChangesAsync();
  274. }
  275. return DotnetLib.Http.HttpResponse.BuildResponse(
  276. log,
  277. url,
  278. json,
  279. CommonErrorCode.Success,
  280. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  281. new { article }
  282. );
  283. }
  284. catch (Exception exception)
  285. {
  286. log.Error("Exception: ", exception);
  287. }
  288. return DotnetLib.Http.HttpResponse.BuildResponse(
  289. log,
  290. url,
  291. json,
  292. CommonErrorCode.SystemError,
  293. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  294. new { }
  295. );
  296. }
  297. }
  298. }