Exemple #1
0
 public static bool Save(Role role)
 {
     try
     {
         using (var context = new KioskWebDBEntities())
         {
             context.Roles.Add(role);
             context.SaveChanges();
         }
         return true;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemple #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;
     }
 }
Exemple #3
0
        public static bool RoleExists(Role role)
        {
            try
            {
                var existingRole = new Role();
                using (var context = new KioskWebDBEntities())
                {
                    existingRole = context.Roles
                                    .Where(t => t.Name.Equals(role.Name))
                                    .FirstOrDefault();
                }

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

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

                    using (var context = new KioskWebDBEntities())
                    {
                        //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;
            }
        }