Esempio n. 1
0
        public void CreateProject(int userId, string key, string name)
        {
            if (!Regex.IsMatch(key, "\\b[A-Z0-9]{2,5}\\b"))
            {
                throw new CheckedException("Key must contains only uppercase alphabetical letters or digits, and must have at least 2 and at most 5 characters.");
            }

            if (_projectDAO.CheckKey(key))
            {
                throw new CheckedException($"Key {key} has already been used.");
            }

            var user = _userDAO.GetUserById(userId);

            if (user == null)
            {
                throw new CheckedException("User does not exist");
            }

            if (user.Permission != Permission.OWNER)
            {
                throw new CheckedException("You don't have permission to create a project");
            }

            Project project = new Project
            {
                Key         = key,
                Name        = name,
                CreatedUser = _userDAO.GetUserById(userId),
                Members     = new ObservableCollection <Member>()
            };

            _projectDAO.InsertProject(project);

            Member member = new Member
            {
                User = user,
                Role = Role.OWNER
            };

            _projectDAO.InsertMember(project.Id, member);
        }