CimController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Microsoft.AspNetCore;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.Hosting;
  6. using Safeguard.Dtos;
  7. using Safeguard.EntityFramework;
  8. using Safeguard.Filters;
  9. using Safeguard.Models;
  10. namespace Safeguard.Controllers
  11. {
  12. [TypeFilter(typeof(GlobalException))]
  13. public class CimController : ControllerBase
  14. {
  15. private readonly ILogger<CimController> _logger;
  16. private readonly Context _context;
  17. public CimController(Context context, ILogger<CimController> logger)
  18. {
  19. _context = context;
  20. _logger = logger;
  21. }
  22. [HttpPost("api/Cim/SetHostMqttInfo")]
  23. public ActionResult<ResultDto> GetHostMqttInfo(HostMqttInfoDto dto)
  24. {
  25. var host = _context.Set<HostMqttInfo>().FirstOrDefault();
  26. if (host == null)
  27. {
  28. var newHost = new HostMqttInfo()
  29. {
  30. Id = Guid.NewGuid(),
  31. IPAddress = dto.IPAddress,
  32. Port = dto.Port
  33. };
  34. _context.Set<HostMqttInfo>().Add(newHost);
  35. _context.SaveChanges();
  36. }
  37. else
  38. {
  39. host.IPAddress = dto.IPAddress;
  40. host.Port = dto.Port;
  41. _context.Set<HostMqttInfo>().Update(host);
  42. _context.SaveChanges();
  43. }
  44. return ResultDto.Success();
  45. }
  46. [HttpPost("api/Cim/GetHostMqttInfo")]
  47. public ActionResult<ResultDto<HostMqttInfo>> GetHostMqttInfo()
  48. {
  49. var host = _context.Set<HostMqttInfo>().FirstOrDefault();
  50. if (host == null)
  51. {
  52. return ResultDto<HostMqttInfo>.Fail("Not Found");
  53. }
  54. return ResultDto<HostMqttInfo>.Success(host);
  55. }
  56. [HttpPost("api/Cim/CheckPassword")]
  57. public ActionResult<ResultDto> CheckPassword([FromBody] CheckPasswordDto dto)
  58. {
  59. var cims = _context.Set<CimPassword>()
  60. .Where(x => x.Enabled == true && x.Password == dto.Password);
  61. if (cims.Count() <= 0)
  62. {
  63. return ResultDto.Fail("password null or incorrect");
  64. }
  65. return ResultDto.Success();
  66. }
  67. [HttpPost("api/Cim/SetPassword")]
  68. public ActionResult<ResultDto> SetPassword([FromBody] PasswordDto dto)
  69. {
  70. if (string.IsNullOrEmpty(dto.Password))
  71. return ResultDto.Fail("password not null");
  72. var cim = _context.Set<CimPassword>()
  73. .FirstOrDefault(x => x.No == dto.No);
  74. if (cim == null)
  75. return ResultDto.Fail("no is null");
  76. cim.Enabled = dto.Enabled;
  77. cim.Password = dto.Password;
  78. _context.Set<CimPassword>().Update(cim);
  79. _context.SaveChanges();
  80. return ResultDto.Success();
  81. }
  82. [HttpGet("api/Cims")]
  83. public ActionResult<IEnumerable<CimPassword>> GetPasswords()
  84. {
  85. var cims = _context.Set<CimPassword>();
  86. return Ok(cims);
  87. }
  88. }
  89. }