public async Task <bool> Create(string password, PatientServiceModel patientServiceModel) { var user = new HospitalMSUser { UserName = patientServiceModel.Email, Email = patientServiceModel.Email }; var userCreateResult = await userManager.CreateAsync(user, password); if (userCreateResult.Succeeded) { user.EmailConfirmed = true; user.IsFirstLogin = false; await userManager.AddToRoleAsync(user, GlobalConstants.PatientRoleName); } patientServiceModel.HospitalMSUserId = user.Id; Patient patient = AutoMapper.Mapper.Map <Patient>(patientServiceModel); context.Patients.Add(patient); int result = await context.SaveChangesAsync(); return(result > 0); }
public async Task <IActionResult> BecomePatient(PatientsCreateInputModel patientCreateInputModel) { if (!this.ModelState.IsValid) { var cities = this.cityService.AllCities().Result; var inputModel = new PatientsCreateInputModel { Cities = cities, }; return(this.View(inputModel)); } var user = await this.userManager.GetUserAsync(this.HttpContext.User); var model = new PatientServiceModel() { FullName = patientCreateInputModel.FullName, Age = patientCreateInputModel.Age, HospitalId = patientCreateInputModel.HospitalId, BloodType = new BloodTypeServiceModel { ABOGroupName = patientCreateInputModel.BloodType.ABOGroupName, RhesusFactor = patientCreateInputModel.BloodType.RhesusFactor, }, UserId = user.Id, Ward = patientCreateInputModel.Ward, NeededBloodBanks = patientCreateInputModel.NeededBloodBanks, }; await this.patientService.CreateAsync(model); return(this.Redirect("/")); }
public async Task CreateAsync_ShouldThrowException_WithInvalidData(string fullName, int age, string aboGroup, string rhesusFactor, int hospitalId, int neededBloodBanks, string ward) { var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var seeder = new Seeder(); await seeder.SeedUsersAsync(context); await seeder.SeedBloodTypesAsync(context); var userManager = this.GetUserManagerMock(context); var patientService = new PatientService(context, userManager.Object); var patientServiceModel = new PatientServiceModel { FullName = fullName, Age = age, BloodType = new BloodDonationSystem.Services.Models.BloodTypeServiceModel { ABOGroupName = aboGroup, RhesusFactor = rhesusFactor, }, HospitalId = hospitalId, NeededBloodBanks = neededBloodBanks, Ward = ward, UserId = context.Users.First().Id, }; await Assert.ThrowsAsync <ArgumentNullException>(async() => { await patientService.CreateAsync(patientServiceModel); }); }
public async Task <IActionResult> Edit(PatientServiceModel model) { if (!ModelState.IsValid) { return(View(model)); } if (!this.User.IsInRole(WebConstants.DoctorRole)) { return(Unauthorized()); } var patientExists = await this.patientService.PatientExists(model.Id); if (!patientExists) { return(NotFound()); } var user = await this.userManager.GetUserAsync(User); var email = user.Email; var success = await this.patientService.Edit(model.Id, model.Name, model.EGN, model.Age, email); if (!success) { return(BadRequest()); } return(RedirectToAction(nameof(All))); }
public async Task <IActionResult> Edit(string id, PatientEditInputModel patientEditInputModel) { if (!ModelState.IsValid) { return(View(patientEditInputModel)); } PatientServiceModel patientServiceModel = AutoMapper.Mapper.Map <PatientServiceModel>(patientEditInputModel); await patientService.Edit(id, patientServiceModel); return(Redirect("/Patient/All")); }
public async Task <bool> Create(PatientServiceModel patientServiceModel) { var user = await userManager.FindByIdAsync(patientServiceModel.HospitalMSUserId); user.IsFirstLogin = false; await userManager.UpdateAsync(user); Patient patient = AutoMapper.Mapper.Map <Patient>(patientServiceModel); context.Patients.Add(patient); int result = await context.SaveChangesAsync(); return(result > 0); }
public async Task <IActionResult> Create(PatientCreateInputModel patientCreateInputModel) { if (!ModelState.IsValid) { await GetAllDepartments(); return(View(patientCreateInputModel)); } PatientServiceModel patientServiceModel = AutoMapper.Mapper.Map <PatientServiceModel>(patientCreateInputModel); await patientService.Create(patientCreateInputModel.Password, patientServiceModel); return(Redirect("/Patient/All")); }
public async Task <PatientServiceModel> GetByUserIdAsync(string userId) { var patient = await this.context.Patients.FirstOrDefaultAsync(x => x.UserId == userId); var patientModel = new PatientServiceModel { Id = patient.Id, FullName = patient.FullName, Age = patient.Age, BloodTypeId = patient.BloodTypeId, HospitalId = patient.HospitalId, Ward = patient.Ward, UserId = patient.UserId, NeededBloodBanks = patient.NeededBloodBanks, }; return(patientModel); }
public async Task CreateAsync_WithCorectData_ShouldReturnCorectResult(string fullName, int age, string aboGroup, string rhesusFactor, int hospitalId, int neededBloodBanks, string ward) { var errorMessage = "PatientService CreateAsync method does not return properly "; var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var seeder = new Seeder(); await seeder.SeedUsersAsync(context); await seeder.SeedBloodTypesAsync(context); var userManager = this.GetUserManagerMock(context); var patientService = new PatientService(context, userManager.Object); var patientServiceModel = new PatientServiceModel { FullName = fullName, Age = age, BloodType = new BloodDonationSystem.Services.Models.BloodTypeServiceModel { ABOGroupName = aboGroup, RhesusFactor = rhesusFactor, }, HospitalId = hospitalId, NeededBloodBanks = neededBloodBanks, Ward = ward, UserId = context.Users.First().Id, }; var result = await patientService.CreateAsync(patientServiceModel); var actualResult = context.Patients.FirstOrDefault(); var expectedResult = patientServiceModel; Assert.True(result); Assert.True(actualResult.FullName == expectedResult.FullName, errorMessage + "FullName"); Assert.True(actualResult.Age == expectedResult.Age, errorMessage + "Age"); Assert.True(actualResult.BloodType.ABOGroupName.ToString() == expectedResult.BloodType.ABOGroupName.ToString(), errorMessage + "ABOGroup"); Assert.True(actualResult.BloodType.RhesusFactor.ToString() == expectedResult.BloodType.RhesusFactor.ToString(), errorMessage + "RhesusFactor"); Assert.True(actualResult.HospitalId == expectedResult.HospitalId, errorMessage + "HospitalId"); Assert.True(actualResult.NeededBloodBanks == expectedResult.NeededBloodBanks, errorMessage + "NeededBloodBanks"); Assert.True(actualResult.Ward == expectedResult.Ward, errorMessage + "Ward"); Assert.True(actualResult.UserId == expectedResult.UserId, errorMessage + "UserId"); }
public async Task <IActionResult> Create(PatientCreateFromFirstLoginInputModel patientCreateFromFirstLoginInputModel) { string userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; var hospitalMSUser = (await userService.GetById(userId)).To <HospitalMSUserViewModel>(); if (!ModelState.IsValid) { return(View(patientCreateFromFirstLoginInputModel)); } PatientServiceModel patientServiceModel = AutoMapper.Mapper.Map <PatientServiceModel>(patientCreateFromFirstLoginInputModel); patientServiceModel.Email = hospitalMSUser.Email; patientServiceModel.HospitalMSUserId = hospitalMSUser.Id; await patientService.Create(patientServiceModel); return(Redirect("Index")); }
public async Task <bool> Edit(string id, PatientServiceModel patientServiceModel) { Patient patientFromDb = await context.Patients.SingleOrDefaultAsync(patient => patient.Id == id); if (patientFromDb == null) { throw new ArgumentNullException(nameof(patientFromDb)); } patientFromDb.FirstName = patientServiceModel.FirstName; patientFromDb.LastName = patientServiceModel.LastName; patientFromDb.PhoneNumber = patientServiceModel.PhoneNumber; patientFromDb.Address = patientServiceModel.Address; patientFromDb.BirthDate = patientServiceModel.BirthDate; context.Patients.Update(patientFromDb); int result = await context.SaveChangesAsync(); return(result > 0); }
public async Task <IActionResult> Create(PatientServiceModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await this.userManager.GetUserAsync(User); var email = user.Email; var success = await this.patientService.Create(model.Name, model.EGN, model.Age, email); if (!success) { return(BadRequest()); } return(RedirectToAction(nameof(All))); }
public async Task <bool> CreateAsync(PatientServiceModel patientServiceModel) { if (string.IsNullOrWhiteSpace(patientServiceModel.FullName) || string.IsNullOrWhiteSpace(patientServiceModel.UserId) || patientServiceModel.BloodType == null || string.IsNullOrWhiteSpace(patientServiceModel.Ward) || patientServiceModel.NeededBloodBanks < MinValueNeededBloodBanks || patientServiceModel.NeededBloodBanks > MaxValueNeededBloodBanks) { throw new ArgumentNullException(); } var user = await this.context.Users.FirstOrDefaultAsync(x => x.Id == patientServiceModel.UserId); var aboGroup = Enum.Parse <ABOGroup>(patientServiceModel.BloodType.ABOGroupName); var rhesusFactor = Enum.Parse <RhesusFactor>(patientServiceModel.BloodType.RhesusFactor); var bloodType = await this.context.BloodTypes.SingleOrDefaultAsync(x => x.ABOGroupName == aboGroup && x.RhesusFactor == rhesusFactor); var patient = new Patient() { FullName = patientServiceModel.FullName, Age = patientServiceModel.Age, BloodType = bloodType, BloodTypeId = bloodType.Id, User = user, UserId = user.Id, HospitalId = patientServiceModel.HospitalId, Ward = patientServiceModel.Ward, NeededBloodBanks = patientServiceModel.NeededBloodBanks, }; await this.userManager.AddToRoleAsync(user, GlobalConstants.PatientRoleName); await this.context.Patients.AddAsync(patient); var result = await this.context.SaveChangesAsync(); return(result > 0); }