SitemapAuthController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Linq;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using FMSAdmin.Data;
  5. using FMSAdmin.Helpers;
  6. using FMSAdmin.Entities;
  7. using FMSAdmin.Services;
  8. using Microsoft.AspNetCore.Authorization;
  9. namespace FMSAdmin.Controllers {
  10. [Authorize]
  11. [ApiController]
  12. [ApiVersion("1")]
  13. [Route("api/[controller]")]
  14. public class SitemapAuthController : Controller {
  15. private readonly ILogger<SitemapAuthController> _logger;
  16. private readonly FMSContext _context;
  17. private readonly SitemapAuthService _service;
  18. public SitemapAuthController(
  19. ILogger<SitemapAuthController> logger,
  20. FMSContext context,
  21. SitemapAuthService service
  22. ) {
  23. _logger = logger;
  24. _context = context;
  25. _service = service;
  26. }
  27. /// <summary>
  28. /// 권한 일괄 수정
  29. /// </summary>
  30. /// <param name="siteId"></param>
  31. /// <param name="userGroupId"></param>
  32. /// <param name="type"></param>
  33. /// <param name="list"></param>
  34. /// <returns></returns>
  35. [HttpPost("{type}")]
  36. public IActionResult Edit(int siteId, int userGroupId, SitemapType type, [FromBody]SitemapAuth[] list) {
  37. _logger.LogInformation(list.Dump());
  38. if (list == null) {
  39. return BadRequest("데이터를 입력해주세요");
  40. }
  41. foreach (var data in list) {
  42. if (data.SiteId != siteId) {
  43. return BadRequest("현장정보가 잘못되었습니다");
  44. }
  45. if (data.UserGroupId != userGroupId) {
  46. return BadRequest("사용자그룹이 잘못되었습니다");
  47. }
  48. if (data.SitemapType != type) {
  49. return BadRequest("메뉴종류가 잘못되었습니다");
  50. }
  51. }
  52. _service.Save(siteId, userGroupId, type, list);
  53. return Ok(list.Count());
  54. }
  55. /// <summary>
  56. /// 자식 노드 리스트
  57. /// </summary>
  58. /// <param name="parentId"></param>
  59. /// <param name="type"></param>
  60. /// <returns></returns>
  61. [HttpGet("childs/{type}/{parentId?}")]
  62. public IActionResult Childs(int siteId, int userGroupId, SitemapType type, int? parentId) {
  63. var query = _service.SitemapAll().Where(x => x.Type == type && x.ParentId == parentId);
  64. // 최대 4뎁스
  65. var list = query.OrderBy(x => x.Position).Select(c => new {
  66. key = c.SitemapId,
  67. title = c.Name,
  68. action1 = c.Action1,
  69. action2 = c.Action2,
  70. action3 = c.Action3,
  71. action4 = c.Action4,
  72. action5 = c.Action5,
  73. auth = c.SitemapAuths.Where(x => x.SiteId == siteId && x.UserGroupId == userGroupId).Select(x => new {
  74. x.Access,
  75. x.Update,
  76. x.Delete,
  77. x.Business,
  78. x.Action1,
  79. x.Action2,
  80. x.Action3,
  81. x.Action4,
  82. x.Action5
  83. }).FirstOrDefault(),
  84. children = c.Childs.OrderBy(x => x.Position).Select(c1 => new {
  85. key = c1.SitemapId,
  86. title = c1.Name,
  87. action1 = c1.Action1,
  88. action2 = c1.Action2,
  89. action3 = c1.Action3,
  90. action4 = c1.Action4,
  91. action5 = c1.Action5,
  92. auth = c1.SitemapAuths.Where(x => x.SiteId == siteId && x.UserGroupId == userGroupId).Select(x => new {
  93. x.Access,
  94. x.Update,
  95. x.Delete,
  96. x.Business,
  97. x.Action1,
  98. x.Action2,
  99. x.Action3,
  100. x.Action4,
  101. x.Action5
  102. }).FirstOrDefault(),
  103. children = c1.Childs.OrderBy(x => x.Position).Select(c2 => new {
  104. key = c2.SitemapId,
  105. title = c2.Name,
  106. action1 = c2.Action1,
  107. action2 = c2.Action2,
  108. action3 = c2.Action3,
  109. action4 = c2.Action4,
  110. action5 = c2.Action5,
  111. auth = c2.SitemapAuths.Where(x => x.SiteId == siteId && x.UserGroupId == userGroupId).Select(x => new {
  112. x.Access,
  113. x.Update,
  114. x.Delete,
  115. x.Business,
  116. x.Action1,
  117. x.Action2,
  118. x.Action3,
  119. x.Action4,
  120. x.Action5
  121. }).FirstOrDefault(),
  122. children = c2.Childs.OrderBy(x => x.Position).Select(c3 => new {
  123. key = c3.SitemapId,
  124. title = c3.Name,
  125. action1 = c3.Action1,
  126. action2 = c3.Action2,
  127. action3 = c3.Action3,
  128. action4 = c3.Action4,
  129. action5 = c3.Action5,
  130. auth = c3.SitemapAuths.Where(x => x.SiteId == siteId && x.UserGroupId == userGroupId).Select(x => new {
  131. x.Access,
  132. x.Update,
  133. x.Delete,
  134. x.Business,
  135. x.Action1,
  136. x.Action2,
  137. x.Action3,
  138. x.Action4,
  139. x.Action5
  140. }).FirstOrDefault(),
  141. })
  142. })
  143. })
  144. });
  145. return Ok(list);
  146. }
  147. }
  148. }