Exemplo n.º 1
0
        public async Task <IActionResult> CreateSensor(Sensor sensor)
        {
            try
            {
                var id = await _sensorService.CreateSensor(sensor);

                return(Ok(id));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 2
0
        public async Task <JsonResult> Create(AddSensorModel 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 != "admin")
                {
                    return(Json("Only Admin can add sensor"));
                }

                else
                {
                    try
                    {
                        ApplicationUserDTO owner = await userService.GetUserByEmail(model.Email);

                        try
                        {
                            ProductDTO product = productService.GetProductByName(model.Product);
                            SensorDTO  sensor  = new SensorDTO
                            {
                                Name              = model.Name,
                                IsWorking         = false,
                                IsProduct         = false,
                                CountProduct      = 1,
                                DeliveryAddress   = model.DeliveryAddress,
                                AutoDelivery      = false,
                                ApplicationUserId = owner.Id,
                                ProductId         = product.Id
                            };

                            return(Json(sensorService.CreateSensor(sensor).Result));
                        }
                        catch
                        {
                            return(Json("Product is not exists"));
                        }
                    }
                    catch
                    {
                        return(Json("Wrang Email"));
                    }
                }
            }
            catch
            {
                return(Json("Email or Token is wrong"));
            }
        }