public IActionResult Put([FromBody] CommandViewModel model) { if (model == null) { return(StatusCode(500, new InternalServerError())); } var user = User.GetUser(_context); if (user == null) { return(Unauthorized(new UnauthorizedError())); } var script = _context.Scripts.Find(model.ScriptId); if (script == null) { return(NotFound(new NotFoundError("Script not found."))); } var userHasController = _context .UserHasControllers .Where(p => p.ControllerId == script.ControllerId) .Where(p => p.UserId == user.Id) .FirstOrDefault(); if (userHasController == null) { return(Unauthorized(new UnauthorizedError())); } var command = new Command { Name = model.Name, ScriptId = model.ScriptId, End = model.End, DeviceConfigurationId = model.DeviceConfigurationId, TimeSpan = model.TimeSpan }; _context.Commands.Add(command); _context.SaveChanges(); try { command.DeviceConfiguration = _context.DeviceConfigurations.Find(command.DeviceConfigurationId); command.DeviceConfiguration.Device = _context.Devices.Find(command.DeviceConfiguration.DeviceId); command.DeviceConfiguration.Measure = _context.Measures.Find(command.DeviceConfiguration.MeasureId); } catch (NullReferenceException exeption) { } return(Json(command.Adapt <CommandViewModel>())); }