using Microsoft.AspNetCore; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Hosting; using Safeguard.Dtos; using Safeguard.EntityFramework; using Safeguard.Filters; using Safeguard.Models; namespace Safeguard.Controllers { [TypeFilter(typeof(GlobalException))] public class CimController : ControllerBase { private readonly ILogger _logger; private readonly Context _context; public CimController(Context context, ILogger logger) { _context = context; _logger = logger; } [HttpPost("api/Cim/SetHostMqttInfo")] public ActionResult GetHostMqttInfo(HostMqttInfoDto dto) { var host = _context.Set().FirstOrDefault(); if (host == null) { var newHost = new HostMqttInfo() { Id = Guid.NewGuid(), IPAddress = dto.IPAddress, Port = dto.Port }; _context.Set().Add(newHost); _context.SaveChanges(); } else { host.IPAddress = dto.IPAddress; host.Port = dto.Port; _context.Set().Update(host); _context.SaveChanges(); } return ResultDto.Success(); } [HttpPost("api/Cim/GetHostMqttInfo")] public ActionResult> GetHostMqttInfo() { var host = _context.Set().FirstOrDefault(); if (host == null) { return ResultDto.Fail("Not Found"); } return ResultDto.Success(host); } [HttpPost("api/Cim/CheckPassword")] public ActionResult CheckPassword([FromBody] CheckPasswordDto dto) { var cims = _context.Set() .Where(x => x.Enabled == true && x.Password == dto.Password); if (cims.Count() <= 0) { return ResultDto.Fail("password null or incorrect"); } return ResultDto.Success(); } [HttpPost("api/Cim/SetPassword")] public ActionResult SetPassword([FromBody] PasswordDto dto) { if (string.IsNullOrEmpty(dto.Password)) return ResultDto.Fail("password not null"); var cim = _context.Set() .FirstOrDefault(x => x.No == dto.No); if (cim == null) return ResultDto.Fail("no is null"); cim.Enabled = dto.Enabled; cim.Password = dto.Password; _context.Set().Update(cim); _context.SaveChanges(); return ResultDto.Success(); } [HttpGet("api/Cims")] public ActionResult> GetPasswords() { var cims = _context.Set(); return Ok(cims); } } }