コード例 #1
0
        public async Task <IActionResult> CreateEncounter(long patientId, [FromBody] EncounterForCreationDto encounterForCreation)
        {
            if (encounterForCreation == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new encounter");
                return(BadRequest(ModelState));
            }

            var patientFromRepo = await _patientRepository.GetAsync(p => p.Id == patientId);

            if (patientFromRepo == null)
            {
                ModelState.AddModelError("Message", "Unable to locate patient");
            }

            var encounterType = _encounterTypeRepository.Get(et => et.Id == encounterForCreation.EncounterTypeId);

            if (encounterType == null)
            {
                ModelState.AddModelError("Message", "Unable to locate encounter type");
            }

            if (encounterForCreation.EncounterDate > DateTime.Today)
            {
                ModelState.AddModelError("Message", "Encounter date should be before current date");
            }
            if (encounterForCreation.EncounterDate < patientFromRepo.DateOfBirth)
            {
                ModelState.AddModelError("Message", "Encounter date should be after date of birth");
            }

            if (!String.IsNullOrEmpty(encounterForCreation.Notes))
            {
                if (Regex.Matches(encounterForCreation.Notes, @"[-a-zA-Z0-9<>/ '.]").Count < encounterForCreation.Notes.Length)
                {
                    ModelState.AddModelError("Message", "Notes contains invalid characters (Enter A-Z, a-z, 0-9, space, apostrophe, period, hyphen)");
                }
            }

            long id = 0;

            if (ModelState.IsValid)
            {
                var encounterDetail = PrepareEncounterDetail(encounterForCreation);
                id = await _patientService.AddEncounterAsync(patientFromRepo, encounterDetail);

                await _unitOfWork.CompleteAsync();

                var mappedEncounter = await GetEncounterAsync <EncounterIdentifierDto>(patientId, id);

                if (mappedEncounter == null)
                {
                    return(StatusCode(500, "Unable to locate newly added encounter"));
                }

                return(CreatedAtAction("GetEncounterByIdentifier",
                                       new
                {
                    id = mappedEncounter.Id
                }, CreateLinksForEncounter <EncounterIdentifierDto>(patientId, mappedEncounter)));
            }

            return(BadRequest(ModelState));
        }