/// <summary>
        ///     Validates budget access rights
        /// </summary>
        /// <param name="budgetAccessRight">Budget access right to validate</param>
        /// <returns>True if budget access right is valid, false otherwise</returns>
        public void Validate(BudgetAccessRight budgetAccessRight)
        {
            if (budgetAccessRight == null)
                throw new ArgumentNullException(nameof(budgetAccessRight));

            this._validator.ValidateAndThrowCustomException(budgetAccessRight);
        }
        public void ValidateBudgetBudgetAccessRight_ValidBudgetAccessRight_ReturnsTrue()
        {
            var budgetAccessRight = new BudgetAccessRight
            {
                Budget = new Budget(),
                Permission = PermissionEnum.Read,
                UserProfile = new UserProfile()
            };

            var budgetAccessRightService = new BudgetAccessRightService(ProvidersFactory.GetNewBudgetsProviders());
            Assert.DoesNotThrow(() => budgetAccessRightService.Validate(budgetAccessRight));
        }
        public void ValidateBudgetBudgetAccessRight_NullUserProfile_ThrowException()
        {
            var budgetAccessRight = new BudgetAccessRight
            {
                Budget = new Budget(),
                Permission = PermissionEnum.Read,
                UserProfile = null
            };

            var budgetAccessRightService = new BudgetAccessRightService(ProvidersFactory.GetNewBudgetsProviders());
            budgetAccessRightService.Validate(budgetAccessRight);
        }
 public void ValidateBudgetBudgetAccessRight_NullBudgetAccessRight_ThrowException(
     BudgetAccessRight budgetAccessRight)
 {
     var budgetAccessRightService = new BudgetAccessRightService(ProvidersFactory.GetNewBudgetsProviders());
     budgetAccessRightService.Validate(budgetAccessRight);
 }
        /// <summary>
        ///     Creates new access budget right
        /// </summary>
        /// <param name="budgetId">ID of a budget</param>
        /// <param name="assignedUserId">Assigned user's ID</param>
        /// <param name="permission">Permission for right</param>
        /// <returns>False if user has already accessright to this budget or when adding failed. True otherwise.</returns>
        public async Task<bool> CreateBudgetAccessRight(Guid budgetId, Guid assignedUserId, PermissionEnum permission)
        {
            // create budget access right only if it isn't already present
            bool budgetAccessRightExists = await _db.BudgetAccessRights.Where(b => b.Budget.Guid == budgetId && b.UserProfile.Guid == assignedUserId).CountAsync() > 0;
            if (budgetAccessRightExists)
            {
                return false;
            }

            // find budget by its Id
            var budget = await this.GetBudgetById(budgetId);
            // finding creator by his ID
            var assignedUser = this.GetAccessRightsCreator(assignedUserId);

            var budgetAccessRight = new BudgetAccessRight
            {
                Budget = budget,
                Permission = permission,
                UserProfile = assignedUser
            };

            this.Validate(budgetAccessRight);
            
            // creating new budget access right
            return await this._db.AddOrUpdateAsync(budgetAccessRight);
        }