示例#1
0
        public async Task <IActionResult> RegisterPatient(PatientRegistrationDto PatientRegistration)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    PatientRegistration.CreationUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
                    PatientRegistration.UpdateUserId   = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

                    // Check CI exists
                    var ciExists = await _personRepo.FindPersonByCi(PatientRegistration.Ci);

                    if (ciExists != null)
                    {
                        return(BadRequest("ci_already_exists"));
                    }

                    // Create insure
                    var insureToCreate = _mapper.Map <Insure>(PatientRegistration);
                    insureToCreate.RegistrationNumber = _insureRepo.GenerateRegistrationNumber(PatientRegistration.Name, PatientRegistration.LastName, PatientRegistration.BirthDate);
                    insureToCreate.Type = "patient";
                    var newInsure = await _insureRepo.CreateInsure(insureToCreate);

                    // Create telephone
                    var telephoneToCreate = new Telephone()
                    {
                        Number = PatientRegistration.TelephoneString
                    };
                    var newTelephone = await _telephoneRepo.CreateTelephone(telephoneToCreate);

                    // Create cellphone
                    var cellphoneToCreate = new Telephone()
                    {
                        Number = PatientRegistration.CellPhoneString
                    };
                    var newCellphone = await _telephoneRepo.CreateTelephone(cellphoneToCreate);

                    // Create person
                    var personToCreate = _mapper.Map <Person>(PatientRegistration);
                    personToCreate.InsureId    = newInsure.Id;
                    personToCreate.TelephoneId = newTelephone.Id;
                    personToCreate.CellPhoneId = newCellphone.Id;
                    var newPerson = await _personRepo.CreatePerson(personToCreate);

                    var patientCreated = _mapper.Map <PatientToReturnDto>(newPerson);
                    transaction.Commit();

                    return(Ok(patientCreated));
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    _logger.LogError(ex.Message);
                    return(BadRequest("update_failed"));
                }
            }
        }