示例#1
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);
        }
示例#2
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);
        }
示例#3
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);
        }
示例#4
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);
        }
示例#5
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);
        }