CompanyController.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Diagnostics;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.Rendering;
  4. using Microsoft.AspNetCore.Authorization;
  5. using bitforum.Models;
  6. using bitforum.Repository;
  7. using bitforum.Helpers;
  8. using bitforum.Constants;
  9. using bitforum.Services;
  10. namespace bitforum.Controllers.System
  11. {
  12. [Authorize]
  13. [Route("System")]
  14. public class CompanyController : Controller
  15. {
  16. private readonly ILogger<CompanyController> _logger;
  17. private readonly IConfigRepository _configRepository;
  18. private readonly ISetupService _setupService;
  19. private readonly string _ViewPath = "~/Views/System/Company.cshtml";
  20. public CompanyController(ILogger<CompanyController> logger, IConfigRepository configRepository, ISetupService setupService)
  21. {
  22. _logger = logger;
  23. _configRepository = configRepository;
  24. _setupService = setupService;
  25. }
  26. [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  27. public IActionResult Error()
  28. {
  29. return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  30. }
  31. [HttpGet("Company")]
  32. public IActionResult Index()
  33. {
  34. var config = _configRepository.GetAll();
  35. ViewBag.Config = config;
  36. ViewBag.BankCodes = BankCodeData.List.Select(item => new SelectListItem
  37. {
  38. Value = item.Value,
  39. Text = item.Text,
  40. Selected = item.Value == config.GetConfig("company_bank_code")
  41. }).ToList();
  42. return View(_ViewPath);
  43. }
  44. [HttpPost("Company")]
  45. public IActionResult Save(CompanyForm request)
  46. {
  47. try
  48. {
  49. if (!ModelState.IsValid)
  50. {
  51. throw new Exception("회사 정보 설정 값 저장에 실패");
  52. }
  53. Functions.SaveConfig(request, _configRepository.Replace);
  54. _setupService.RefreshConfigAsync().Wait();
  55. string message = "회사 정보 설정 값이 정상적으로 저장되었습니다.";
  56. TempData["SuccessMessage"] = message;
  57. _logger.LogInformation(message);
  58. }
  59. catch (Exception e)
  60. {
  61. TempData["ErrorMessages"] = e.Message;
  62. _logger.LogError(e, e.Message);
  63. }
  64. return Index();
  65. }
  66. }
  67. }