private void buttonApply_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var selectedErrorName = this.dropDownErrors.SelectedItem?.ToString();
                if (selectedErrorName == null)
                {
                    throw new Exception("No error was selected");
                }

                using (var db = new DatabaseCon())
                {
                    var error = this._createObjectFromData(db);
                    if (selectedErrorName == NEW_ITEM_TEXT)
                    {
                        this._enforceMneumonicIsUnique(db, error.error_code_mneumonic);
                        this._enforceErrorCodeIsUnique(db, error.error_code1);

                        this._window.updateStatus($"Adding new error called '{error.error_code_mneumonic}' to the database");
                        db.error_code.Add(error);
                        db.SaveChanges();
                    }
                    else
                    {
                        this._window.updateStatus($"Updating error called '{selectedErrorName}'");

                        var dbError = db.error_code.SingleOrDefault(er => er.error_code_mneumonic == selectedErrorName);

                        // If we're changing the mneumonic/error code, make sure it's fine to do.
                        if (dbError.error_code1 != error.error_code1)
                        {
                            this._enforceErrorCodeIsUnique(db, error.error_code1);
                        }

                        if (dbError.error_code_mneumonic != error.error_code_mneumonic)
                        {
                            this._enforceMneumonicIsUnique(db, error.error_code_mneumonic);
                        }

                        dbError.application_ids      = error.application_ids;
                        dbError.default_severity_id  = error.default_severity_id;
                        dbError.device_ids           = error.device_ids;
                        dbError.error_code1          = error.error_code1;
                        dbError.error_code_mneumonic = error.error_code_mneumonic;
                        dbError.narrative            = error.narrative;
                        db.SaveChanges();
                    }
                }

                this._updateDropdowns();
            }
            catch (Exception ex)
            {
                this._window.updateStatus($"[ERROR] {ex.ToString()}");
            }
        }
        public ActionResult Create([Bind(Include = "ID,roleDes")] Role role)
        {
            if (Session["user"] == null)
            {
                return(View("Index", "Home"));
            }
            if (ModelState.IsValid)
            {
                db.Roles.Add(role);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(role));
        }
        public ActionResult Create([Bind(Include = "ID,firstName,lastName,telephoneNo,role,email,address1,city,country,lat,lon")] Employee employee)
        {
            if (Session["user"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return(RedirectToAction("Employees"));
            }

            ViewBag.role = new SelectList(db.Roles, "ID", "roleDes", employee.role);
            return(View(employee));
        }
        private void _addLanguage(DatabaseCon db, string name, string path)
        {
            this._window.updateStatus($"Adding new language '{name}'");
            db.languages.Add(
                new language()
            {
                description       = name,
                path_to_templates = path
            });
            db.SaveChanges();

            this._updateLanguages();
        }
        private void _addDevice(DatabaseCon db, string name, byte bitIndex)
        {
            this._window.updateStatus($"Adding new device '{name}'");
            db.device_type.Add(
                new device_type()
            {
                bit_index   = bitIndex,
                description = name
            });
            db.SaveChanges();

            this._updateDevices();
        }
示例#6
0
        // The reason this function is in ViewHelper, instead of DataHelper, is because of the user-interaction is requires, as well as the requirement of the MainWindow, making it specialised for use in views.
        /// <summary>
        /// Deletes a `T` from the database that has a description matching the given description.
        /// (note: in a lot of cases the description is just a name, hence 'name_description')
        ///
        /// This function will first prompt the user with a message box, asking them to confirm the deletion of the object.
        ///
        /// It is a requirement that `T` has a field called `description`.
        /// </summary>
        /// <typeparam name="T">The database type to remove (and also determines the database table that's used)</typeparam>
        /// <param name="window">The main window, so it's status can be updated</param>
        /// <param name="name_description">The name/description of the object to remove</param>
        /// <returns>True if something was delete.</returns>
        public static bool deleteTByDescription <T>(MainWindow window, string name_description) where T : class
        {
            if (window == null)
            {
                throw new ArgumentNullException("window");
            }

            if (name_description == null)
            {
                throw new ArgumentNullException("name_description");
            }

            var TName  = typeof(T).Name;
            var result = System.Windows.Forms.MessageBox.Show($"Are you sure you want to remove the {TName} '{name_description}'?",
                                                              "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.No)
            {
                window.updateStatus($"Not going through with removal of the {TName}");
                return(false);
            }

            using (var db = new DatabaseCon())
            {
                var set   = db._getSetFromT <T>();
                T   value = null;

                // I need to use dynamic, so can't use SingleOrDefault since it doesn't like lambdas apparently
                foreach (var val in set)
                {
                    dynamic dyVal = val;
                    if (dyVal.description == name_description)
                    {
                        value = val;
                        break;
                    }
                }

                if (value == null)
                {
                    window.updateStatus($"Can't delete a(n) {TName} that doesn't exist");
                    return(false);
                }

                window.updateStatus($"Removing {TName} '{name_description}' from the database");
                set.Remove(value);
                db.SaveChanges();
            }

            return(true);
        }
示例#7
0
        private void _addApplication(DatabaseCon db, string name, byte bitIndex, string path)
        {
            this._window.updateStatus($"Adding new application '{name}'");
            db.applications.Add(
                new application()
            {
                bit_index           = bitIndex,
                description         = name,
                path_to_output_file = path
            });
            db.SaveChanges();

            this._updateApplications();
        }
        private void _updateLanguage(DatabaseCon db, string name, string path)
        {
            this._window.updateStatus($"Updating existing language '{name}'");
            var oldName = this.dropDownLanguages.SelectedItem.ToString();

            var lang = db.languages.SingleOrDefault(l => l.description == oldName);

            if (lang == null)
            {
                throw new Exception($"For some reason the language {oldName} no longer exists in the database.");
            }

            lang.description       = name;
            lang.path_to_templates = path;
            db.SaveChanges();

            this._updateLanguages();
        }
        private void _updateDevice(DatabaseCon db, string name, byte bitIndex)
        {
            this._window.updateStatus($"Updating existing device '{name}'");
            var oldName = this.dropDownDevices.SelectedItem.ToString();

            var device = db.device_type.SingleOrDefault(d => d.description == oldName);

            if (device == null)
            {
                throw new Exception($"For some reason the device {oldName} no longer exists in the database.");
            }

            device.description = name;
            device.bit_index   = bitIndex;
            db.SaveChanges();

            this._updateDevices();
        }
        private void buttonDeleteError_Click(object sender, RoutedEventArgs e)
        {
            // Again, since error_code doesn't use 'description' but rather 'error_code_mneumonic' I can't use ViewHelper.deleteTByDescription for this.
            // So I've pretty much just copied to code into here and tweaked it a bit.
            var window           = this._window;
            var name_description = this.dropDownErrors.SelectedItem?.ToString();

            if (window == null)
            {
                throw new ArgumentNullException("window");
            }

            if (name_description == null)
            {
                throw new ArgumentNullException("name_description");
            }

            var TName  = "error_code";
            var result = System.Windows.Forms.MessageBox.Show($"Are you sure you want to remove the {TName} '{name_description}'?",
                                                              "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.No)
            {
                window.updateStatus($"Not going through with removal of the {TName}");
                return;
            }

            using (var db = new DatabaseCon())
            {
                var set   = db.error_code;
                var value = db.error_code.SingleOrDefault(v => v.error_code_mneumonic == name_description);

                if (value == null)
                {
                    window.updateStatus($"Can't delete an {TName} that doesn't exist");
                    return;
                }

                window.updateStatus($"Removing {TName} '{name_description}' from the database");
                set.Remove(value);
                db.SaveChanges();
                this._updateDropdowns();
            }
        }
示例#11
0
        private void _updateApplication(DatabaseCon db, string name, byte bitIndex, string path)
        {
            this._window.updateStatus($"Updating existing application '{name}'");
            var oldName = this.dropDownApplications.SelectedItem.ToString();

            var app = db.applications.SingleOrDefault(a => a.description == oldName);

            if (app == null)
            {
                throw new Exception($"For some reason the application {oldName} no longer exists in the database.");
            }

            app.description         = name;
            app.path_to_output_file = path;
            app.bit_index           = bitIndex;
            db.SaveChanges();

            this._updateApplications();
        }