コード例 #1
0
        public IActionResult AddActive(ActiveReadingAddViewModel model)
        {
            if (model.DeviceId == null)
            {
                return(RedirectToAction("Index", "Device", new { armessage = ActiveReadingMessageId.DeviceNotFound }));
            }

            Device device = _deviceRepo.Get(model.DeviceId);

            if (device == null)
            {
                return(RedirectToAction("Index", "Device", new { armessage = ActiveReadingMessageId.DeviceNotFound }));
            }

            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.ValidationFailed }));
            }

            string authUserId = GetCurrentUserAsync().Result.Id;

            if (!device.User.UserId.Equals(authUserId))
            {
                return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.NotAuthorized }));
            }

            if (_readingsRepo.CheckReadingNameAlreadyExists(model.Name, authUserId))
            {
                return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.ReadingNameAlreadyExists }));
            }

            try
            {
                var activeReading = new ActiveReading
                {
                    Id           = Guid.NewGuid().ToString(),
                    Device       = device,
                    ActiveSince  = DateTime.Now,
                    Name         = model.Name,
                    DataFilepath = model.DataFilepath,
                    ReadingType  = _readingsRepo.GetReadingType(model.ReadingType),
                    Owner        = device.User
                };

                _readingsRepo.AddActiveReading(activeReading);
                _logger.LogInformation("User activated a new reading.");
                return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.AddActiveReadingSuccess }));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
                return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.AddActiveReadingFailed }));
            }
        }
コード例 #2
0
        public IActionResult AddActive(string deviceId)
        {
            if (deviceId == null)
            {
                return(RedirectToAction("Manage", "Device", new { armessage = ActiveReadingMessageId.ActiveReadingNotFound }));
            }

            Device device = _deviceRepo.Get(deviceId);

            if (device == null)
            {
                return(RedirectToAction("Index", "Device", new { dmessage = DeviceController.DeviceMessageId.DeviceNotFound }));
            }

            ViewData["deviceName"] = device.Hostname;
            var model = new ActiveReadingAddViewModel
            {
                DeviceId        = deviceId,
                ReadingTypeList = _readingsRepo.GetReadingTypesAll()
            };

            return(PartialView(model));
        }