/// <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));
        }
Esempio n. 2
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));
        }