Ejemplo n.º 1
0
        public GroupResult Edit(int id, BGroup bGroup)
        {
            var groupResult = new GroupResult();

            using (_context = new GroupsManagementDbEntities())
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        Group group = _context.Groups.Find(id);
                        group.Name = bGroup.Name;
                        _context.SaveChanges();

                        transaction.Commit();

                        groupResult.bGroups.Add(new BGroup
                        {
                            GroupId = group.GroupId,
                            Name    = group.Name
                        });
                    }
                    catch (Exception e)
                    {
                        groupResult.IsValid = false;
                        groupResult.ErrorMessages.Add(e.Message);
                    }
                }
            }
            return(groupResult);
        }
Ejemplo n.º 2
0
        public RoleResult Add(BRole bRole)
        {
            RoleResult roleResult = new RoleResult();

            using (_context = new GroupsManagementDbEntities())
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        Role role = new Role()
                        {
                            RoleName = bRole.Name
                        };
                        _context.Roles.Add(role);
                        _context.SaveChanges();
                        transaction.Commit();
                        roleResult.bRoles.Add(Conversions.ConvertRoleToBRole(role));
                    }
                    catch (Exception e)
                    {
                        roleResult.IsValid = false;
                        roleResult.ErrorMessages.Add(e.Message);
                    }
                }
            }
            return(roleResult);
        }
Ejemplo n.º 3
0
        public RoleResult Edit(int id, BRole bRole)
        {
            RoleResult roleResult = new RoleResult();

            using (_context = new GroupsManagementDbEntities())
            {
                try
                {
                    Role role = _context.Roles.FirstOrDefault(r => r.RoleId == id);
                    if (role != null)
                    {
                        role.RoleName = bRole.Name;
                        _context.SaveChanges();
                        roleResult.bRoles.Add(Conversions.ConvertRoleToBRole(role));
                    }
                    else
                    {
                        roleResult.IsValid = false;
                        roleResult.ErrorMessages.Add("This role doesn't exist.");
                    }
                }
                catch (Exception e)
                {
                    roleResult.IsValid = false;
                    roleResult.ErrorMessages.Add(e.Message);
                }
            }
            return(roleResult);
        }
Ejemplo n.º 4
0
        public RoleResult Get(int id)
        {
            var roleResult = new RoleResult();

            using (_context = new GroupsManagementDbEntities())
            {
                try
                {
                    Role role = _context.Roles.FirstOrDefault(r => r.RoleId == id);
                    if (role != null)
                    {
                        roleResult.bRoles.Add(Conversions.ConvertRoleToBRole(role));
                    }
                    else
                    {
                        roleResult.IsValid = false;
                        roleResult.ErrorMessages.Add("Role not found");
                    }
                }
                catch (Exception e)
                {
                    roleResult.IsValid = false;
                    roleResult.ErrorMessages.Add(e.Message);
                }
            }
            return(roleResult);
        }
Ejemplo n.º 5
0
        public UserResult Delete(int id)
        {
            UserResult userResult = new UserResult();

            using (_context = new GroupsManagementDbEntities())
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        User  user  = _context.Users.Include("Group").FirstOrDefault(u => u.UserId == id);
                        BUser bUser = Conversions.ConvertUserToBUser(user);
                        bUser.GroupName = user.Group.Name;
                        userResult.bUsers.Add(bUser);

                        _context.Users.Remove(user);
                        _context.SaveChanges();
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        userResult.IsValid = false;
                        userResult.ErrorMessages.Add(e.Message);
                    }
                }
            }

            return(userResult);
        }
Ejemplo n.º 6
0
        public UserResult Edit(int id, BUser bUser)
        {
            UserResult userResult = new UserResult();

            using (_context = new GroupsManagementDbEntities())
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        User user = _context.Users.Find(id);
                        user.UserName = bUser.Name;
                        user.Email    = bUser.Email;
                        user.GroupId  = bUser.GroupId;
                        _context.SaveChanges();
                        transaction.Commit();

                        userResult.bUsers.Add(Conversions.ConvertUserToBUser(user));
                    }
                    catch (Exception e)
                    {
                        userResult.IsValid = false;
                        userResult.ErrorMessages.Add(e.Message);
                    }
                }
            }

            return(userResult);
        }
Ejemplo n.º 7
0
        public bool UserHasRole(string email, string roleName)
        {
            UserResult userResult = GetByEmail(email);

            using (_context = new GroupsManagementDbEntities())
            {
                if (userResult.IsValid)
                {
                    BUser bUser = userResult.bUsers.Single();
                    // get the user group
                    Group group = _context.Groups.FirstOrDefault(g => g.GroupId == bUser.GroupId);
                    if (group != null)
                    {
                        var groupRole = _context.GroupsRoles.
                                        Where(gr => gr.GroupId == group.GroupId).
                                        Where(gr => gr.Role.RoleName == roleName).FirstOrDefault();
                        if (groupRole != null)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 8
0
        public UserResult GetByEmail(string email)
        {
            UserResult userResult = new UserResult();

            using (_context = new GroupsManagementDbEntities())
            {
                try
                {
                    User user = _context.Users.FirstOrDefault(u => u.Email == email);
                    if (user != null)
                    {
                        userResult.bUsers.Add(Conversions.ConvertUserToBUser(user));
                    }
                    else
                    {
                        userResult.IsValid = false;
                        userResult.ErrorMessages.Add("There is no user with this email");
                    }
                }
                catch (Exception e)
                {
                    userResult.IsValid = false;
                    userResult.ErrorMessages.Add(e.Message);
                }
            }
            return(userResult);
        }
Ejemplo n.º 9
0
        public RoleResult GetAll()
        {
            var roleResult = new RoleResult();

            using (_context = new GroupsManagementDbEntities())
            {
                try
                {
                    var roles = _context.Roles.ToList().Select(r => Conversions.ConvertRoleToBRole(r));
                    roleResult.bRoles = roles.ToList();
                }
                catch (Exception e)
                {
                    roleResult.IsValid = false;
                    roleResult.ErrorMessages.Add(e.Message);
                }
            }
            return(roleResult);
        }
Ejemplo n.º 10
0
 public bool CheckPasswork(int userId, string password)
 {
     using (_context = new GroupsManagementDbEntities())
     {
         try
         {
             var user = _context.Users.First(u => u.UserId == userId);
             if (user.Password == password)
             {
                 return(true);
             }
         }
         catch (Exception e)
         {
             return(false);
         }
     }
     return(false);
 }
Ejemplo n.º 11
0
        public UserResult GetAll()
        {
            UserResult userResult = new UserResult();

            using (_context = new GroupsManagementDbEntities())
            {
                try
                {
                    IEnumerable <BUser> users = _context.Users.ToList().Select(u => Conversions.ConvertUserToBUser(u));
                    userResult.bUsers = users.ToList();
                }
                catch (Exception e)
                {
                    userResult.IsValid = false;
                    userResult.ErrorMessages.Add(e.Message);
                }
            }

            return(userResult);
        }
Ejemplo n.º 12
0
        public GroupResult GetAll()
        {
            var groupReslut = new GroupResult();

            using (_context = new GroupsManagementDbEntities())
            {
                try
                {
                    IList <BGroup> groups = _context.Groups.Select(g => new BGroup {
                        GroupId = g.GroupId, Name = g.Name
                    }).ToList();
                    groupReslut.bGroups = groups;
                }
                catch (Exception e)
                {
                    groupReslut.IsValid = false;
                    groupReslut.ErrorMessages.Add(e.Message);
                }
            }
            return(groupReslut);
        }
Ejemplo n.º 13
0
        public GroupResult Get(int id)
        {
            GroupResult groupResult = new GroupResult();

            using (_context = new GroupsManagementDbEntities())
            {
                try
                {
                    Group group = _context.Groups.Find(id);
                    groupResult.bGroups.Add(new BGroup()
                    {
                        GroupId = group.GroupId,
                        Name    = group.Name
                    });
                }
                catch (Exception e)
                {
                    groupResult.IsValid = false;
                    groupResult.ErrorMessages.Add(e.Message);
                }
            }
            return(groupResult);
        }
Ejemplo n.º 14
0
        public UserResult Add(BUser bUser)
        {
            UserResult userResult = new UserResult();

            using (_context = new GroupsManagementDbEntities())
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        // check if group exists
                        Group group = _context.Groups.FirstOrDefault(g => g.GroupId == bUser.GroupId);
                        if (group != null)
                        {
                            User user = _context.Users.Add(Conversions.ConvertBUserToUser(bUser));
                            _context.SaveChanges();
                            transaction.Commit();

                            _context.Entry(user).Reference(u => u.Group).Load();
                            BUser newBUser = Conversions.ConvertUserToBUser(user);
                            userResult.bUsers.Add(newBUser);
                        }
                        else
                        {
                            throw new Exception("Can't create the user because this group doesn't exist");
                        }
                    }
                    catch (Exception e)
                    {
                        userResult.IsValid = false;
                        userResult.ErrorMessages.Add(e.Message);
                    }
                }
            }
            return(userResult);
        }