public bool Delete(string Id, ref string msg) { bool result = true; using (NuWebContext cxt = new NuWebContext()) { try { var isExist = (from mp in cxt.G_ModulePermission from ro in cxt.G_RoleOrganization from ros in cxt.G_RoleOnStore from ur in cxt.G_UserRole where mp.RoleID.Equals(ro.Id) && ros.RoleId.Equals(ro.Id) && ur.RoleID.Equals(ro.Id) && ro.Id.Equals(Id) select new { mp, ro, ros, ur }).FirstOrDefault(); if (isExist != null) { result = false; msg = "This role is already in use and unable to be deleted."; } else { string msgChild = ""; _MPFactory.Delete(Id, ref msgChild); _RoSfactory.Delete(Id, ref msgChild); G_RoleOrganization itemDelete = (from tb in cxt.G_RoleOrganization where tb.Id == Id select tb).FirstOrDefault(); cxt.G_RoleOrganization.Remove(itemDelete); cxt.SaveChanges(); //========= } } catch (Exception ex) { _logger.Error(ex); result = false; } finally { if (cxt != null) { cxt.Dispose(); } } } return(result); }
public bool Insert(RoleOrganizationModels model, ref string msg) { bool result = true; using (NuWebContext cxt = new NuWebContext()) { try { var itemExsit = cxt.G_RoleOrganization.Any(x => x.Name.ToLower().Equals(model.Name.ToLower()) && x.OrganizationId == model.OrganizationId); if (itemExsit) { result = false; msg = "Role's name [" + model.Name + "] is duplicated"; } else { string RoleID = Guid.NewGuid().ToString(); string msgChild = ""; G_RoleOrganization item = new G_RoleOrganization(); item.Id = RoleID; item.Name = model.Name; item.IsActive = model.IsActive; item.OrganizationId = model.OrganizationId; item.CreatedDate = DateTime.Now; item.CreatedUser = model.CreatedUser; item.ModifiedDate = DateTime.Now; item.ModifiedUser = model.ModifiedUser; cxt.G_RoleOrganization.Add(item); cxt.SaveChanges(); //===========Get List Module Permission GetListModulePermission(model.ListModule, ""); ListModPer.ForEach(x => { x.Id = ""; x.RoleID = RoleID; x.CreatedDate = DateTime.Now; x.CreatedUser = model.CreatedUser; x.ModifiedDate = DateTime.Now; x.ModifiedUser = model.ModifiedUser; x.Status = (byte)Commons.EStatus.Actived; }); _MPFactory.InsertOrUpdate(ListModPer, ref msgChild); //=========== Get List Approval Store List <RoleOnStoreModels> listRoStore = new List <RoleOnStoreModels>(); foreach (var roleonStore in model.ListStore) { listRoStore.Add(new RoleOnStoreModels { CreatedDate = DateTime.Now, CreatedUser = model.CreatedUser, ModifiedUser = model.ModifiedUser, ModifiedDate = DateTime.Now, Id = roleonStore.Id, IsActive = roleonStore.Checked, StoreId = roleonStore.StoreId, RoleId = RoleID }); } _RoSfactory.InsertOrUpdate(listRoStore, ref msgChild); } } catch (Exception ex) { _logger.Error(ex); result = false; } finally { if (cxt != null) { cxt.Dispose(); } } } return(result); }