Esempio n. 1
0
        public ActionResult Edit(CreateComputerViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Save primitive type to 'local' first without committing to DB.
                db.Computers.AddOrUpdate(model.Computer);

                // Get model back into context including dependencie objects (color,type,location) and primitive types saved above.
                ComputerModel computer = db.Computers.Single(x => x.Id == model.Computer.Id);
                computer.Type     = db.ComputerTypes.SingleOrDefault(x => x.Name == model.SelectedType);
                computer.Color    = db.Colors.SingleOrDefault(x => x.Name == model.SelectedColor);
                computer.Location = db.Locations.SingleOrDefault(x => x.Location == model.SelectedLocation);

                // Check friendly name
                if (model.Computer.Name == null)
                {
                    computer.Name = model.Computer.Hostname.ToUpper();
                }

                db.Computers.AddOrUpdate(computer);
                db.SaveChanges();

                return(RedirectToAction("Index", "Computer"));
            }

            // If NOT valid
            // Send lists of items for dropdowns.
            ViewBag.Colors    = db.Colors.ToList();
            ViewBag.Locations = db.Locations.ToList();
            ViewBag.Types     = db.ComputerTypes.ToList();
            // Return model with errors.
            return(View(model));
        }
        /// <summary>
        /// This auto-generates the form to add a computer to the database based on the Computer model
        /// </summary>
        /// <returns></returns>
        // GET: Computer/Create
        public ActionResult Create()
        {
            //Creates a new instance based on the view model
            CreateComputerViewModel computerViewModel = new CreateComputerViewModel
                                                            (_config.GetConnectionString("DefaultConnection"));

            //Pass it to the view
            return(View(computerViewModel));
        }
Esempio n. 3
0
        public ActionResult CreateMultiple()
        {
            // Send lists of items for dropdowns.
            ViewBag.Colors    = db.Colors.ToList();
            ViewBag.Locations = db.Locations.ToList();
            ViewBag.Types     = db.ComputerTypes.ToList();
            CreateComputerViewModel model = new CreateComputerViewModel();

            return(View(model));
        }
Esempio n. 4
0
        // GET: Computer/Create
        //NEW FORM
        public ActionResult Create()
        {
            // Create a new instance of a CreateComputerViewModel
            // If we want to get the employees, we need to use the constructor that's expecting a connection string.
            // When we create this instance, the constructor will run and get the employees.
            CreateComputerViewModel computerViewModel = new CreateComputerViewModel(_config.GetConnectionString("DefaultConnection"));

            // Once we've created it, we can pass it to the view
            return(View(computerViewModel));
        }
        public ActionResult Create(CreateComputerViewModel computerViewModel)
        {
            try
            {
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = @"INSERT INTO Computer (make, manufacturer, purchaseDate, decomissionDate)
                                                OUTPUT INSERTED.Id
                                                VALUES (@make, @manufacturer, @purchaseDate, null)";
                        cmd.Parameters.Add(new SqlParameter("@make", computerViewModel.computer.Make));
                        cmd.Parameters.Add(new SqlParameter("@manufacturer", computerViewModel.computer.Manufacturer));
                        cmd.Parameters.Add(new SqlParameter("@purchaseDate", computerViewModel.computer.PurchaseDate));

                        int newId = (int)cmd.ExecuteScalar();
                        computerViewModel.computer.Id = newId;

                        /// <summary>If the user selects an employee to whom they want to assign the computer, assign and unassign old</summary>
                        if (computerViewModel.employeeId != 0)
                        {
                            cmd.CommandText = @"INSERT INTO ComputerEmployee (EmployeeId, ComputerId, AssignDate, UnassignDate)
                                                OUTPUT INSERTED.Id
                                                VALUES (@employeeId, @computerId, @assignDate, null)";
                            cmd.Parameters.Add(new SqlParameter("@employeeId", computerViewModel.employeeId));
                            cmd.Parameters.Add(new SqlParameter("@computerId", newId));
                            cmd.Parameters.Add(new SqlParameter("@assignDate", DateTime.Now));


                            int newCEId = (int)cmd.ExecuteScalar();

                            cmd.CommandText = @"UPDATE ComputerEmployee SET UnassignDate = @unassignDate WHERE employeeID = @employeeId AND computerId != @computerId";

                            cmd.Parameters.Add(new SqlParameter("@unassignDate", DateTime.Now));

                            cmd.ExecuteScalar();
                        }


                        return(RedirectToAction(nameof(Index)));
                    }
                }
            }
            catch
            {
                return(View());
            }
        }
Esempio n. 6
0
        public ActionResult CreateMultiple(CreateComputerViewModel model)
        {
            // Get models from dropdowns
            model.Computer.Type     = db.ComputerTypes.SingleOrDefault(x => x.Name == model.SelectedType);
            model.Computer.Color    = db.Colors.SingleOrDefault(x => x.Name == model.SelectedColor);
            model.Computer.Location = db.Locations.SingleOrDefault(x => x.Location == model.SelectedLocation);

            if (model.Computer.Type == null || model.Computer.Color == null || model.Computer.Location == null)
            {
                ModelState.AddModelError("dependency", "Type, color or location could not be found!");
            }

            // Get array of computers to create
            string[] hostnames = model.Computer.Hostname.Split(',');

            // Set Last Seen
            model.Computer.LastSeen = DateTime.Now;

            if (ModelState.IsValid)
            {
                // Save to db if valid
                foreach (string hostname in hostnames)
                {
                    // Make sure name/hostname is OK.
                    string cname = hostname.Replace(" ", "").Replace(Environment.NewLine, "");
                    model.Computer.Hostname = cname.ToLower();
                    model.Computer.Name     = cname.ToUpper();
                    if (model.Computer.Hostname.Length > 0 && model.Computer.Name.Length > 0)
                    {
                        db.Computers.Add(model.Computer);
                        db.SaveChanges();
                    }
                }


                return(RedirectToAction("Index", "Computer"));
            }

            // If NOT valid
            // Send lists of items for dropdowns.
            ViewBag.Colors    = db.Colors.ToList();
            ViewBag.Locations = db.Locations.ToList();
            ViewBag.Types     = db.ComputerTypes.ToList();
            // Return model with errors.
            return(View(model));
        }
Esempio n. 7
0
        public ActionResult Edit(int id)
        {
            // Send lists of items for dropdowns.
            ViewBag.Colors    = db.Colors.ToList();
            ViewBag.Locations = db.Locations.ToList();
            ViewBag.Types     = db.ComputerTypes.ToList();
            // Create view model and add model
            CreateComputerViewModel model = new CreateComputerViewModel();

            //Replace line breaks with HTML code.
            //model.Computer.Description?.Replace(Environment.NewLine, "<br/>");

            model.Computer         = db.Computers.SingleOrDefault(x => x.Id == id);
            model.SelectedType     = model.Computer.Type.ToString();
            model.SelectedColor    = model.Computer.Color.ToString();
            model.SelectedLocation = model.Computer.Location.ToString();
            return(View(model));
        }
Esempio n. 8
0
        public ActionResult Create(CreateComputerViewModel model)
        {
            // Get models from dropdowns
            model.Computer.Type     = db.ComputerTypes.SingleOrDefault(x => x.Name == model.SelectedType);
            model.Computer.Color    = db.Colors.SingleOrDefault(x => x.Name == model.SelectedColor);
            model.Computer.Location = db.Locations.SingleOrDefault(x => x.Location == model.SelectedLocation);

            if (model.Computer.Type == null || model.Computer.Color == null || model.Computer.Location == null)
            {
                ModelState.AddModelError("dependency", "Type, color or location could not be found!");
            }

            // Make sure hostname looks ok!
            string hostname = model.Computer.Hostname.Replace(" ", "").Replace(Environment.NewLine, "");

            model.Computer.Hostname = hostname.ToLower();

            // Set Last Seen
            model.Computer.LastSeen = DateTime.Now;

            // Set the friendly name if not supplied.
            if (model.Computer.Name == null)
            {
                model.Computer.Name = model.Computer.Hostname.ToUpper();
            }

            if (ModelState.IsValid)
            {
                // Save to db if valid
                db.Computers.Add(model.Computer);
                db.SaveChanges();
                return(RedirectToAction("Index", "Computer"));
            }

            // If NOT valid
            // Send lists of items for dropdowns.
            ViewBag.Colors    = db.Colors.ToList();
            ViewBag.Locations = db.Locations.ToList();
            ViewBag.Types     = db.ComputerTypes.ToList();
            // Return model with errors.
            return(View(model));
        }
Esempio n. 9
0
        public ActionResult Create(CreateComputerViewModel computerViewModel)
        {
            {
                int newId = 0;
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = @"INSERT INTO Computer
                ( Make, Manufacturer, PurchaseDate)
                 OUTPUT INSERTED.Id
                VALUES
                ( @Make, @Manufacturer, @PurchaseDate)";

                        cmd.Parameters.Add(new SqlParameter("@Make", computerViewModel.computer.Make));
                        cmd.Parameters.Add(new SqlParameter("@Manufacturer", computerViewModel.computer.Manufacturer));
                        cmd.Parameters.Add(new SqlParameter("@PurchaseDate", computerViewModel.computer.PurchaseDate));
                        newId = (int)cmd.ExecuteScalar();


                        // if an employee is assigned insert an entry into the computeremployee table
                        if (computerViewModel.assignedEmployee.Id != 0)
                        {
                            cmd.CommandText = @"INSERT INTO ComputerEmployee
                (ComputerId, EmployeeId, AssignDate)
                 VALUES
                  (@ComputerId, @EmployeeId, @AssignDate) ";

                            cmd.Parameters.Add(new SqlParameter("@ComputerId", newId));
                            cmd.Parameters.Add(new SqlParameter("@EmployeeId", computerViewModel.assignedEmployee.Id));
                            cmd.Parameters.Add(new SqlParameter("@AssignDate", DateTime.Now));

                            cmd.ExecuteNonQuery();
                        }
                        return(RedirectToAction(nameof(Index)));
                    }
                }
            }
        }
Esempio n. 10
0
        // GET: Computer/Create
        public ActionResult Create()
        {
            CreateComputerViewModel computerViewModel = new CreateComputerViewModel(_config.GetConnectionString("DefaultConnection"));

            return(View(computerViewModel));
        }