Esempio n. 1
0
        /// <summary>
        /// Saves.
        /// </summary>
        /// <param name="id">The entity identifier.</param>
        /// <param name="delta">The data to change.</param>
        /// <param name="cancellationToken">The optional token to monitor for cancellation requests.</param>
        /// <returns>The change method.</returns>
        public virtual async Task <ChangingResultInfo> SaveAsync(string id, JsonObjectNode delta, CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                if (delta == null)
                {
                    return(null);
                }
                try
                {
                    var newEntity = delta.Deserialize <T>();
                    return(await SaveAsync(newEntity, cancellationToken) ?? new ChangingResultInfo(ChangeErrorKinds.Service, "No response."));
                }
                catch (System.Text.Json.JsonException)
                {
                    return(new ChangingResultInfo(ChangeErrorKinds.Argument, "Failed to convert the JSON object."));;
                }
            }

            var entity = await GetAsync(id, true, cancellationToken);

            if (delta == null || delta.Count == 0)
            {
                return(new ChangingResultInfo <T>(ChangeMethods.Unchanged, entity, "Update properties."));
            }
            ;
            entity.SetProperties(delta);
            await SaveAsync(entity, cancellationToken);

            return(new ChangingResultInfo <T>(ChangeMethods.MemberModify, entity, "Update properties."));
        }