public void TestAddStepToGoal()
        {
            // Generate db entry to test.
            var rnd   = new Random();
            var count = rnd.Next(1, 13);

            foreach (var i in Range(0, count))
            {
                _ = _userEditRepo.Create(RandomUserEdit()).Result;
            }
            var origUserEdit = _userEditRepo.Create(RandomUserEdit()).Result;

            // Generate correct result for comparison.
            var          modUserEdit  = origUserEdit.Clone();
            const string stringStep   = "This is another step added.";
            const int    modGoalIndex = 0;

            modUserEdit.Edits[modGoalIndex].StepData.Add(stringStep);

            // Create and put wrapper object.
            var stepWrapperObj = new UserEditStepWrapper(modGoalIndex, stringStep);

            _ = _userEditController.UpdateUserEditStep(_projId, origUserEdit.Id, stepWrapperObj);

            // Step count should have increased by 1.
            Assert.That(_userEditRepo.GetAllUserEdits(_projId).Result, Has.Count.EqualTo(count + 1));

            var userEdit = _userEditRepo.GetUserEdit(_projId, origUserEdit.Id).Result;

            if (userEdit is null)
            {
                Assert.Fail();
                return;
            }
            Assert.Contains(stringStep, userEdit.Edits[modGoalIndex].StepData);

            // Now update a step within the goal.
            const string modStringStep = "This is a replacement step.";
            const int    modStepIndex  = 1;

            modUserEdit.Edits[modGoalIndex].StepData[modStepIndex] = modStringStep;

            // Create and put wrapper object.
            stepWrapperObj = new UserEditStepWrapper(modGoalIndex, modStringStep, modStepIndex);
            _ = _userEditController.UpdateUserEditStep(_projId, origUserEdit.Id, stepWrapperObj);

            // Step count should not have further increased.
            Assert.That(_userEditRepo.GetAllUserEdits(_projId).Result, Has.Count.EqualTo(count + 1));

            userEdit = _userEditRepo.GetUserEdit(_projId, origUserEdit.Id).Result;
            if (userEdit is null)
            {
                Assert.Fail();
                return;
            }
            Assert.Contains(modStringStep, userEdit.Edits[modGoalIndex].StepData);
        }
        public void TestGoalToUserEdit()
        {
            // Generate db entry to test
            var rnd   = new Random();
            var count = rnd.Next(1, 13);

            for (var i = 0; i < count; i++)
            {
                _ = _userEditRepo.Create(RandomUserEdit()).Result;
            }
            var origUserEdit = _userEditRepo.Create(RandomUserEdit()).Result;

            // Generate correct result for comparison
            var          modUserEdit    = origUserEdit.Clone();
            const string stringUserEdit = "This is another step added";

            modUserEdit.Edits[0].StepData.Add(stringUserEdit);

            // Create wrapper object
            const int modGoalIndex = 0;
            var       wrapperObj   = new UserEditObjectWrapper(modGoalIndex, stringUserEdit);

            var action = _userEditController.Put(_projId, origUserEdit.Id, wrapperObj);

            Assert.That(_userEditRepo.GetAllUserEdits(_projId).Result, Has.Count.EqualTo(count + 1));
            Assert.Contains(stringUserEdit, _userEditRepo.GetUserEdit(
                                _projId, origUserEdit.Id).Result.Edits[modGoalIndex].StepData);
        }
        /// <summary> Adds an <see cref="Edit"/> to a specified <see cref="UserEdit"/> </summary>
        /// <returns> Tuple of a bool: success of operation and int: index at which the new edit was placed </returns>
        public async Task <Tuple <bool, int> > AddGoalToUserEdit(string projectId, string userEditId, Edit edit)
        {
            // Get userEdit to change
            var userEntry = await _repo.GetUserEdit(projectId, userEditId);

            var newUserEdit = userEntry.Clone();

            // Add the new goal index to Edits list
            newUserEdit.Edits.Add(edit);

            // Replace the old UserEdit object with the new one that contains the new list entry
            var replaceSucceeded  = _repo.Replace(projectId, userEditId, newUserEdit).Result;
            var indexOfNewestEdit = -1;

            if (replaceSucceeded)
            {
                var newestEdit = _repo.GetUserEdit(projectId, userEditId).Result;
                indexOfNewestEdit = newestEdit.Edits.Count - 1;
            }

            return(new Tuple <bool, int>(replaceSucceeded, indexOfNewestEdit));
        }
Beispiel #4
0
        /// <summary>
        /// Adds an <see cref="Edit"/> to a specified <see cref="UserEdit"/>,
        /// or updates existing one if edit with same <see cref="Guid"/> already present.
        /// </summary>
        /// <returns>
        /// Tuple of
        ///     bool: success of operation
        ///     int: index at which the edit was placed or -1 on failure
        /// </returns>
        public async Task <Tuple <bool, int> > AddGoalToUserEdit(string projectId, string userEditId, Edit edit)
        {
            // Get userEdit to change
            var oldUserEdit = await _userEditRepo.GetUserEdit(projectId, userEditId);

            const int invalidEditIndex = -1;
            var       failureResult    = new Tuple <bool, int>(false, invalidEditIndex);

            if (oldUserEdit is null)
            {
                return(failureResult);
            }

            var newUserEdit = oldUserEdit.Clone();

            // Update existing Edit if guid exists, otherwise add new one at end of List.
            var indexOfNewestEdit = newUserEdit.Edits.FindIndex(e => e.Guid == edit.Guid);

            if (indexOfNewestEdit > invalidEditIndex)
            {
                newUserEdit.Edits[indexOfNewestEdit] = edit;
            }
            else
            {
                newUserEdit.Edits.Add(edit);
                indexOfNewestEdit = newUserEdit.Edits.Count - 1;
            }

            // Replace the old UserEdit object with the new one that contains the new/updated edit
            var replaceSucceeded = await _userEditRepo.Replace(projectId, userEditId, newUserEdit);

            if (!replaceSucceeded)
            {
                indexOfNewestEdit = invalidEditIndex;
            }

            return(new Tuple <bool, int>(replaceSucceeded, indexOfNewestEdit));
        }
Beispiel #5
0
        public async Task <IActionResult> Get(string projectId, string userEditId)
        {
            if (!_permissionService.HasProjectPermission(Permission.WordEntry, HttpContext))
            {
                return(new ForbidResult());
            }

            // Ensure project exists
            var proj = _projectService.GetProject(projectId);

            if (proj == null)
            {
                return(new NotFoundObjectResult(projectId));
            }

            var userEdit = await _repo.GetUserEdit(projectId, userEditId);

            if (userEdit == null)
            {
                return(new NotFoundObjectResult(userEditId));
            }
            return(new ObjectResult(userEdit));
        }
        public async Task <IActionResult> GetUserEdit(string projectId, string userEditId)
        {
            if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry))
            {
                return(Forbid());
            }

            // Ensure project exists
            var proj = await _projRepo.GetProject(projectId);

            if (proj is null)
            {
                return(NotFound(projectId));
            }

            var userEdit = await _userEditRepo.GetUserEdit(projectId, userEditId);

            if (userEdit is null)
            {
                return(NotFound(userEditId));
            }
            return(Ok(userEdit));
        }