Esempio n. 1
0
        /// <summary>
        ///     Removes entity from changesList if exists.
        ///     If entity does not exist, adds entity to the ChangesList with modifier: Remove
        /// </summary>
        /// <param name="changesList">List, where all the changes a saved</param>
        /// <param name="compareObject">Any class entity</param>
        /// <param name="logger">Ilogger instance</param>
        /// <typeparam name="T">Represents a class</typeparam>
        /// <returns>List of changes</returns>
        /// <exception cref="NullReferenceException">Will be thrown if changesList or compareObject are null</exception>
        public List <Changes> Remove <T>(List <Changes> changesList, T compareObject, ILogger logger) where T : class
        {
            if (logger is null)
            {
                throw new NullReferenceException("Logger was null");
            }
            if (changesList is null)
            {
                logger.LogError($"Parameter {nameof(changesList)} was null");
                throw new NullReferenceException($"Parameter {nameof(changesList)} was null");
            }

            if (compareObject is null)
            {
                logger.LogError($"Parameter {nameof(compareObject)} was null");
                throw new NullReferenceException($"Parameter {nameof(compareObject)} was null");
            }

            var item = new Changes(compareObject, ChangeIdentifier.Remove);

            logger.LogDebug("New item was created. ChangeIdentifier.Remove");

            var exist = changesList.Any(c => c.ChangeObject.Equals(compareObject));

            logger.LogDebug("Check item existence against ChangesList.");
            if (exist)
            {
                logger.LogDebug("Item was found.");
                var deletedItem = changesList.FirstOrDefault(c => c.ChangeObject.Equals(compareObject));
                changesList.Remove(deletedItem);
                logger.LogDebug("Item was removed from the list");
            }
            else
            {
                changesList.Add(item);
                logger.LogDebug("Item was successfully added to the changes list");
            }

            logger.LogInformation("Updated list returned");
            return(changesList);
        }
Esempio n. 2
0
        /// <summary>
        ///     Add entity to changes list if entity does not exists.
        ///     If entity exists, changesList will be returned
        /// </summary>
        /// <param name="changesList">List, where all the changes a saved</param>
        /// <param name="compareObject">Any class entity</param>
        /// <typeparam name="T">Represents a class</typeparam>
        /// <returns>List of changes</returns>
        /// <exception cref="NullReferenceException">Will be thrown if changesList or compareObject are null</exception>
        public List <Changes> Update <T>(List <Changes> changesList, T compareObject) where T : class
        {
            if (changesList is null)
            {
                throw new NullReferenceException($"Parameter {nameof(changesList)} was null");
            }
            if (compareObject is null)
            {
                throw new NullReferenceException($"Parameter {nameof(compareObject)} was null");
            }

            var item  = new Changes(compareObject, ChangeIdentifier.Update);
            var exist = changesList.Any(c => c.ChangeObject.Equals(compareObject));

            if (exist)
            {
                return(changesList);
            }
            changesList.Add(item);
            return(changesList);
        }
Esempio n. 3
0
        /// <summary>
        ///     Add entity of type T to changesList if not exists.
        ///     If entity exist, the modifier will be checked. If modifier equals 'Remove', entity will be removed from list.
        ///     The updated list will be returned.
        /// </summary>
        /// <param name="changesList">List, where all the changes a saved</param>
        /// <param name="compareObject">Any class entity</param>
        /// <typeparam name="T">Represents a class</typeparam>
        /// <returns>List of changes</returns>
        /// <exception cref="NullReferenceException">Will be thrown if changesList or compareObject are null</exception>
        public List <Changes> Add <T>(List <Changes> changesList, T compareObject) where T : class
        {
            // Check if changesList is null
            if (changesList is null)
            {
                // Throw NullReferenceException if changesList is null
                throw new NullReferenceException($"Parameter {nameof(changesList)} was null");
            }
            // Check if compareObject is null
            if (compareObject is null)
            {
                // Throw NullReferenceException if compareObject is null
                throw new NullReferenceException($"Parameter {nameof(compareObject)} was null");
            }
            // create new Changes item
            var item = new Changes(compareObject, ChangeIdentifier.Add);
            // check if item exists in the changesList
            var exists = changesList.Any(c => c.ChangeObject.Equals(compareObject));

            if (exists)
            {
                // get the identifier for the item
                var identifier = changesList.FirstOrDefault(c => c.ChangeObject.Equals(compareObject)).ChangeIdentifier;
                // if item exists, need to check previous interaction.
                // If identifier equals 'Remove', that means, than the item was previously removed and now should be added again.
                // This action neutralizes the previous one.
                if (identifier == ChangeIdentifier.Remove)
                {
                    changesList.Remove(changesList.FirstOrDefault(c => c.ChangeObject.Equals(compareObject)));
                }
                // return updated changesList
                return(changesList);
            }

            // add item to the changesList
            changesList.Add(item);
            // return updated changesList
            return(changesList);
        }