Пример #1
0
        /// <summary>
        /// Create the specified resource.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="mode">The mode.</param>
        /// <returns>FhirOperationResult.</returns>
        /// <exception cref="System.ArgumentNullException">target</exception>
        /// <exception cref="System.IO.InvalidDataException"></exception>
        /// <exception cref="System.Data.SyntaxErrorException"></exception>
        public virtual Resource Create(Resource target, TransactionMode mode)
        {
            this.m_traceSource.TraceInfo("Creating resource {0} ({1})", this.ResourceType, target);

            if (target == null)
            {
                this.m_traceSource.TraceError($"Argument {nameof(target)} null or empty");
                throw new ArgumentNullException(this.m_localizationService.GetString("error.type.ArgumentNullException"));
            }
            else if (!(target is TFhirResource))
            {
                throw new InvalidDataException(this.m_localizationService.GetString("error.type.InvalidDataException"));
            }

            target = ExtensionUtil.ExecuteAfterReceiveRequestBehavior(TypeRestfulInteraction.Create, this.ResourceType, target);

            // We want to map from TFhirResource to TModel
            var modelInstance = this.MapToModel(target as TFhirResource);

            if (modelInstance == null)
            {
                throw new ArgumentException(this.m_localizationService.FormatString("error.type.InvalidDataException.userMessage", new
                {
                    param = "Model"
                }));
            }

            var result = this.Create(modelInstance, mode);

            // Return fhir operation result
            var retVal = this.MapToFhir(result);

            return(ExtensionUtil.ExecuteBeforeSendResponseBehavior(TypeRestfulInteraction.Create, this.ResourceType, retVal));
        }
Пример #2
0
        /// <summary>
        /// Updates the specified resource.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="target">The target.</param>
        /// <param name="mode">The mode.</param>
        /// <returns>Returns the FHIR operation result containing the updated resource.</returns>
        /// <exception cref="System.ArgumentNullException">target</exception>
        /// <exception cref="System.IO.InvalidDataException"></exception>
        /// <exception cref="System.Data.SyntaxErrorException"></exception>
        /// <exception cref="System.ArgumentException"></exception>
        /// <exception cref="System.Reflection.AmbiguousMatchException"></exception>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException"></exception>
        public Resource Update(string id, Resource target, TransactionMode mode)
        {
            this.m_traceSource.TraceInfo("Updating resource {0}/{1} ({2})", this.ResourceType, id, target);

            if (target == null)
            {
                this.m_traceSource.TraceError($"Argument {nameof(target)} is null or empty");
                throw new ArgumentNullException(this.m_localizationService.GetString("error.type.ArgumentNullException"));
            }
            else if (!(target is TFhirResource))
            {
                throw new InvalidDataException(this.m_localizationService.GetString("error.type.InvalidDataException"));
            }

            target = ExtensionUtil.ExecuteAfterReceiveRequestBehavior(TypeRestfulInteraction.Update, this.ResourceType, target);

            // We want to map from TFhirResource to TModel
            target.Id = id;
            var modelInstance = this.MapToModel(target as TFhirResource);

            if (modelInstance == null)
            {
                throw new ArgumentException(this.m_localizationService.FormatString("error.type.InvalidDataException.userMessage", new
                {
                    param = "Request"
                }));
            }

            // Guid identifier
            var guidId = Guid.Empty;

            if (!Guid.TryParse(id, out guidId))
            {
                throw new ArgumentException(this.m_localizationService.FormatString("error.type.ArgumentException", new
                {
                    param = "id"
                }));
            }

            // Model instance key does not equal path
            if (modelInstance.Key != Guid.Empty && modelInstance.Key != guidId)
            {
                throw new InvalidOperationException(this.m_localizationService.GetString("error.messaging.fhir.resourceBase.key"));
            }
            else if (modelInstance.Key == Guid.Empty)
            {
                modelInstance.Key = guidId;
            }

            var result = this.Update(modelInstance, mode);

            // Return fhir operation result
            var retVal = this.MapToFhir(result);

            return(ExtensionUtil.ExecuteBeforeSendResponseBehavior(TypeRestfulInteraction.Update, this.ResourceType, retVal));
        }