private Clinic_Patient CreatePatient(string firstName, string lastName, string address, string dni, Clinic_MedicalPlan medicalPlan, Clinic_Client client, Clinic clinic) { Clinic_Patient patient; using (var dbContext = new ApplicationDbContext()) { patient = new Clinic_Patient { FirstName = firstName, LastName = lastName, Address = address, Dni = dni, PhoneNumber = string.Empty, MedicalPlanId = medicalPlan.Id, ClientId = client.Id, UserId = clinic.Id }; dbContext.Clinic_Patients.Add(patient); dbContext.SaveChanges(); } return(patient); }
public void AddForNonClient([FromBody] AddPatientForNonClientDto patientDto) { using (var dbContext = new ApplicationDbContext()) { var userId = GetUserId(); var medicalPlan = dbContext.Clinic_MedicalPlans.FirstOrDefault(mp => mp.Id == patientDto.MedicalPlanId); if (medicalPlan == null) { throw new ApplicationException(ExceptionMessages.BadRequest); } if (!_roleManager.RoleExistsAsync(Roles.Client).Result) { throw new ApplicationException(ExceptionMessages.RolesHaveNotBeenCreated); } var user = new ApplicationUser { UserName = patientDto.Email, Email = patientDto.Email }; var result = _userManager.CreateAsync(user, patientDto.Password).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.UsernameAlreadyExists); } var appUser = _userManager.Users.SingleOrDefault(au => au.Email == patientDto.Email); result = _userManager.AddToRoleAsync(appUser, Roles.Client).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.InternalServerError); } var client = new Clinic_Client { UserId = appUser.Id }; dbContext.Clinic_Clients.Add(client); dbContext.SaveChanges(); var patient = new Clinic_Patient { FirstName = patientDto.FirstName, LastName = patientDto.LastName, Address = patientDto.Address, PhoneNumber = patientDto.PhoneNumber, Dni = patientDto.Dni, UserId = userId, ClientId = client.Id, MedicalPlanId = patientDto.MedicalPlanId }; dbContext.Clinic_Patients.Add(patient); dbContext.SaveChanges(); } }
public void RequestAppointmentForNonClient([FromBody] RequestAppointmentForNonClientDto requestAppointmentDto) { using (var dbContext = new ApplicationDbContext()) { var userId = GetUserId(); if (requestAppointmentDto.Day.Date < DateTime.Today.Date) { throw new BadRequestException(ExceptionMessages.AppointmentCantBeRequested); } var doctor = dbContext.Clinic_Doctors.FirstOrDefault(d => d.Id == requestAppointmentDto.DoctorId && d.UserId == userId); if (doctor == null) { throw new BadRequestException(ExceptionMessages.BadRequest); } var medicalPlan = dbContext.Clinic_MedicalPlans.FirstOrDefault(mp => mp.Id == requestAppointmentDto.MedicalPlanId); if (medicalPlan == null) { throw new ApplicationException(ExceptionMessages.BadRequest); } if (!_roleManager.RoleExistsAsync(Roles.Client).Result) { throw new ApplicationException(ExceptionMessages.RolesHaveNotBeenCreated); } var user = new ApplicationUser { UserName = requestAppointmentDto.Email, Email = requestAppointmentDto.Email }; var result = _userManager.CreateAsync(user, requestAppointmentDto.Password).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.UsernameAlreadyExists); } var appUser = _userManager.Users.SingleOrDefault(au => au.Email == requestAppointmentDto.Email); result = _userManager.AddToRoleAsync(appUser, Roles.Client).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.InternalServerError); } var client = new Clinic_Client { UserId = appUser.Id }; dbContext.Clinic_Clients.Add(client); dbContext.SaveChanges(); var patient = new Clinic_Patient { FirstName = requestAppointmentDto.FirstName, LastName = requestAppointmentDto.LastName, Address = requestAppointmentDto.Address, PhoneNumber = requestAppointmentDto.PhoneNumber, Dni = requestAppointmentDto.Dni, UserId = userId, ClientId = client.Id, MedicalPlanId = requestAppointmentDto.MedicalPlanId }; dbContext.Clinic_Patients.Add(patient); dbContext.SaveChanges(); var availableAppointments = doctor.GetAllAvailablesForDay(requestAppointmentDto.Day.Date); var appointment = new DateTime( requestAppointmentDto.Day.Year, requestAppointmentDto.Day.Month, requestAppointmentDto.Day.Day, requestAppointmentDto.Time.Hour, requestAppointmentDto.Time.Minute, requestAppointmentDto.Time.Second ); if (!availableAppointments.Contains(appointment)) { throw new BadRequestException(ExceptionMessages.AppointmentAlreadyTaken); } dbContext.Clinic_Appointments.Add(new Clinic_Appointment { DoctorId = requestAppointmentDto.DoctorId, Doctor = doctor, DateTime = appointment, State = AppointmentStateEnum.Reserved, PatientId = patient.Id, UserId = userId }); dbContext.SaveChanges(); } }
public void RequestAppointmentByClient([FromBody] RequestAppointmentByClientDto requestAppointmentDto) { using (var dbContext = new ApplicationDbContext()) { var userId = GetUserId(); if (requestAppointmentDto.Day.Date < DateTime.Today.Date) { throw new BadRequestException(ExceptionMessages.AppointmentCantBeRequested); } var clinic = dbContext.Clinics.FirstOrDefault(c => c.Id == requestAppointmentDto.ClinicId); if (clinic == null) { throw new BadRequestException(ExceptionMessages.BadRequest); } var doctor = dbContext.Clinic_Doctors.FirstOrDefault(d => d.Id == requestAppointmentDto.DoctorId && d.UserId == clinic.UserId); if (doctor == null) { throw new BadRequestException(ExceptionMessages.BadRequest); } var client = dbContext.Clinic_Clients.FirstOrDefault(c => c.UserId == userId); if (client == null) { throw new BadRequestException(ExceptionMessages.BadRequest); } var medicalPlan = dbContext.Clinic_MedicalPlans.FirstOrDefault(mp => mp.Id == requestAppointmentDto.MedicalPlanId && mp.UserId == clinic.UserId); if (medicalPlan == null) { throw new ApplicationException(ExceptionMessages.BadRequest); } var patient = dbContext.Clinic_Patients.FirstOrDefault(p => p.ClientId == client.Id && p.UserId == clinic.UserId); if (patient != null) { throw new ApplicationException(ExceptionMessages.BadRequest); } patient = new Clinic_Patient { FirstName = requestAppointmentDto.FirstName, LastName = requestAppointmentDto.LastName, Address = requestAppointmentDto.Address, PhoneNumber = requestAppointmentDto.PhoneNumber, Dni = requestAppointmentDto.Dni, UserId = clinic.UserId, ClientId = client.Id, MedicalPlanId = requestAppointmentDto.MedicalPlanId }; dbContext.Clinic_Patients.Add(patient); var availableAppointments = doctor.GetAllAvailablesForDay(requestAppointmentDto.Day.Date); var appointment = new DateTime( requestAppointmentDto.Day.Year, requestAppointmentDto.Day.Month, requestAppointmentDto.Day.Day, requestAppointmentDto.Time.Hour, requestAppointmentDto.Time.Minute, requestAppointmentDto.Time.Second ); if (!availableAppointments.Contains(appointment)) { throw new BadRequestException(ExceptionMessages.AppointmentAlreadyTaken); } dbContext.Clinic_Appointments.Add(new Clinic_Appointment { DoctorId = requestAppointmentDto.DoctorId, Doctor = doctor, DateTime = appointment, State = AppointmentStateEnum.Reserved, PatientId = patient.Id, UserId = clinic.UserId }); dbContext.SaveChanges(); } }
private Clinic_Appointment CreateAppointment(DateTime dateTime, Clinic_Doctor doctor, Clinic_Patient patient, AppointmentStateEnum state, Clinic_Rating rating, Clinic clinic) { Clinic_Appointment appointment; using (var dbContext = new ApplicationDbContext()) { appointment = new Clinic_Appointment { DateTime = dateTime, DoctorId = doctor.Id, PatientId = patient.Id, State = state, RatingId = rating?.Id ?? 0, UserId = clinic.UserId }; dbContext.Clinic_Appointments.Add(appointment); dbContext.SaveChanges(); } return(appointment); }