private Clinic_Client CreateClientUser(string email, string password) { Clinic_Client client; if (!_roleManager.RoleExistsAsync(Roles.Client).Result) { throw new ApplicationException(ExceptionMessages.RolesHaveNotBeenCreated); } var user = new ApplicationUser { UserName = email, Email = email }; var result = _userManager.CreateAsync(user, password).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.UsernameAlreadyExists); } using (var dbContext = new ApplicationDbContext()) { var appUser = _userManager.Users.SingleOrDefault(au => au.Email == email); result = _userManager.AddToRoleAsync(appUser, Roles.Client).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.InternalServerError); } client = new Clinic_Client { UserId = appUser.Id }; dbContext.Clinic_Clients.Add(client); dbContext.SaveChanges(); } return(client); }
public void Register([FromBody] RegisterClientDto clientDto) { if (!_roleManager.RoleExistsAsync(Roles.Client).Result) { throw new ApplicationException(ExceptionMessages.RolesHaveNotBeenCreated); } var user = new ApplicationUser { UserName = clientDto.Email, Email = clientDto.Email }; var result = _userManager.CreateAsync(user, clientDto.Password).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.UsernameAlreadyExists); } using (var dbContext = new ApplicationDbContext()) { var appUser = _userManager.Users.SingleOrDefault(au => au.Email == clientDto.Email); result = _userManager.AddToRoleAsync(appUser, Roles.Client).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.InternalServerError); } var client = new Clinic_Client { UserId = appUser.Id, Logo = "" }; dbContext.Clinic_Clients.Add(client); dbContext.SaveChanges(); } }
public LogOnDto LoginFacebook([FromBody] LoginFacebookDto model) { using (var dbContext = new ApplicationDbContext()) { var client = dbContext.Clinic_Clients.FirstOrDefault(c => c.User.Email == model.Email); // Si client es null, el usuario no esta registrado. Si es distinto de null, ya esta registrado. if (client == null) { // Registrar cliente if (!_roleManager.RoleExistsAsync(Roles.Client).Result) { throw new ApplicationException(ExceptionMessages.RolesHaveNotBeenCreated); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = _userManager.CreateAsync(user, Guid.NewGuid().ToString()).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.UsernameAlreadyExists); } var applicationUser = _userManager.Users.SingleOrDefault(au => au.Email == model.Email); result = _userManager.AddToRoleAsync(applicationUser, Roles.Client).Result; if (!result.Succeeded) { throw new ApplicationException(ExceptionMessages.InternalServerError); } client = new Clinic_Client { UserId = applicationUser.Id, Logo = "", FacebookUserId = model.UserId }; dbContext.Clinic_Clients.Add(client); dbContext.SaveChanges(); } // Chequeo que el FacebookUserId sea correcto if (client.FacebookUserId != model.UserId) { throw new ApplicationException(ExceptionMessages.BadRequest); } // Logueo al usuario var appUser = _userManager.Users.SingleOrDefault(user => user.Email == model.Email); string token = GenerateJwtToken(model.Email, appUser); int userId = appUser.Id; if (!_userManager.IsInRoleAsync(appUser, Roles.Client).Result) { throw new ApplicationException(ExceptionMessages.BadRequest); } ValidTokens.Add($"{JwtBearerDefaults.AuthenticationScheme} {token}", userId); return(new LogOnDto { Token = token, Logo = client.Logo }); } }
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(); } }
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); }