/// <summary> /// Function called by login_button when clicked, handles login by: /// 1. Querying Database for matching username and password and selecting record. /// 2. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void login_button_Click(object sender, EventArgs e) { //check if fields are filled if (!(string.IsNullOrEmpty(user_id_box.Text) && string.IsNullOrEmpty(password_box.Text))) { //verify username and password using (var db = new Session1Entities1()) { var user = (from u in db.Users where u.userId == user_id_box.Text where u.userPw == password_box.Text select new { u, u.User_Type.userTypeId }).ToList(); //if count of users in the list is not zero, user exists. if (user.Count() > 0 && user.First().userTypeId == 2) { //username and password auth successful. //launch next form this.Hide(); var RMF = new ResourceManagementForm(); RMF.Closed += (s, args) => this.Close(); RMF.Show(); } else { MessageBox.Show("Invalid Username, Password or Unauthorised Access, this incident will be reported."); } } } else { MessageBox.Show("One or more fields are empty!!"); } }
public async Task UpdateUI(int id) { using (var db = new Session1Entities1()) { var res = (from r in db.Resources where r.resId == id select r).First(); resname_box.Text = res.resName; restype_combo.Text = res.Resource_Type.resTypeName; var alloc = (from a in db.Resource_Allocation where a.resIdFK == id select a).ToList(); numericUpDown1.Value = res.remainingQuantity; foreach (var item in alloc) { if (item.skillIdFK == 1) { CS.Checked = true; } else if (item.skillIdFK == 2) { SSB.Checked = true; } else if (item.skillIdFK == 3) { NSA.Checked = true; } else if (item.skillIdFK == 4) { WT.Checked = true; } } } }
public async Task <List <string> > GetSkills() { using (var db = new Session1Entities1()) { return((from s in db.Skills select s.skillName).ToList()); } }
public async Task <List <string> > GetType() { using (var db = new Session1Entities1()) { return((from s in db.Resource_Type select s.resTypeName).ToList()); } }
public CreateAccount() { InitializeComponent(); using (var db = new Session1Entities1()) { usertype_combo.DataSource = (from t in db.User_Type select t.userTypeName).ToList(); } }
/// <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(); } }
/// <summary> /// Get Resources from database to fill the default list, which will then be used to populate the datagridview. /// </summary> /// <param name="typefilter"> /// parameter to filter by type. /// </param> /// <returns></returns> public async Task <List <ResourceManagement> > GetResources(string typefilter = null, string skillfilter = null) { var returnlist = new List <ResourceManagement>(); using (var db = new Session1Entities1()) { var resources = (from r in db.Resources join rt in db.Resource_Type on r.resTypeIdFK equals rt.resTypeId select new { r, rt }).ToList(); if (typefilter != null) { resources = (from r in resources where r.rt.resTypeName == typefilter select r).ToList(); } foreach (var item in resources) { var skills = (from ra in db.Resource_Allocation where ra.resIdFK == item.r.resId select ra.Skill.skillName).ToList(); string allc = "Nil"; string quantity = "Not Available"; int noSkills = 0; if (skills.Count > 0) { allc = ""; foreach (var a in skills) { noSkills++; allc += $", {a}"; } } if (item.r.remainingQuantity > 5) { quantity = "Sufficient"; } else if (item.r.remainingQuantity < 5 && item.r.remainingQuantity > 1) { quantity = "Low Stock"; } if (skillfilter != null) { if (skills.Contains(skillfilter)) { returnlist.Add( new ResourceManagement() { ID = item.r.resId, Name = item.r.resName, Type = item.rt.resTypeName, NoSkills = noSkills, AllocatedSkills = allc, AvailableQuantity = quantity }); } } else { returnlist.Add( new ResourceManagement() { ID = item.r.resId, Name = item.r.resName, Type = item.rt.resTypeName, NoSkills = noSkills, AllocatedSkills = allc, AvailableQuantity = quantity }); } } } return(returnlist); }
/// <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); } }
/// <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"); } }