/// <summary> /// Method to update an old patient. /// </summary> /// <param name="oldPatientDto">oldPatientDto contains information of patient to be modified.</param> /// <returns>ResponseModel regarding the modification status of the patient</returns> public ResponseModel Update(PatientDto oldPatientDto, int userId) { var validationStatus = ValidateRequest.ValidatePatientDto(oldPatientDto); if (!(bool)validationStatus.First) { return(ReturnStatements.BadRequestResponse(validationStatus.Second)); } oldPatientDto = validationStatus.Second; using (var unitOfWork = new UnitOfWork()) { var findPatient = unitOfWork.Patients.Find(m => m.Id.Equals(oldPatientDto.Id)); if (findPatient == null) { return(ReturnStatements.FailedResponse(Strings.NoPatientFound)); } if (!findPatient.UserId.Equals(userId)) { return(ReturnStatements.FailedResponse(Strings.Unauthorized)); } findPatient = MapForUpdate(oldPatientDto, findPatient); unitOfWork.Patients.Update(findPatient); var saveResponse = unitOfWork.Complete(); return(Functions.ReturnGeneric(saveResponse, Strings.SuccessfullModification, Strings.UnsuccessfullModification)); } }
/// <summary> /// Method for checking login credentials of user. /// </summary> /// <param name="loginDto">loginDto contains login detail of a new user trying to log in.</param> /// <returns>ResponseModel associated to login status of a user.</returns> public ResponseModel Login(LoginDto loginDto) { using (var unitOfWork = new UnitOfWork()) { var userList = unitOfWork.Users.FindOnConstraint(QueryExpressions.UserHandle(loginDto), prop => prop.UserPasswords).ToList(); if (!userList.Count.Equals(0)) { var userInfo = userList[0]; if (!userInfo.IsVerified) { return(ReturnStatements.FailedLogin(StatusMessages.UserNotVerified, StatusCodes.Unauthorized)); } var userPassword = userInfo.UserPasswords.Where(QueryExpressions.UserPassword(loginDto, userInfo.Id)).ToList()[0]; if (userPassword != null) { return(ReturnStatements.SuccessLogin(userInfo.Id)); } } return(ReturnStatements.FailedLogin(StatusMessages.LoginFailed, StatusCodes.Unauthorized)); } }
/// <summary> /// Method to add patient to database. /// </summary> /// <param name="newPatientDto">newPatientDto contains information of patient to be added.</param> /// <returns>ResponseModel containing patient addition status</returns> public ResponseModel Add(PatientDto newPatientDto, int userId) { var validationStatus = ValidateRequest.ValidatePatientDto(newPatientDto); if (!(bool)validationStatus.First) { return(ReturnStatements.BadRequestResponse(validationStatus.Second)); } newPatientDto = validationStatus.Second; using (var unitOfWork = new UnitOfWork()) { var dbPatient = DtoToDatabase.Patient(newPatientDto, userId); unitOfWork.Patients.Add(dbPatient); var saveResponse = unitOfWork.Complete(); if (saveResponse.Equals(Integers.UnsuccessfullDatabaseSave)) { return(ReturnStatements.FailedResponse(DynamicListForResponse.Create(newPatientDto))); } SendMail(newPatientDto); return(ReturnStatements.SuccessResponse(DynamicListForResponse.Create(newPatientDto))); } }
/// <summary> /// Method to add user to database. /// </summary> /// <param name="newUserDto">newUserDto contains personal information of user and user id and password.</param> /// <returns>ResponseModel of user addition in database.</returns> public ResponseModel Add(UserDto newUserDto) { var validationStatus = ValidateRequest.ValidateUserDto(newUserDto); if (!(bool)validationStatus.First) { return(ReturnStatements.BadRequestResponse(validationStatus.Second)); } newUserDto = validationStatus.Second; using (var unitOfWork = new UnitOfWork()) { var checkIfUserAlreadyExist = unitOfWork.Users.Find(m => m.Handle.Equals(newUserDto.Handle) || m.EmailId.Equals(newUserDto.EmailId)) != null; if (checkIfUserAlreadyExist) { return(ReturnStatements.BadRequestResponse(Strings.UserAlreadyExist)); } var dbUser = DtoToDatabase.User(newUserDto); var loginDto = new LoginDto(loginHandle: newUserDto.Handle, loginPassword: newUserDto.Password); var dbUserPassword = DtoToDatabase.UserPassword(loginDto, dbUser.Id); dbUserPassword.User = dbUser; unitOfWork.UserPasswords.Add(dbUserPassword); var saveResponse = unitOfWork.Complete(); if (saveResponse.Equals(Integers.UnsuccessfullDatabaseSave)) { return(ReturnStatements.FailedResponse(DynamicListForResponse.Create(loginDto))); } SendMail(loginDto.LoginHandle, dbUser.VerificationCode, dbUser.EmailId, false); return(ReturnStatements.SuccessResponse(DynamicListForResponse.Create(loginDto))); } }
/// <summary> /// Method to create a new forgot password request. /// </summary> /// <param name="forgotPasswordDto">forgotpassword dto contains dtails required by system to generate a new forgot password request.</param> /// <returns>ResponseModel containing the forgot password request creation status</returns> public ResponseModel ForgotPassword(ForgotPasswordDto forgotPasswordDto) { var validationStatus = ValidateRequest.ValidateForgotPasswordDto(forgotPasswordDto); if (!(bool)validationStatus.First) { return(ReturnStatements.BadRequestResponse(validationStatus.Second)); } forgotPasswordDto = validationStatus.Second; using (var unitOfWork = new UnitOfWork()) { var user = unitOfWork.Users.Find(m => m.Handle.Equals(forgotPasswordDto.Handle)); if (user == null) { return(ReturnStatements.FailedResponse(Strings.UserDoNotExist)); } user.ForgotPasswordFlag = true; user.ForgotPasswordCode = Functions.GenerateCode().ToString(); unitOfWork.Users.Update(user); var saveResponse = unitOfWork.Complete(); if (saveResponse.Equals(Integers.UnsuccessfullDatabaseSave)) { return(ReturnStatements.FailedResponse(DynamicListForResponse.Create(forgotPasswordDto))); } SendMail(forgotPasswordDto.Handle, user.ForgotPasswordCode, user.EmailId, true); return(ReturnStatements.SuccessResponse(DynamicListForResponse.Create(forgotPasswordDto))); } }
/// <summary> /// Method to get insurance from database based on user query. /// </summary> /// <param name="insuranceQuery">insuranceQuery contains query parameters to get insurances.</param> /// <returns>ResponseModel containing list of InsuranceDto.</returns> public ResponseModel Get(InsuranceQuery insuranceQuery) { using (var unitOfWork = new UnitOfWork()) { //Creating a parameter less query model if it is null from request if (insuranceQuery == null) { insuranceQuery = new InsuranceQuery(id: CommonString.OptionalStringParamInteger, name: CommonString.OptionalStringParam, phoneNumber: CommonString.OptionalStringParam, pubId: CommonString.OptionalStringParam); } insuranceQuery.SetTypedVariables(); var result = unitOfWork.Insurances.FindAll(QueryExpressions.Insurance(insuranceQuery)); result = result.OrderByDescending(m => m.Id).ToList(); return(ReturnStatements.SuccessResponse(CollectionConversions.ListInsurance(result))); } }
/// <summary> /// Method to get patient insurance from database based on user query. /// </summary> /// <param name="patientInsuranceQuery">patientInsuranceQuery containing query to get PatientInsuranceDto from database</param> /// <returns>ResponseModel containing list of PatientInsuranceDto</returns> public ResponseModel Get(PatientInsuranceQuery patientInsuranceQuery) { using (var unitOfWork = new UnitOfWork()) { //Creating a parameter less query model if it is null from request if (patientInsuranceQuery == null) { patientInsuranceQuery = new PatientInsuranceQuery(patientId: CommonString.OptionalStringParamInteger, insuranceId: CommonString.OptionalStringParamInteger); } patientInsuranceQuery.SetTypedVariables(); var result = unitOfWork.PatientInsurances.FindOnConstraint(QueryExpressions.PatientInsurance(patientInsuranceQuery), prop => prop.Insurance, prop => prop.Patient).ToList(); result = result.OrderByDescending(m => m.Id).ToList(); return(ReturnStatements.SuccessResponse(CollectionConversions.ListPatientInsuranceToDto(ConvertPatientInsuranceToPairList(result)))); } }
/// <summary> /// Method to reset user password /// </summary> /// <param name="resetPasswordDto">resetPasswordDto contains information to reset a password for a user</param> /// <returns>ResponseModel associated to the status of resetting a user password</returns> public ResponseModel ResetPassword(ResetPasswordDto resetPasswordDto) { var validationStatus = ValidateRequest.ValidateResetPasswordDto(resetPasswordDto); if (!(bool)validationStatus.First) { return(ReturnStatements.BadRequestResponse(validationStatus.Second)); } using (var unitOfWork = new UnitOfWork()) { var userList = unitOfWork.Users.FindOnConstraint(m => m.Handle.Equals(resetPasswordDto.Handle), prop => prop.UserPasswords).ToList(); //Check if no user exist. if (userList.Count.Equals(0)) { return(ReturnStatements.FailedResponse(Strings.UserDoNotExist)); } //Get user at first index. Since handles are unique , therefore only one record will be fetched from database. var user = userList[0]; if (!user.ForgotPasswordFlag) { return(ReturnStatements.FailedResponse(Strings.NoRequestCreated)); } if (user.ForgotPasswordCode.Equals(resetPasswordDto.VerificationCode)) { user.ForgotPasswordFlag = false; var userPassword = user.UserPasswords; var passwordToUpdate = userPassword.Where(m => m.Status.Equals(true)).ToList()[0]; passwordToUpdate.Password = resetPasswordDto.Password; passwordToUpdate.CreationDate = Functions.GetCurrentDate(); passwordToUpdate.CreationTime = Functions.GetCurrentTime(); unitOfWork.Users.Update(user); unitOfWork.UserPasswords.Update(passwordToUpdate); var saveResponse = unitOfWork.Complete(); if (saveResponse.Equals(Integers.UnsuccessfullDatabaseSave)) { return(ReturnStatements.FailedResponse(DynamicListForResponse.Create(resetPasswordDto))); } return(ReturnStatements.SuccessResponse(DynamicListForResponse.Create(resetPasswordDto))); } return(ReturnStatements.FailedResponse(Strings.VerificationCodeMismatch)); } }
/// <summary> /// User service to verify a new user. The method check the verification code in database saved in database against the verificationCode sent by user. /// </summary> /// <param name="verificationDto">verificationDto contains verification details of the user</param> /// <returns>ResponseModel is the model sent associated to the verification process.</returns> public ResponseModel VerifyUser(VerificationDto verificationDto) { var validationStatus = ValidateRequest.ValidateVerificationDto(verificationDto); if (!(bool)validationStatus.First) { return(ReturnStatements.BadRequestResponse(validationStatus.Second)); } verificationDto = validationStatus.Second; using (var unitOfWork = new UnitOfWork()) { var userList = unitOfWork.Users.FindOnConstraint(m => m.Handle.Equals(verificationDto.Handle), prop => prop.UserPasswords).ToList(); //If userList.Count is 0 then user does not exist. Zero index is used in this method because initially there is only one user and one password. if (userList.Count.Equals(0)) { return(ReturnStatements.FailedResponse(Strings.UserDoNotExist)); } var userPassword = userList[0].UserPasswords.ToList()[0]; if (!userPassword.Password.Equals(verificationDto.Password)) { return(ReturnStatements.FailedResponse(Strings.PasswordMismatch)); } if (userList[0].VerificationCode.Equals(verificationDto.VerificationCode)) { userList[0].IsVerified = true; unitOfWork.Users.Update(userList[0]); var saveResponse = unitOfWork.Complete(); if (saveResponse.Equals(Integers.UnsuccessfullDatabaseSave)) { return(ReturnStatements.FailedResponse(Strings.UnsuccessfullVerification)); } return(ReturnStatements.SuccessResponse(Strings.SuccessfullVerification)); } return(ReturnStatements.FailedResponse(Strings.VerificationCodeMismatch)); } }
/// <summary> /// Method to add patient insurance to database. /// </summary> /// <param name="newpatientInsurance">newpatientInsurance containing required information of patient insurance</param> /// <param name="userId">userId is the id of the logged in user who made the request to add a patient insurance</param> /// <returns>HttpResponseMessage containing addition status of patient insurance</returns> public ResponseModel Add(AddPatientInsurance newPatientInsurance, int userId) { var validationStatus = ValidateRequest.ValidatePatientInsuranceDto(newPatientInsurance); if (!(bool)validationStatus.First) { return(ReturnStatements.BadRequestResponse(validationStatus.Second)); } newPatientInsurance = validationStatus.Second; using (var unitOfWork = new UnitOfWork()) { var dbPatientInsurance = DtoToDatabase.PatientInsurance(new Pair { First = newPatientInsurance, Second = userId }); unitOfWork.PatientInsurances.Add(dbPatientInsurance); var saveResponse = unitOfWork.Complete(); if (saveResponse.Equals(Integers.UnsuccessfullDatabaseSave)) { return(ReturnStatements.FailedResponse(DynamicListForResponse.Create(newPatientInsurance))); } return(ReturnStatements.SuccessResponse(DynamicListForResponse.Create(newPatientInsurance))); } }