Example #1
0
        /// <summary>
        /// Creates account and performs the necessary checks defined by the paper.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void create_button_Click(object sender, EventArgs e)
        {
            //first check if everything is filled in
            if (!(string.IsNullOrEmpty(username_box.Text) && string.IsNullOrEmpty(user_id_box.Text) && string.IsNullOrEmpty(password_box.Text) && string.IsNullOrEmpty(password_again_box.Text) && string.IsNullOrEmpty(password_box.Text) && (usertype_combo.SelectedItem == null)))
            {
                //if everything is filled in, proceed with other checks
                //check if both passwords are the same
                if (password_box.Text == password_again_box.Text)
                {
                    //check if user id exists in db
                    using (var db = new Session1Entities1())
                    {
                        var query1 = (from u in db.Users
                                      where u.userId == user_id_box.Text
                                      select u).ToList();
                        if (query1.Count == 0)
                        {
                            //check if userid is equal to 8 chars or more.
                            if (user_id_box.Text.Length >= 8)
                            {
                                var user = new User()
                                {
                                    userId       = user_id_box.Text,
                                    userName     = username_box.Text,
                                    userPw       = password_again_box.Text,
                                    userTypeIdFK = (from t in db.User_Type where t.userTypeName == usertype_combo.SelectedItem.ToString() select t.userTypeId).First()
                                };
                                db.Users.Add(user);
                                await db.SaveChangesAsync();

                                this.Hide();
                                var form = new LoginPage();
                                form.Closed += (s, args) => this.Close();
                                form.Show();
                                MessageBox.Show("Added User!");
                            }
                            else
                            {
                                MessageBox.Show("UserID must be 8 characters or more!!");
                            }
                        }
                        else
                        {
                            MessageBox.Show("UserID is taken!!");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Passwords don't match!");
                }
            }
        }
 private async void Button2_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("This action cannot be undone!", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
     {
         using (var db = new Session1Entities1())
         {
             var selectedid = int.Parse(dataGridView1.Rows[0].Cells[0].Value.ToString());
             var s          = (from a in db.Resources
                               where a.resId == selectedid
                               select a).First();
             var allocs = (from a in db.Resource_Allocation
                           where a.resIdFK == selectedid
                           select a).ToList();
             db.Resources.Remove(s);
             foreach (var item in allocs)
             {
                 db.Resource_Allocation.Remove(item);
             }
             await db.SaveChangesAsync();
         }
         ReloadDGV();
     }
 }
Example #3
0
        /// <summary>
        /// Adds a new resource and its allocation, depending on if the checkbox for the different skills are checked.
        /// </summary>
        /// <returns></returns>
        public async Task AddResource()
        {
            using (var db = new Session1Entities1()) {
                //need to add resource and RA entries.
                int resourceid = 1;
                try
                {
                    resourceid = (from x in db.Resources orderby x.resId descending select x.resId).First() + 1;
                }
                catch
                {
                }
                var resource = new Resource()
                {
                    remainingQuantity = (int)numericUpDown1.Value,
                    resId             = resourceid,
                    resName           = resname_box.Text,
                    resTypeIdFK       = restype_combo.SelectedIndex + 1
                };
                db.Resources.Add(resource);
                if (CS.Checked)
                {
                    //try catch because it might error if there are no results found(database does not contain any values by default)
                    try
                    {
                        //try getting the largest id +1 as entity framework does not support auto incrementing.
                        var ra = new Resource_Allocation()
                        {
                            allocId   = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1,
                            resIdFK   = resourceid,
                            skillIdFK = 1
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                    catch
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = 1,
                            resIdFK   = resourceid,
                            skillIdFK = 1
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                }
                if (SSB.Checked)
                {
                    try
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1,
                            resIdFK   = resourceid,
                            skillIdFK = 2
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                    catch
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = 1,
                            resIdFK   = resourceid,
                            skillIdFK = 2
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                }
                if (NSA.Checked)
                {
                    try
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1,
                            resIdFK   = resourceid,
                            skillIdFK = 3
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                    catch
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = 1,
                            resIdFK   = resourceid,
                            skillIdFK = 3
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                }
                if (WT.Checked)
                {
                    try
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1,
                            resIdFK   = resourceid,
                            skillIdFK = 4
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                    catch
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = 1,
                            resIdFK   = resourceid,
                            skillIdFK = 4
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                }
                await db.SaveChangesAsync();

                MessageBox.Show("Done!");
                back_button_Click(null, null);
            }
        }
Example #4
0
        /// <summary>
        /// Update function, tries to update and check if the resource allocation already exists for the specific resource and will update, add and delete accordingly.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        ///
        public async Task updateDatabase(int id)
        {
            using (var db = new Session1Entities1())
            {
                var res = (from r in db.Resources
                           where r.resId == id
                           select r).First();
                var ras = (from ra in db.Resource_Allocation
                           where ra.resIdFK == id
                           select ra.skillIdFK).ToList();
                res.remainingQuantity = (int)numericUpDown1.Value;
                if (CS.Checked && !ras.Contains(1))
                {
                    try
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1,
                            resIdFK   = res.resId,
                            skillIdFK = 1
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                    catch
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = 1,
                            resIdFK   = res.resId,
                            skillIdFK = 1
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                }
                else if (!CS.Checked && ras.Contains(1))
                {
                    //sometimes .First() can return an error, hence put it in a try catch
                    try
                    {
                        var findra = (from a in db.Resource_Allocation
                                      where a.skillIdFK == 1
                                      where a.resIdFK == id
                                      select a).First();
                        db.Resource_Allocation.Remove(findra);
                    }
                    catch
                    {
                    }
                }
                if (SSB.Checked && !ras.Contains(2))
                {
                    try
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1,
                            resIdFK   = res.resId,
                            skillIdFK = 2
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                    catch
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = 1,
                            resIdFK   = res.resId,
                            skillIdFK = 2
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                }
                else if (!SSB.Checked && ras.Contains(2))
                {
                    //sometimes .First() can return an error, hence put it in a try catch
                    try
                    {
                        var findra = (from a in db.Resource_Allocation
                                      where a.skillIdFK == 2
                                      where a.resIdFK == id
                                      select a).First();
                        db.Resource_Allocation.Remove(findra);
                    }
                    catch
                    {
                    }
                }
                if (NSA.Checked && !ras.Contains(3))
                {
                    try
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1,
                            resIdFK   = res.resId,
                            skillIdFK = 3
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                    catch
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = 1,
                            resIdFK   = res.resId,
                            skillIdFK = 3
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                }
                else if (!NSA.Checked && ras.Contains(3))
                {
                    //sometimes .First() can return an error, hence put it in a try catch
                    try
                    {
                        var findra = (from a in db.Resource_Allocation
                                      where a.skillIdFK == 3
                                      where a.resIdFK == id
                                      select a).First();
                        db.Resource_Allocation.Remove(findra);
                    }
                    catch
                    {
                    }
                }
                if (WT.Checked && !ras.Contains(4))
                {
                    try
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1,
                            resIdFK   = res.resId,
                            skillIdFK = 4
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                    catch
                    {
                        var ra = new Resource_Allocation()
                        {
                            allocId   = 1,
                            resIdFK   = res.resId,
                            skillIdFK = 4
                        };
                        db.Resource_Allocation.Add(ra);
                    }
                }
                else if (!WT.Checked && ras.Contains(4))
                {
                    //sometimes .First() can return an error, hence put it in a try catch
                    try
                    {
                        var findra = (from a in db.Resource_Allocation
                                      where a.skillIdFK == 4
                                      where a.resIdFK == id
                                      select a).First();
                        db.Resource_Allocation.Remove(findra);
                    }
                    catch
                    {
                    }
                }
                await db.SaveChangesAsync();

                MessageBox.Show("Done");
            }
        }