MobileShortcutController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using FMSAdmin.Data;
  5. using FMSAdmin.Models;
  6. using FMSAdmin.Entities;
  7. using FMSApp.Services;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Logging;
  10. using Microsoft.AspNetCore.Authorization;
  11. using FMSAdmin.Helpers;
  12. using System.Data;
  13. using System.IO;
  14. using OfficeOpenXml;
  15. using System.Collections.Generic;
  16. using System.Linq.Dynamic.Core;
  17. namespace FMSApp.Controllers {
  18. [Authorize]
  19. [ApiController]
  20. [ApiVersion("1")]
  21. [Route("api/app/[controller]")]
  22. public class MobileShortcutController : Controller {
  23. private readonly ILogger<MobileShortcutController> _logger;
  24. private readonly FMSContext _context;
  25. private readonly MobileShortcutService _service;
  26. public MobileShortcutController(
  27. ILogger<MobileShortcutController> logger,
  28. FMSContext context,
  29. MobileShortcutService service
  30. ) {
  31. _logger = logger;
  32. _context = context;
  33. _service = service;
  34. }
  35. [HttpGet("[action]")]
  36. public IActionResult DefaultDashboardList() {
  37. return Ok(List(null, ShortcutType.대시보드메뉴, false));
  38. }
  39. [HttpGet("[action]")]
  40. public IActionResult DefaultBottomList() {
  41. return Ok(List(null, ShortcutType.하단메뉴, false));
  42. }
  43. [HttpGet("[action]")]
  44. public IActionResult DashboardList([FromQuery] string userId) {
  45. var list = List(userId, ShortcutType.대시보드메뉴);
  46. if (list.Count() == 0) {
  47. return DefaultDashboardList();
  48. } else {
  49. return Ok(RemoveDeleted(list));
  50. }
  51. }
  52. [HttpGet("[action]")]
  53. public IActionResult BottomList([FromQuery] string userId) {
  54. var list = List(userId, ShortcutType.하단메뉴);
  55. _logger.LogInformation($"BottomList - list.Count = {list.Count()}");
  56. if (list.Count() == 0) {
  57. return DefaultBottomList();
  58. } else {
  59. return Ok(RemoveDeleted(list));
  60. }
  61. }
  62. private IQueryable<object> RemoveDeleted(IQueryable<object> list) {
  63. return list.Where("IsDelete == false");
  64. }
  65. private IQueryable<object> List(string userId, ShortcutType type, bool useDeleted = true) {
  66. var query = _service.GetAll();
  67. query = query.Where(x => x.Type == type);
  68. if (string.IsNullOrEmpty(userId)) {
  69. query = query.Where(x => x.UserId == null || x.UserId == "");
  70. } else {
  71. query = query.Where(x => x.UserId == userId);
  72. }
  73. if (!useDeleted) {
  74. query = query.Where(x => x.IsDelete == false);
  75. }
  76. query = query.OrderBy(x => x.Position);
  77. var list = query.Select(x => new {
  78. x.MobileShortcutId,
  79. x.UserId,
  80. UserName = x.CmUser.Name,
  81. x.SitemapId,
  82. Sitemap = new {
  83. x.Sitemap.Name,
  84. x.Sitemap.Path,
  85. },
  86. x.Type,
  87. x.Icon,
  88. x.IsDelete,
  89. });
  90. return list;
  91. }
  92. [HttpGet("[action]")]
  93. public IActionResult Get([FromQuery] string shortcutId) {
  94. var shortcut = _service.GetAll().Where(
  95. x => x.MobileShortcutId == Util.ToInt(shortcutId)
  96. ).FirstOrDefault();
  97. return Ok(new {
  98. shortcut.MobileShortcutId,
  99. UserId = shortcut.UserId ?? "",
  100. UserName = shortcut.CmUser?.Name ?? "",
  101. shortcut.SitemapId,
  102. Sitemap = new {
  103. shortcut.Sitemap.Name,
  104. shortcut.Sitemap.Path,
  105. },
  106. shortcut.Type,
  107. shortcut.Icon,
  108. });
  109. }
  110. /// <summary>
  111. /// 저장
  112. /// </summary>
  113. /// <param name="data"></param>
  114. /// <returns></returns>
  115. [HttpPost("[action]")]
  116. public IActionResult Save([FromBody] MobileShortcut data) {
  117. if (ModelState.IsValid) {
  118. _service.Save(data);
  119. } else {
  120. return BadRequest(ModelState);
  121. }
  122. return Ok();
  123. }
  124. [HttpDelete("{userId}/{sitemapId}/{type}")]
  125. public IActionResult DeleteBottom(string userId, int sitemapId, ShortcutType type) {
  126. _service.Delete(userId, sitemapId, type);
  127. return Ok();
  128. }
  129. [HttpGet("[action]")]
  130. public IActionResult PositionUp([FromQuery] string userId, [FromQuery] int sitemapId, [FromQuery] ShortcutType type) {
  131. _service.PositionUp(userId, sitemapId, type);
  132. return Ok();
  133. }
  134. [HttpGet("[action]")]
  135. public IActionResult PositionDown([FromQuery] string userId, [FromQuery] int sitemapId, [FromQuery] ShortcutType type) {
  136. _service.PositionDown(userId, sitemapId, type);
  137. return Ok();
  138. }
  139. // 검색 & 정렬 공통
  140. private IQueryable<MobileShortcut> _FilterAndSort(PagingRequest req) {
  141. var query = _service.GetAll();
  142. // 기본 Entity 검색
  143. query = query.Filter(req.conditions);
  144. // 기본 Entity 정렬
  145. query = query.Sort(req.sort);
  146. return query;
  147. }
  148. }
  149. }