Example #1
0
        public async Task <JsonResult> GetSensorById(SensorId 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.Email,
                SecurityStamp = model.SecurityStamp
            };

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

                if (user.Role != "user")
                {
                    return(Json("Only User can have sensors"));
                }
                else
                {
                    var sensor = sensorService.GetSensorById(model.Id);
                    if (sensor.ApplicationUserId != user.Id)
                    {
                        return(Json("You doesn't have access to this sensor"));
                    }
                    var        mapper    = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap <SensorDTO, GetSensor>()).CreateMapper();
                    GetSensor  getSensor = mapper.Map <SensorDTO, GetSensor>(sensor);
                    ProductDTO product   = productService.GetProductById(sensor.ProductId);
                    getSensor.ProductName = product.Name;
                    MonitoringDTO      monitoring = monitoringService.GetMonitoringsBySensorId(sensor.Id);
                    ApplicationUserDTO observer   = await userService.GetUserById(monitoring.ApplicationUserId);

                    getSensor.ObserverEmail = observer.Email;

                    return(Json(getSensor, JsonRequestBehavior.AllowGet));
                }
            }
            catch
            {
                return(Json("Email or Token is wrong"));
            }
        }
Example #2
0
        public async Task <JsonResult> GetMonitoringsByUserEmailAndSecurityStamp(CheckModel 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.Email,
                SecurityStamp = model.SecurityStamp
            };

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

                if (user.Role != "user")
                {
                    return(Json("Only User can monitoring", JsonRequestBehavior.AllowGet));
                }
                else
                {
                    try
                    {
                        var monitorngs = monitoringService.GetMonitoringsByUserId(user.Id).ToList();
                        List <GetSensor> getSensors = new List <GetSensor>();
                        foreach (var monitoring in monitorngs)
                        {
                            try
                            {
                                SensorDTO sensor = sensorService.GetSensorById(monitoring.SensorId);
                                try
                                {
                                    ApplicationUserDTO ownerUser = await userService.GetUserById(sensor.ApplicationUserId);

                                    try
                                    {
                                        var        mapper    = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap <SensorDTO, GetSensor>()).CreateMapper();
                                        GetSensor  getSensor = mapper.Map <SensorDTO, GetSensor>(sensor);
                                        ProductDTO product   = productService.GetProductById(sensor.ProductId);
                                        getSensor.ProductName   = product.Name;
                                        getSensor.ObserverEmail = ownerUser.Email;
                                        getSensors.Add(getSensor);
                                    }
                                    catch
                                    {
                                        return(Json("Bad with get product", JsonRequestBehavior.AllowGet));
                                    }
                                }
                                catch
                                {
                                    return(Json("Bad with get owner user", JsonRequestBehavior.AllowGet));
                                }
                            }
                            catch
                            {
                                return(Json("Bad with get sensor", JsonRequestBehavior.AllowGet));
                            }
                        }
                        return(Json(getSensors, JsonRequestBehavior.AllowGet));
                    }
                    catch
                    {
                        return(Json("Bad with monitorings", JsonRequestBehavior.AllowGet));
                    }
                }
            }
            catch
            {
                return(Json("Email or Token is wrong", JsonRequestBehavior.AllowGet));
            }
        }