using System; using System.Collections.Generic; using System.Windows.Forms; using System.Linq; using Tanshu.Accounts.Entities.Auth; using Tanshu.Accounts.Repository; namespace Tanshu.Accounts.PointOfSale { public partial class AssignGroupRoles : Form { public AssignGroupRoles() { InitializeComponent(); } private void AssignGroupRoles_Load(object sender, EventArgs e) { FillGroups(); } private void FillGroups() { cmbGroup.DisplayMember = "Name"; cmbGroup.ValueMember = "GroupID"; using (var bi = new GroupBI()) cmbGroup.DataSource = bi.List(); } private void cmbGroup_SelectedIndexChanged(object sender, EventArgs e) { if (cmbGroup.SelectedValue == null) GetRoles(null); else GetRoles((Guid)cmbGroup.SelectedValue); } private void GetRoles(Guid? groupID) { using (var bi = new GroupBI()) { var roles = bi.RoleList(); clbRoles.DataSource = roles; for (int i = 0; i < clbRoles.Items.Count; i++) { clbRoles.SetItemChecked(i, false); } if (groupID.HasValue) { var group = bi.Get(x => x.GroupID == groupID.Value); for (int i = 0; i < clbRoles.Items.Count; i++) { var item = (Role)clbRoles.Items[i]; if (item.Groups.Any(x => x.Group == group)) { clbRoles.SetItemChecked(i, true); } } } } } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void btnOk_Click(object sender, EventArgs e) { if (cmbGroup.SelectedValue != null) Save((Guid)cmbGroup.SelectedValue); } private void Save(Guid groupID) { using (var bi = new GroupBI()) { var group = bi.Get(x => x.GroupID == groupID); bi.Update(group, clbRoles.CheckedItems.OfType()); bi.SaveChanges(); } MessageBox.Show("Update / Save Successful"); } private void clbRoles_Format(object sender, ListControlConvertEventArgs e) { e.Value = ((Role)e.ListItem).Name; } } }