Ejemplo n.º 1
0
        public MessageResultDto AddUser(string firstname, string lastname, string nickname, string username, string password, Guid groupId)
        {
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<Group> groupRepository = new Repository<Group>(session);
                var group = groupRepository.FindOne(x => x.Id == groupId);

                IRepository<User> userRepository = new Repository<User>(session);
                var existingUser = userRepository.FindOne(x => x.Username == username);

                if (null == existingUser)
                {
                    userRepository.Add(new User
                    {
                        FirstName = firstname,
                        LastName = lastname,
                        Nickname = nickname,
                        Username = username,
                        Password = password,
                        Group = group
                    });

                    transaction.Commit();
                }
                else
                {
                    result.ErrorMessage("User with same username already exists");
                }
            }
            return result;
        }
Ejemplo n.º 2
0
        public MessageResultDto AddGroup(string name)
        {
            Group group = null;
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<Group> groupRepository = new Repository<Group>(session);
                var existingGroup = groupRepository.FindOne(new Tuple<Expression<Func<Group, object>>, string>(x => x.Name, name));

                if (null == existingGroup)
                {
                    group = new Group { Name = name };
                    groupRepository.Add(group);
                    transaction.Commit();
                    result.SuccessMessage("Group added successfully", group.Id);
                }
                else
                {
                    result.ErrorMessage("Group with same name already exisits");
                }
            }
            return result;
        }
Ejemplo n.º 3
0
        public MessageResultDto AddGroup(string name)
        {
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<Group> groupRepository = new Repository<Group>(session);
                groupRepository.Add(new Group { Name = name });
                transaction.Commit();
            }
            return result;
        }
Ejemplo n.º 4
0
        public MessageResultDto UpdateGroup(Guid id, string name)
        {
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<Group> groupRepository = new Repository<Group>(session);
                var group = groupRepository.FindOne(x => x.Id == id);
                group.Name = name;
                groupRepository.Update(group);
                transaction.Commit();
            }
            return result;
        }
Ejemplo n.º 5
0
        public MessageResultDto DeleteUser(Guid id)
        {
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<User> userRepository = new Repository<User>(session);
                var user = userRepository.FindOne(id);
                if (user.Accounts.Count > 0)
                {
                    result.ErrorMessage("User has got accounts registered. Please remove all accounts first");
                }
                else
                {
                    userRepository.Delete(id);
                    transaction.Commit();
                    result.Message = "User deleted successfully";
                }
            }
            return result;
        }
Ejemplo n.º 6
0
        public MessageResultDto DeleteGroup(Guid id)
        {
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<Group> groupRepository = new Repository<Group>(session);
                var group = groupRepository.FindOne(id);
                if (group.Users.Count > 0)
                {
                    result.ErrorMessage("Group has got users registered. Please remove the user(s) from the group first.");
                }
                else
                {
                    groupRepository.Delete(id);
                    transaction.Commit();
                    result.Message = "Group deleted successfully";
                }
            }
            return result;
        }
Ejemplo n.º 7
0
        public MessageResultDto AddUser(string firstname, string lastname, string nickname, string username, Guid groupId)
        {
            User user;
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<Group> groupRepository = new Repository<Group>(session);
                var group = groupRepository.FindOne(x => x.Id == groupId);

                IRepository<User> userRepository = new Repository<User>(session);
                var existingUser = userRepository.FindOne(new Tuple<Expression<Func<User, object>>, string>(x => x.Username, username));

                if (null == existingUser)
                {
                    user = new User
                    {
                        FirstName = firstname,
                        LastName = lastname,
                        Nickname = nickname,
                        Username = username,
                        Password = Constants.DEFAULT_PASSWORD,
                        Group = group
                    };
                    userRepository.Add(user);
                    transaction.Commit();
                    result.SuccessMessage("User added successfully", user.Id);
                }
                else
                {
                    result.ErrorMessage("User with same username already exists");
                }
            }
            return result;
        }
Ejemplo n.º 8
0
        public MessageResultDto UpdateUser(Guid id, string firstname, string lastname, string nickname, string username, string password, Guid groupId)
        {
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<User> userRepository = new Repository<User>(session);
                var user = userRepository.FindOne(x => x.Id == id);

                if (user.Group.Id == groupId)
                {
                    IRepository<Group> groupRepository = new Repository<Group>(session);
                    var group = groupRepository.FindOne(x => x.Id == groupId);
                    user.Group = group;
                }

                user.FirstName = firstname;
                user.LastName = lastname;
                user.Nickname = nickname;
                user.Username = username;
                user.Password = password;

                userRepository.Add(user);

                transaction.Commit();
            }
            return result;
        }
Ejemplo n.º 9
0
        public MessageResultDto ResetPassword(Guid id)
        {
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<User> userRepository = new Repository<User>(session);
                var user = userRepository.FindOne(id);
                user.Password = Constants.DEFAULT_PASSWORD;
                transaction.Commit();
                result.Message = "Password has been reset successfully";
            }
            return result;
        }
Ejemplo n.º 10
0
        public MessageResultDto LockedStatus(Guid id, bool isLocked)
        {
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<User> userRepository = new Repository<User>(session);
                var user = userRepository.FindOne(id);
                user.IsLocked = isLocked ;
                transaction.Commit();
                result.Message = string.Format("User has been {0} successsfully", isLocked ? "locked" : "unlocked");
            }
            return result;
        }
Ejemplo n.º 11
0
        public MessageResultDto UpdateGroup(Guid id, string name)
        {
            var result = new MessageResultDto();

            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                IRepository<Group> groupRepository = new Repository<Group>(session);
                var existingGroup = groupRepository.FindOne(x => x.Id != id, new Tuple<Expression<Func<Group, object>>, string>(x => x.Name, name));

                if (null == existingGroup)
                {
                    var group = groupRepository.FindOne(x => x.Id == id);
                    group.Name = name;
                    groupRepository.Update(group);
                    transaction.Commit();
                    result.SuccessMessage("Group updated successfully", group.Id);
                }
                else
                {
                    result.ErrorMessage("Group with same name already exisits");
                }
            }
            return result;
        }