public IActionResult Sensor(AddSensorModel addSensor)
        {
            string connString = @"Data Source=DESKTOP-JM077BN;Initial Catalog=ArduinoStore;Integrated Security=True";

            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    string     query = $"INSERT INTO Product VALUES({addSensor.Price}, {addSensor.CompanyID}, '{addSensor.Model}', 0, '{addSensor.ProductImage}'); SELECT SCOPE_IDENTITY()";
                    SqlCommand cmd   = new SqlCommand(query, conn);
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        dr.Read();
                        int productID = int.Parse(dr[0].ToString());
                        dr.Close();
                        int digitalPin = addSensor.DigitalPin ? 1 : 0;
                        int analogPin  = addSensor.AnalogPin ? 1 : 0;
                        query = $"INSERT INTO Sensor VALUES({productID}, '{addSensor.Type}', {addSensor.Voltage}, {digitalPin}, {analogPin}); SELECT SCOPE_IDENTITY()";
                        cmd   = new SqlCommand(query, conn);
                        dr    = cmd.ExecuteReader();
                        if (dr.HasRows)
                        {
                            conn.Close();
                            return(RedirectToAction("index", "admin"));
                        }
                    }
                    else
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(View());
        }
        public IActionResult Sensor()
        {
            ViewData["CustomerId"] = HttpContext.Session.Get <int>("_AccountId");
            ViewData["AdminId"]    = HttpContext.Session.Get <int>("_AdminId");
            AddSensorModel addSensor = new AddSensorModel();

            addSensor.Companies = new List <Company>();
            string connString = @"Data Source=DESKTOP-JM077BN;Initial Catalog=ArduinoStore;Integrated Security=True";

            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    string     query = $"SELECT * FROM Company ORDER BY name";
                    SqlCommand cmd   = new SqlCommand(query, conn);
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            int    companyID = int.Parse(dr["CompanyID"].ToString());
                            string name      = dr["Name"].ToString();
                            addSensor.Companies.Add(new Company {
                                CompanyID = companyID, Name = name
                            });
                        }
                        return(View(addSensor));
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(View());
        }
Ejemplo n.º 3
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"));
            }
        }