123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 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<CimController> _logger;
- private readonly Context _context;
-
- public CimController(Context context, ILogger<CimController> logger)
- {
- _context = context;
- _logger = logger;
- }
- [HttpPost("api/Cim/SetHostMqttInfo")]
- public ActionResult<ResultDto> GetHostMqttInfo(HostMqttInfoDto dto)
- {
- var host = _context.Set<HostMqttInfo>().FirstOrDefault();
- if (host == null)
- {
- var newHost = new HostMqttInfo()
- {
- Id = Guid.NewGuid(),
- IPAddress = dto.IPAddress,
- Port = dto.Port
- };
- _context.Set<HostMqttInfo>().Add(newHost);
- _context.SaveChanges();
- }
- else
- {
- host.IPAddress = dto.IPAddress;
- host.Port = dto.Port;
- _context.Set<HostMqttInfo>().Update(host);
- _context.SaveChanges();
- }
- return ResultDto.Success();
- }
- [HttpPost("api/Cim/GetHostMqttInfo")]
- public ActionResult<ResultDto<HostMqttInfo>> GetHostMqttInfo()
- {
- var host = _context.Set<HostMqttInfo>().FirstOrDefault();
- if (host == null)
- {
- return ResultDto<HostMqttInfo>.Fail("Not Found");
- }
- return ResultDto<HostMqttInfo>.Success(host);
- }
- [HttpPost("api/Cim/CheckPassword")]
- public ActionResult<ResultDto> CheckPassword([FromBody] CheckPasswordDto dto)
- {
- var cims = _context.Set<CimPassword>()
- .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<ResultDto> SetPassword([FromBody] PasswordDto dto)
- {
- if (string.IsNullOrEmpty(dto.Password))
- return ResultDto.Fail("password not null");
- var cim = _context.Set<CimPassword>()
- .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<CimPassword>().Update(cim);
- _context.SaveChanges();
- return ResultDto.Success();
- }
- [HttpGet("api/Cims")]
- public ActionResult<IEnumerable<CimPassword>> GetPasswords()
- {
- var cims = _context.Set<CimPassword>();
- return Ok(cims);
- }
- }
- }
|