BasicController.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Diagnostics;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Identity;
  4. using Microsoft.AspNetCore.Authorization;
  5. using bitforum.Models;
  6. using bitforum.Models.User;
  7. using bitforum.Repository;
  8. using bitforum.Helpers;
  9. using bitforum.Services;
  10. namespace bitforum.Controllers.System
  11. {
  12. [Authorize]
  13. [Route("System")]
  14. public class BasicController : Controller
  15. {
  16. private readonly ILogger<BasicController> _logger;
  17. private readonly IConfigRepository _configRepository;
  18. private readonly IFileUploadService _fileUploadService;
  19. private readonly ISetupService _setupService;
  20. private readonly UserManager<ApplicationUser> _userManager;
  21. private readonly string _ViewPath = "~/Views/System/Basic.cshtml";
  22. public BasicController(ILogger<BasicController> logger, IConfigRepository configRepository, IFileUploadService fileUploadService, ISetupService setupService, UserManager<ApplicationUser> userManager)
  23. {
  24. _logger = logger;
  25. _configRepository = configRepository;
  26. _fileUploadService = fileUploadService;
  27. _setupService = setupService;
  28. _userManager = userManager;
  29. }
  30. [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  31. public IActionResult Error()
  32. {
  33. return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  34. }
  35. [HttpGet("Basic")]
  36. public IActionResult Index()
  37. {
  38. ViewBag.Config = _configRepository.GetAll();
  39. // 최고 관리자
  40. ViewBag.Admin = _userManager.GetUsersInRoleAsync("Admin").Result?.ToList();
  41. return View(_ViewPath);
  42. }
  43. [HttpPost("Basic")]
  44. public IActionResult Save(BasicForm request)
  45. {
  46. try
  47. {
  48. if (!ModelState.IsValid)
  49. {
  50. throw new Exception("기본 설정 값 저장에 실패");
  51. }
  52. var config = _configRepository.GetAll();
  53. request.BlockAlertContent = _fileUploadService.UploadEditorAsync(request.BlockAlertContent, config.GetConfig("block_alert_content"), UploadFolder.Basic, 1).Result;
  54. request.MaintenanceContent = _fileUploadService.UploadEditorAsync(request.MaintenanceContent, config.GetConfig("maintenance_content"), UploadFolder.Basic, 2).Result;
  55. Functions.SaveConfig(request, _configRepository.Replace);
  56. _setupService.RefreshConfigAsync().Wait();
  57. string message = "기본 설정 값이 정상적으로 저장되었습니다.";
  58. TempData["SuccessMessage"] = message;
  59. _logger.LogInformation(message);
  60. }
  61. catch (Exception e)
  62. {
  63. TempData["ErrorMessages"] = e.Message;
  64. _logger.LogError(e, e.Message);
  65. }
  66. return Index();
  67. }
  68. }
  69. }