Ejemplo n.º 1
0
        public async Task <IActionResult> UpdatePatient([FromBody] PatientUpdateModel model)
        {
            // Получаем id пользователя из токена. Id туда попал при выдаче токена во время авторизации
            var nameIdentifier = this.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);
            int userId         = int.Parse(nameIdentifier.Value);

            // Достаем запись юзера
            User user = await _db.Users.FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                return(NotFound());
            }

            // Достаем запись пациента
            Patient patient = await _db.Patients.FirstOrDefaultAsync(x => x.User.Id == user.Id);

            if (user == null)
            {
                return(NotFound());
            }

            if (!string.IsNullOrWhiteSpace(model.Email))
            {
                user.Email = model.Email;
            }
            if (!string.IsNullOrWhiteSpace(model.Fio))
            {
                user.Fio = model.Fio;
            }

            await _db.SaveChangesAsync();

            return(Ok(new { success = true }));
        }
Ejemplo n.º 2
0
        public async Task <MyWebApp.Domain.Patient> InsertAsync(PatientUpdateModel patient)
        {
            var result = await this.Context.AddAsync(this.Mapper.Map <Patient>(patient));

            await this.Context.SaveChangesAsync();

            return(this.Mapper.Map <MyWebApp.Domain.Patient>(result.Entity));
        }
Ejemplo n.º 3
0
        public async Task <MyWebApp.Domain.Patient> UpdateAsync(PatientUpdateModel patient)
        {
            var existing = await this.Get(patient);

            var result = this.Mapper.Map(patient, existing);

            this.Context.Update(result);

            await this.Context.SaveChangesAsync();

            return(this.Mapper.Map <MyWebApp.Domain.Patient>(result));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Update()
        {
            var patientData = await PatientFacade.GetPatientByUsernameAsync(HttpContext.User.Identity.Name);

            PatientUpdateModel patientUpdateModel = new PatientUpdateModel
            {
                Name    = patientData.Name,
                Surname = patientData.Surname,
                Email   = patientData.Email
            };

            return(View(patientUpdateModel));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult> Update(PatientUpdateModel patientUpdateModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(patientUpdateModel));
            }
            var username        = HttpContext.User.Identity.Name;
            var patientToUpdate = await PatientFacade.GetPatientByUsernameAsync(username);

            patientToUpdate.Email   = patientUpdateModel.Email;
            patientToUpdate.Name    = patientUpdateModel.Name;
            patientToUpdate.Surname = patientUpdateModel.Surname;
            await PatientFacade.EditPatientAsync(patientToUpdate);

            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 6
0
        public async Task <Dictionary <ServiceDictionaryKey, object> > TryUpdatePatient(HttpRequest request)
        {
            Dictionary <ServiceDictionaryKey, object> dictionary = new Dictionary <ServiceDictionaryKey, object>();

            try
            {
                PatientUpdateModel patient = await _messageSerializer.Deserialize <PatientUpdateModel>(request.Body);

                if (patient.PatientId <= 0)
                {
                    dictionary.Add(ServiceDictionaryKey.ERROR, "Please pass a valid patient ID to update.");
                    return(dictionary);
                }

                try
                {
                    if (!await IsAuthorized(dictionary, request, patient.PatientId))
                    {
                        return(dictionary);
                    }
                }
                catch
                {
                    dictionary.Add(ServiceDictionaryKey.ERROR, "Auth threw an error. Has the token lifetime expired?");
                    dictionary.Add(ServiceDictionaryKey.HTTPSTATUSCODE, HttpStatusCode.Unauthorized);
                    return(dictionary);
                }

                bool success = await _patientRepository.Update(patient);

                if (!success)
                {
                    dictionary.Add(ServiceDictionaryKey.ERROR, $"Update failed for given ID: {patient.PatientId}. Does patient exist?");
                    dictionary.Add(ServiceDictionaryKey.HTTPSTATUSCODE, HttpStatusCode.NotFound);
                    return(dictionary);
                }

                dictionary.Add(ServiceDictionaryKey.VALUE, $"Deleted patient with ID: {patient.PatientId}");
            }
            catch (Exception ex)
            {
                dictionary.AddErrorMessage(ServiceDictionaryKey.ERROR, ex, FeedbackHandler);
            }

            return(dictionary);
        }
Ejemplo n.º 7
0
        private string BuildUpdateQuery(PatientUpdateModel patient)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("UPDATE patient ");
            sb.Append("SET ");

            bool isNotFirst = false;

            if (patient.DateOfBirth != null && patient.DateOfBirth != new DateTime())
            {
                sb.Append("date_of_birth=@DOB");
                isNotFirst = true;
            }
            if (patient.WeightInKilograms > 0)
            {
                if (isNotFirst)
                {
                    sb.Append(", ");
                }

                sb.Append("weight=@WEIGHT");
                isNotFirst = true;
            }
            if (patient.DoctorId > 0)
            {
                if (isNotFirst)
                {
                    sb.Append(", ");
                }

                sb.Append("doctor_id=@DOCTORID");
                isNotFirst = true;
            }

            sb.Append(" WHERE id=@PATIENTID");

            return(sb.ToString());
        }
Ejemplo n.º 8
0
        public async Task <bool> Update(PatientUpdateModel patient)
        {
            bool success = false;

            string sqlQuery = BuildUpdateQuery(patient);

            using (SqlConnection sqlConn = new SqlConnection(Environment.GetEnvironmentVariable("sqldb_connection")))
            {
                sqlConn.Open();

                SqlCommand sqlCmd = new SqlCommand(sqlQuery, sqlConn);

                sqlCmd.Parameters.Add("@PATIENTID", SqlDbType.Int).Value = patient.PatientId;

                if (patient.DateOfBirth != null && patient.DateOfBirth != new DateTime())
                {
                    sqlCmd.Parameters.Add("@DOB", SqlDbType.Date).Value = patient.DateOfBirth;
                }

                if (patient.DoctorId > 0)
                {
                    sqlCmd.Parameters.Add("@DOCTORID", SqlDbType.Int).Value = patient.DoctorId;
                }

                if (patient.WeightInKilograms > 0)
                {
                    sqlCmd.Parameters.Add("@WEIGHT", SqlDbType.Float).Value = patient.WeightInKilograms;
                }

                int rows = await sqlCmd.ExecuteNonQueryAsync();

                if (rows > 0)
                {
                    success = true;
                }
            }

            return(success);
        }
        public async Task CreateAsync_PatientValidationSucceed_CreatesPatient()
        {
            // Arrange
            var patient  = new PatientUpdateModel();
            var expected = new Patient();

            var streetService = new Mock <IStreetService>();

            streetService.Setup(x => x.ValidateAsync(patient));

            var patientDAL = new Mock <IPatientDAL>();

            patientDAL.Setup(x => x.InsertAsync(patient)).ReturnsAsync(expected);

            var patientService = new PatientService(patientDAL.Object, streetService.Object);

            // Act
            var result = await patientService.CreateAsync(patient);

            // Assert
            result.Should().Be(expected);
        }
        public async Task CreateAsync_PatientValidationFailed_ThrowsError()
        {
            // Arrange
            var fixture  = new Fixture();
            var patient  = new PatientUpdateModel();
            var expected = fixture.Create <string>();

            var streetService = new Mock <IStreetService>();

            streetService
            .Setup(x => x.ValidateAsync(patient))
            .Throws(new InvalidOperationException(expected));

            var patientDAL = new Mock <IPatientDAL>();

            var patientService = new PatientService(patientDAL.Object, streetService.Object);

            var action = new Func <Task>(() => patientService.CreateAsync(patient));

            // Assert
            await action.Should().ThrowAsync <InvalidOperationException>().WithMessage(expected);

            patientDAL.Verify(x => x.InsertAsync(patient), Times.Never);
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> UpdatePatient(int id, [FromBody] PatientUpdateModel model)
        {
            await _patientService.UpdatePatient(id, _mapper.Map <PatientDTO>(model));

            return(NoContent());
        }
Ejemplo n.º 12
0
        public async Task <Patient> UpdateAsync(PatientUpdateModel patient)
        {
            await this.StreetService.ValidateAsync(patient);

            return(await this.PatientDAL.UpdateAsync(patient));
        }