Skip to content

Comments

Fixes #71: Create API for patient profile#76

Open
rray524 wants to merge 2 commits intomainfrom
rray524/create-api-patient-profile
Open

Fixes #71: Create API for patient profile#76
rray524 wants to merge 2 commits intomainfrom
rray524/create-api-patient-profile

Conversation

@rray524
Copy link
Collaborator

@rray524 rray524 commented Dec 23, 2024

Checklist:

  • I have created CRUD api for creating, updating, deleting and getting a single patient profile.
  • I have created user type as patient when I will create new user while I will register user.

Resolves #71

How to create user Rails console?

user_data = {
  email: "rray52490@gmail.com",
  password: "420993121644@Rohit~gmail.com",
  password_confirmation: "420993121644@Rohit~gmail.com",
  user_type: "patient"
}
user = User.create!(user_data)

How to create Patient profile in rails console:

# Address parameters
address_params = {
  address_line_1: "123 Street",
  address_line_2: "Suite 101",
  city: "Metropolis",
  state: "Stateville",
  country: "Countryland",
  pincode: "123456"
}
address = Address.create!(address_params)

# Establishment parameters (optional; fallback to defaults)
establishment_params = {
  name: "Best Hospital",
  latitude: 12.34,
  longitude: 56.78,
  maps_location: "https://maps.example.com/custom_location"
}
establishment_defaults = {
  name: "Default Hospital",
  latitude: 0.0,
  longitude: 0.0,
  maps_location: "https://maps.example.com/default_location"
}
establishment = Establishment.create!(
  establishment_defaults.merge(establishment_params).merge(address: address)
)

# Patient profile parameters
patient_params = {
  name: "John Doe",
  gender: "Male",
  dob: "1990-01-01",
  blood_group: "O+"
}
current_user = User.find_by(email: "current_user_email@example.com") # Replace with your current user's email
patient_profile = PatientProfile.create!(
  patient_params.merge(address: address, establishment: establishment, user_id: current_user.id)
)

# Update the current user's patient_profile_id
current_user.update!(patient_profile_id: patient_profile.id)

# Confirm creation
puts "Patient Profile Created: #{patient_profile.inspect}"

How to fetch all profiles with optional filters in rails console:

patient_profiles = PatientProfile.includes(:address, :user)

# Apply filters if needed
patient_profiles = patient_profiles.where("name ILIKE ?", "%John%") if params[:name].present?
patient_profiles = patient_profiles.joins(:address).where("addresses.city ILIKE ?", "%Metropolis%") if params[:city].present?

# Inspect the results
puts patient_profiles.map { |p| p.as_json(include: { address: {}, user: { only: [:id, :email, :patient_profile_id] } }) }

How to fetch a single patient profile:

current_user = User.find_by(email: "current_user_email@example.com") # Replace with your current user's email

patient_profile = PatientProfile.includes(:address, :user).find_by(id: 1, user_id: current_user.id) # Replace 1 with the patient profile ID

if patient_profile
  puts patient_profile.as_json(include: {
    address: { only: [:id, :address_line_1, :city, :state, :country, :pincode] },
    user: { only: [:id, :email, :patient_profile_id] }
  })
else
  puts "Patient profile not found or not authorized."
end

How to delete a patient profile:

current_user = User.find_by(email: "current_user_email@example.com") # Replace with your current user's email

patient_profile = PatientProfile.find_by(id: 1, user_id: current_user.id) # Replace 1 with the patient profile ID

if patient_profile
  patient_profile.destroy
  puts "Patient profile deleted successfully."
else
  puts "Patient profile not found or not authorized."
end

How to update a patient profile:

current_user = User.find_by(email: "current_user_email@example.com") # Replace with your current user's email

patient_profile = PatientProfile.find_by(id: 1, user_id: current_user.id) # Replace 1 with the patient profile ID

if patient_profile
  # Patient profile parameters
  patient_profile_params = { name: "Jane Doe", gender: "Female", blood_group: "A-" }
  address_params = { city: "New City", state: "New State", pincode: "654321" }

  if patient_profile.update(patient_profile_params) && patient_profile.address.update(address_params)
    puts "Patient profile updated successfully."
    puts patient_profile.as_json(include: :address)
  else
    puts "Failed to update patient profile: #{patient_profile.errors.full_messages + patient_profile.address.errors.full_messages}"
  end
else
  puts "Patient profile not found or not authorized."
end

  • Replace current_user_email@example.com with the actual email of the user performing the operation.
  • Replace profile IDs (1) in queries with the actual IDs you want to target

Description

This CRUD operation allows users to manage Patient Profiles efficiently. Users can create a profile with associated address and establishment details, ensuring data integrity through transactional operations. It supports filtering and retrieving patient profiles with flexible search criteria. Additionally, users can update or delete profiles securely, with access control to ensure only authorized users can perform modifications.

@rray524 rray524 marked this pull request as ready for review December 23, 2024 11:22
@rray524 rray524 requested a review from rt4914 as a code owner December 23, 2024 11:22
@rray524 rray524 requested review from rt4914 and removed request for rt4914 December 23, 2024 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Create API for PatientProfile

2 participants