public async Task <IActionResult> EditSensor(
            [Bind(include: WebConstants.UserSensorViewModelBindingString)] EditSensorModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(View(model));
            }
            if (model.UserMinValue >= model.UserMaxValue)
            {
                this.TempData["Invalid-Min-Max-Value"] = "The minimum value can not be equal to or greater than the maximum value.";
                return(View(model));
            }

            var editedSensor = new UserSensor()
            {
                Id               = model.Id,
                Name             = model.Name,
                PollingInterval  = model.UserPollingInterval,
                SampleSensorId   = model.SampleSensorId,
                UserId           = model.UserId,
                UserMinValue     = model.UserMinValue,
                UserMaxValue     = model.UserMaxValue,
                Latitude         = model.Latitude,
                Longitude        = model.Longitude,
                SendNotification = model.SendNotification,
                IsPrivate        = model.IsPrivate,
                CreatedOn        = model.RegisteredOn
            };

            UserSensor sensor;

            try
            {
                sensor = await this.sensorsService.EditSensorAsync(editedSensor);
            }
            catch (SensorNullableException ex)
            {
                this.TempData["Service-Error"] = ex.Message;
                return(View("ServiceError"));
            }

            return(this.RedirectToAction("SensorDetails", new { userSensorid = sensor.Id }));
        }
        public async Task <IActionResult> EditSensor(Guid id)
        {
            if (id == null)
            {
                return(BadRequest());
            }

            UserSensor userSensor;

            try
            {
                userSensor = await this.sensorsService.GetUserSensorAsync(id);
            }
            catch (SensorNullableException ex)
            {
                this.TempData["Service-Error"] = ex.Message;
                return(View("ServiceError"));
            }

            var model = new EditSensorModel(userSensor);

            return(View("EditSensor", model));
        }
Exemple #3
0
        public async Task <JsonResult> SiteEdit(EditSensorModel model)
        {
            var results = new List <ValidationResult>();
            var context = new ValidationContext(model);

            if (!Validator.TryValidateObject(model, context, results, true))
            {
                return(Json("Bad data"));
            }
            ApplicationUserDTO checkUser = new ApplicationUserDTO
            {
                Email         = model.EmailAdmin,
                SecurityStamp = model.SecurityStamp
            };

            try
            {
                ApplicationUserDTO user = await userService.GetUserByEmailAndSecurityStamp(checkUser);

                if (user.Role != "user")
                {
                    return(Json("Only User can have sensors"));
                }
                else
                {
                    try
                    {
                        var sensor = sensorService.GetSensorById(model.Id);
                        if (sensor.ApplicationUserId != user.Id)
                        {
                            return(Json("You doesn't have access to this sensor"));
                        }
                        try
                        {
                            ApplicationUserDTO userObserver = await userService.GetUserByEmail(model.ObserverEmail);

                            try
                            {
                                MonitoringDTO monitoring = monitoringService.GetMonitoringsBySensorId(sensor.Id);
                                monitoring.ApplicationUserId = userObserver.Id;
                                monitoringService.EditMonitoring(monitoring);
                                try
                                {
                                    sensor.IsWorking       = model.IsWorking;
                                    sensor.AutoDelivery    = model.AutoDelivery;
                                    sensor.DeliveryAddress = model.DeliveryAddress;
                                    sensor.Name            = model.Name;
                                    sensor.CountProduct    = model.CountProduct;
                                    return(Json(sensorService.EditSensor(sensor).Result, JsonRequestBehavior.AllowGet));
                                }
                                catch
                                {
                                    return(Json("Bad with update Sensor", JsonRequestBehavior.AllowGet));
                                }
                            }
                            catch
                            {
                                return(Json("Bad with update Monitoring"));
                            }
                        }
                        catch
                        {
                            return(Json("Observer Email is wrong"));
                        }
                    }
                    catch
                    {
                        return(Json("Wrong Sensor Id"));
                    }
                }
            }
            catch
            {
                return(Json("Email or Token is wrong"));
            }
        }