public IActionResult Edit(DeviceEditViewModel model)
        {
            if (model.Id == null)
            {
                return(RedirectToAction("Index", new { dmessage = DeviceMessageId.DeviceNotFound }));
            }

            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index), new { dmessage = DeviceMessageId.ValidationFailed }));
            }

            try
            {
                Device device = _deviceRepo.Get(model.Id);

                var authUserId = GetCurrentUserAsync().Result.Id;
                if (device.Hostname != model.Hostname)
                {
                    if (_deviceRepo.CheckHostnameExists(model.Hostname, authUserId))
                    {
                        return(RedirectToAction(nameof(Index), new { dmessage = DeviceMessageId.HostnameAlreadyExists }));
                    }
                }

                if (!device.User.UserId.Equals(authUserId))
                {
                    return(RedirectToAction(nameof(Index), new { dmessage = DeviceMessageId.NotAuthorized }));
                }

                device.Hostname   = model.Hostname;
                device.DeviceType = _deviceRepo.GetDeviceType(model.DeviceType);
                device.UpdatedOn  = DateTime.Now;

                _deviceRepo.Update(device);
                _logger.LogInformation("User updated one device.");
                return(RedirectToAction("Index", new { dmessage = DeviceMessageId.UpdateDeviceSuccess }));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
                return(RedirectToAction("Index", new { dmessage = DeviceMessageId.UpdateDeviceFailed }));
            }
        }
Beispiel #2
0
        public async Task <DeviceResponse> UpdateAsync(int Id, Device device)
        {
            var existing = await _deviceRepo.FindByIdAsync(Id);

            if (existing == null)
            {
                return(new DeviceResponse("Device not found."));
            }

            existing.Vendor = device.Vendor;
            existing.Status = device.Status;

            try {
                _deviceRepo.Update(existing);
                await _deviceRepo.SaveAllAsync();

                return(new DeviceResponse(existing));
            } catch (Exception ex) {
                return(new DeviceResponse($"An error occurred : {ex.Message}"));
            }
        }