Exemplo n.º 1
0
 public static bool Save(Role role)
 {
     try
     {
         using (var context = new PrinterMonitorDBEntities())
         {
             context.Roles.Add(role);
             context.SaveChanges();
         }
         return true;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 2
0
 public static bool Save(Role role, out string message)
 {
     try
     {
         if (RoleDL.RoleExists(role))
         {
             message = string.Format("Role with name: {0} exists already", role.Name);
             return false;
         }
         else
         {
             message = string.Empty;
             return RoleDL.Save(role);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 3
0
        public static bool RoleExists(Role role)
        {
            try
            {
                var existingRole = new Role();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingRole = context.Roles
                                    .Where(t => t.Name.Equals(role.Name))
                                    .FirstOrDefault();
                }

                if (existingRole == null)
                    return false;
                else
                    return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 4
0
        public static bool Update(Role role)
        {
            try
            {
                Role existingRole = new Role();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingRole = context.Roles
                                    .Where(t => t.ID == role.ID)
                                    .FirstOrDefault();
                }

                if (existingRole != null)
                {
                    existingRole.Name = role.Name;

                    using (var context = new PrinterMonitorDBEntities())
                    {
                        //Transaction block
                        using (var transaction = context.Database.BeginTransaction())
                        {
                            try
                            {
                                //Modifying just the property details
                                context.Entry(existingRole).State = EntityState.Modified;
                                context.SaveChanges();

                                //Delete existing role function of the role
                                IEnumerable<RoleFunction> existingRoleFunctions = context.RoleFunctions.Include("Role")
                                                                .Where(t => existingRole.ID.Equals(t.RoleID))
                                                                .ToList();

                                if (existingRoleFunctions != null && existingRoleFunctions.ToList().Count != 0)
                                {
                                    context.RoleFunctions.RemoveRange(existingRoleFunctions);
                                    context.SaveChanges();
                                }

                                //Adding new Role Functions
                                List<RoleFunction> newRoleFunctions = new List<RoleFunction>();
                                foreach (RoleFunction function in role.RoleFunctions)
                                {
                                    RoleFunction roleFunction = new RoleFunction();
                                    roleFunction.RoleID = existingRole.ID;
                                    roleFunction.FunctionID = function.FunctionID;

                                    newRoleFunctions.Add(roleFunction);
                                }
                                if (newRoleFunctions != null && newRoleFunctions.Count != 0)
                                {
                                    context.RoleFunctions.AddRange(newRoleFunctions);
                                    context.SaveChanges();
                                }

                                //commit changes
                                transaction.Commit();
                            }
                            catch (Exception ex)
                            {
                                transaction.Rollback();
                                throw ex;
                            }
                        }

                    }

                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 5
0
 public static bool Update(Role role)
 {
     try
     {
         return RoleDL.Update(role);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }