public async Task <IActionResult> PostH_sensor([FromBody] H_sensor h_sensor) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (User.Claims.Single(x => x.Type == ClaimTypes.Role).Value == "User") { if (Convert.ToInt32(User.FindFirstValue("Sub")) != _context.Enterprises.Find(_context.Rooms.Find(h_sensor.RoomId).EnterpriseId).UserId) { return(BadRequest("Stop hacking pls...")); } } if (GetUserElements.AddNewItem(Convert.ToInt32(User.FindFirstValue("Sub")), "Sensor", _context) == false) { return(BadRequest("Достигнуто максимально кол-во Sensors для вашего тарифа.")); } _context.H_sensors.Add(h_sensor); await _context.SaveChangesAsync(); return(CreatedAtAction("GetH_sensor", new { id = h_sensor.Id }, h_sensor)); }
public async Task <IActionResult> PutH_sensor([FromRoute] int id, [FromBody] H_sensor h_sensor) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != h_sensor.Id) { return(BadRequest()); } if (User.Claims.Single(x => x.Type == ClaimTypes.Role).Value == "User") { User user = _context.Users.Find(Convert.ToInt32(User.FindFirstValue("Sub"))); List <H_sensor> h_Sensors = GetUserElements.GetUserH_sensors(user.Id, _context); if (h_Sensors == null || h_Sensors.Count == 0 || h_Sensors.Where(x => x.Id == id).ToList().Count == 0) { return(NotFound()); } } //Alarm H_sensor sensor = _context.H_sensors.Find(h_sensor.Id); if (h_sensor.Value < sensor.Min_value) { _context.Rooms.Find(h_sensor.RoomId).Alarm = true; Enterprise enterprise = _context.Enterprises.Find(_context.Rooms.Find(h_sensor.RoomId).EnterpriseId); User user = _context.Users.Find(enterprise.UserId); SendMail.sendEmail(user.Mail, "ALARM"); } else if (h_sensor.Value > sensor.Max_value) { _context.Rooms.Find(h_sensor.RoomId).Alarm = true; Enterprise enterprise = _context.Enterprises.Find(_context.Rooms.Find(h_sensor.RoomId).EnterpriseId); User user = _context.Users.Find(enterprise.UserId); SendMail.sendEmail(user.Mail, "ALARM"); } _context.Entry(h_sensor).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!H_sensorExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }