Ejemplo n.º 1
0
        public IActionResult Delete(DeviceDeleteViewModel 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);

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

                _deviceRepo.Delete(device);
                _logger.LogInformation("User deleted one device.");
                return(RedirectToAction("Index", new { dmessage = DeviceMessageId.DeleteDeviceSuccess }));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
                return(RedirectToAction("Index", new { dmessage = DeviceMessageId.DeleteDeviceFailed }));
            }
        }
Ejemplo n.º 2
0
        public async Task <DeviceResponse> DeleteAsync(int Id)
        {
            var existing = await _deviceRepo.FindByIdAsync(Id);

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

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

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