예제 #1
0
        public IActionResult PutConsent([FromBody] ConsentSpecification specification)
        {
            if (!ModelState.IsValid)
            {
                Logger.LogInformation("Invalid ModelState {@error}", new SerializableError(ModelState));
                return(new BadRequestObjectResult(ModelState));
            }


            var study = Studies.GetStudy(specification.StudyId);

            if (study == null)
            {
                Logger.LogWarning("Study#{studyId} not found", specification.StudyId);
                return(NotFound());
            }
            var studySubject = Subjects.GetStudySubject(study.Id, specification.SubjectIdentifier);

            if (studySubject == null)
            {
                var subjectSpecification = new { specification.StudyId, specification.PersonId };
                Logger.LogDebug("No existing studySubject - creating a new subject for {spec}", subjectSpecification);
                var personId = new PersonIdentity(specification.PersonId);

                studySubject = Subjects.FindStudySubject(study.Id, personId);
                if (studySubject != null)
                {
                    Logger.LogError("There is already a study subject for {spec} - {identifier}", subjectSpecification, studySubject.SubjectIdentifier);
                    return(BadRequest());
                }

                studySubject = new StudySubject(study.Id, specification.SubjectIdentifier, personId);
                studySubject = Subjects.AddStudySubject(studySubject);
            }
            else
            {
                var existingConsent = Consents.FindActiveConsent(studySubject);
                if (existingConsent != null)
                {
                    //TODO: Decide what to do with evidence, etc, for existing consents, or if you can be consented twice
                    return(new SeeOtherOjectActionResult(
                               "GetStudySubject",
                               routeValues: new { studyId = study, subjectIdentifier = studySubject.SubjectIdentifier },
                               result: existingConsent.Id));
                }
            }

            var evidence = EvidenceDtoMarshallers.ConvertToIdentifiers(specification.Evidence);

            var newConsentId = Consents.AddConsent(
                new Common.Consent.Consent(
                    studySubject,
                    specification.DateGiven,
                    specification.GivenBy,
                    evidence));

            return(CreatedAtAction(
                       "GetStudySubject",
                       new { studyId = study, subjectIdentifier = studySubject.SubjectIdentifier },
                       newConsentId.Id));
        }