From f28e6845646c527dc5718ed3d25b605dfd5cd63e Mon Sep 17 00:00:00 2001 From: rray524 Date: Wed, 30 Oct 2024 20:56:11 +0530 Subject: [PATCH 1/5] api-get-doctors-services-establishments --- app/controllers/api/v1/doctors_controller.rb | 44 +++++++++++++++++++ .../api/v1/establishments_controller.rb | 12 +++++ app/controllers/api/v1/services_controller.rb | 12 +++++ app/models/doctor_profile.rb | 8 ++++ app/models/service.rb | 7 +++ config/routes.rb | 9 ++++ ...41029134526_add_city_to_doctor_profiles.rb | 5 +++ ...241029134608_add_city_to_establishments.rb | 5 +++ db/schema.rb | 4 +- 9 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/doctors_controller.rb create mode 100644 app/controllers/api/v1/establishments_controller.rb create mode 100644 app/controllers/api/v1/services_controller.rb create mode 100644 db/migrate/20241029134526_add_city_to_doctor_profiles.rb create mode 100644 db/migrate/20241029134608_add_city_to_establishments.rb diff --git a/app/controllers/api/v1/doctors_controller.rb b/app/controllers/api/v1/doctors_controller.rb new file mode 100644 index 0000000..1053bf2 --- /dev/null +++ b/app/controllers/api/v1/doctors_controller.rb @@ -0,0 +1,44 @@ +# app/controllers/api/v1/doctor_controller.rb +module Api + module V1 + class DoctorsController < ApplicationController + def create + doctor_profile_params = params.require(:doctor_profile).permit( + :name, :specialization_id, :degree_id, :institute_id, :city, :gender, :date_of_birth, + establishment_ids: [], + services_attributes: [:name, :day_of_week, :amount_cents, :establishment_id, :start_time, :end_time] + ) + + doctor_profile = DoctorProfile.new(doctor_profile_params) + + if doctor_profile.save + render json: { message: 'Doctor profile created successfully', doctor: doctor_profile }, status: :created + else + render json: { errors: doctor_profile.errors.full_messages }, status: :unprocessable_entity + end + end + def by_city + city = params[:city] + + # Fetch all establishments in the specified city + establishments = Establishment.where(city: city) + + # Fetch doctor profiles through DoctorEstablishment join table + doctor_profiles = DoctorProfile.joins(:doctor_establishments) + .where(doctor_establishments: { establishment_id: establishments.pluck(:id) }) + + # Fetch services related to doctors and establishments in the specified city + services = Service.joins(:doctor_profile, :establishment) + .where(doctor_profiles: { id: doctor_profiles.pluck(:id) }) + .where(establishments: { city: city }) + + render json: { + doctors: doctor_profiles, + establishments: establishments, + services: services + } + end + end + end + end + \ No newline at end of file diff --git a/app/controllers/api/v1/establishments_controller.rb b/app/controllers/api/v1/establishments_controller.rb new file mode 100644 index 0000000..67f822d --- /dev/null +++ b/app/controllers/api/v1/establishments_controller.rb @@ -0,0 +1,12 @@ +# app/controllers/api/v1/establishments_controller.rb +module Api + module V1 + class EstablishmentsController < ApplicationController + def index + establishments = Establishment.all + render json: establishments, status: :ok + end + end + end + end + \ No newline at end of file diff --git a/app/controllers/api/v1/services_controller.rb b/app/controllers/api/v1/services_controller.rb new file mode 100644 index 0000000..adf297b --- /dev/null +++ b/app/controllers/api/v1/services_controller.rb @@ -0,0 +1,12 @@ +# app/controllers/api/v1/services_controller.rb +module Api + module V1 + class ServicesController < ApplicationController + def index + services = Service.all + render json: services, status: :ok + end + end + end + end + \ No newline at end of file diff --git a/app/models/doctor_profile.rb b/app/models/doctor_profile.rb index f5f3393..033f7b3 100644 --- a/app/models/doctor_profile.rb +++ b/app/models/doctor_profile.rb @@ -3,4 +3,12 @@ class DoctorProfile < ApplicationRecord belongs_to :degree, optional: true belongs_to :institute, optional: true has_one :user + + # Association for the join table + has_many :doctor_establishments + has_many :establishments, through: :doctor_establishments + + # Define the association with Service + has_many :services + accepts_nested_attributes_for :services end \ No newline at end of file diff --git a/app/models/service.rb b/app/models/service.rb index 8fe9cb8..67bd1b7 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -3,5 +3,12 @@ class Service < ApplicationRecord belongs_to :establishment enum day_of_week: { Monday: 0, Tuesday: 1, Wednesday: 2, Thursday: 3, Friday: 4, Saturday: 5, Sunday: 6 } monetize :amount_cents + + validates :name, presence: true + validates :day_of_week, presence: true + validates :amount_cents, presence: true + validates :establishment_id, presence: true + validates :start_time, presence: true + validates :end_time, presence: true end diff --git a/config/routes.rb b/config/routes.rb index 7703399..5f395e2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,4 +15,13 @@ # Defines the root path route ("/") # root "posts#index" + + namespace :api do + namespace :v1 do + get 'doctors/by_city', to: 'doctors#by_city' + post 'doctors', to: 'doctors#create' + resources :services, only: [:index] # This line adds the GET /api/v1/services route + resources :establishments, only: [:index] # This line adds the GET /api/v1/establishments route + end + end end diff --git a/db/migrate/20241029134526_add_city_to_doctor_profiles.rb b/db/migrate/20241029134526_add_city_to_doctor_profiles.rb new file mode 100644 index 0000000..4213aef --- /dev/null +++ b/db/migrate/20241029134526_add_city_to_doctor_profiles.rb @@ -0,0 +1,5 @@ +class AddCityToDoctorProfiles < ActiveRecord::Migration[7.1] + def change + add_column :doctor_profiles, :city, :string + end +end diff --git a/db/migrate/20241029134608_add_city_to_establishments.rb b/db/migrate/20241029134608_add_city_to_establishments.rb new file mode 100644 index 0000000..58c6d4d --- /dev/null +++ b/db/migrate/20241029134608_add_city_to_establishments.rb @@ -0,0 +1,5 @@ +class AddCityToEstablishments < ActiveRecord::Migration[7.1] + def change + add_column :establishments, :city, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 50b2b64..28bca48 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_10_25_084443) do +ActiveRecord::Schema[7.1].define(version: 2024_10_29_134608) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -63,6 +63,7 @@ t.integer "year_of_experience" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "city" t.index ["degree_id"], name: "index_doctor_profiles_on_degree_id" t.index ["institute_id"], name: "index_doctor_profiles_on_institute_id" t.index ["specialization_id"], name: "index_doctor_profiles_on_specialization_id" @@ -76,6 +77,7 @@ t.string "maps_location" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "city" t.index ["address_id"], name: "index_establishments_on_address_id" end From 4c615fcd82d419ec469f68a991bacd93686b506b Mon Sep 17 00:00:00 2001 From: rray524 Date: Mon, 4 Nov 2024 18:07:09 +0530 Subject: [PATCH 2/5] api-get-doctors-services-establishments-1.0 --- app/controllers/api/v1/doctors_controller.rb | 132 +++++++++++++++++- .../api/v1/establishments_controller.rb | 6 +- app/controllers/api/v1/services_controller.rb | 15 +- app/models/doctor_profile.rb | 2 +- app/models/payment.rb | 1 - app/models/service.rb | 9 +- config/routes.rb | 10 ++ ...013732_remove_city_from_doctor_profiles.rb | 5 + ...104023408_add_address_to_establishments.rb | 5 + db/schema.rb | 4 +- 10 files changed, 176 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20241104013732_remove_city_from_doctor_profiles.rb create mode 100644 db/migrate/20241104023408_add_address_to_establishments.rb diff --git a/app/controllers/api/v1/doctors_controller.rb b/app/controllers/api/v1/doctors_controller.rb index 1053bf2..57b12c3 100644 --- a/app/controllers/api/v1/doctors_controller.rb +++ b/app/controllers/api/v1/doctors_controller.rb @@ -2,9 +2,79 @@ module Api module V1 class DoctorsController < ApplicationController + def all_data + # Fetch all doctor profiles + doctor_profiles = DoctorProfile.includes(:services, :establishments) + + # Fetch all services + services = Service.includes(:doctor_profile, :establishment) + + # Fetch all establishments + establishments = Establishment.all + + # Render the response with structured data + render json: { + doctors: doctor_profiles.map { |doctor| + { + id: doctor.id, + name: doctor.name, + specialization_id: doctor.specialization_id, + degree_id: doctor.degree_id, + institute_id: doctor.institute_id, + gender: doctor.gender, + date_of_birth: doctor.date_of_birth, + year_of_experience: doctor.year_of_experience, + establishments: doctor.establishments.map { |establishment| + { + id: establishment.id, + name: establishment.name, + city: establishment.city, + address: establishment.address + } + }, + services: doctor.services.map { |service| + { + id: service.id, + name: service.name, + day_of_week: service.day_of_week, + amount_cents: service.amount_cents, + amount_currency: service.amount_currency, + start_time: service.start_time, + end_time: service.end_time, + establishment_id: service.establishment_id, + city: service.establishment.city, # Accessing city from the establishment + doctor_name: doctor.name # Accessing doctor's name + } + } + } + }, + services: services.map { |service| + { + id: service.id, + name: service.name, + day_of_week: service.day_of_week, + amount_cents: service.amount_cents, + amount_currency: service.amount_currency, + start_time: service.start_time, + end_time: service.end_time, + establishment_id: service.establishment_id, + city: service.establishment.city, + doctor_name: service.doctor_profile.name + } + }, + establishments: establishments.map { |establishment| + { + id: establishment.id, + name: establishment.name, + city: establishment.city, + address: establishment.address + } + } + }, status: :ok + end def create doctor_profile_params = params.require(:doctor_profile).permit( - :name, :specialization_id, :degree_id, :institute_id, :city, :gender, :date_of_birth, + :name, :specialization_id, :degree_id, :institute_id, :gender, :date_of_birth, establishment_ids: [], services_attributes: [:name, :day_of_week, :amount_cents, :establishment_id, :start_time, :end_time] ) @@ -17,6 +87,46 @@ def create render json: { errors: doctor_profile.errors.full_messages }, status: :unprocessable_entity end end + def index + # Fetch all doctor profiles and preload related establishments and services + doctor_profiles = DoctorProfile.includes(:services, :establishments) + + # Render the response with nested data + render json: doctor_profiles.map { |doctor| + { + id: doctor.id, + name: doctor.name, + specialization_id: doctor.specialization_id, + degree_id: doctor.degree_id, + institute_id: doctor.institute_id, + gender: doctor.gender, + date_of_birth: doctor.date_of_birth, + year_of_experience: doctor.year_of_experience, + establishments: doctor.establishments.map { |establishment| + { + id: establishment.id, + name: establishment.name, + city: establishment.city, + address: establishment.address # Include other fields as necessary + } + }, + services: doctor.services.map { |service| + establishment = service.establishment + { + id: service.id, + name: service.name, + day_of_week: service.day_of_week, + amount_cents: service.amount_cents, + amount_currency: service.amount_currency, + start_time: service.start_time, + end_time: service.end_time, + establishment_id: service.establishment_id, + city: establishment&.city + } + } + } + }, status: :ok + end def by_city city = params[:city] @@ -31,14 +141,28 @@ def by_city services = Service.joins(:doctor_profile, :establishment) .where(doctor_profiles: { id: doctor_profiles.pluck(:id) }) .where(establishments: { city: city }) - + + # Format the services to include the city from the establishment + formatted_services = services.map do |service| + { + id: service.id, + name: service.name, + day_of_week: service.day_of_week, + amount_cents: service.amount_cents, + amount_currency: service.amount_currency, + start_time: service.start_time, + end_time: service.end_time, + establishment_id: service.establishment_id, + city: service.establishment.city, + doctor_name: service.doctor_profile.name + } + end render json: { doctors: doctor_profiles, establishments: establishments, - services: services + services: formatted_services } end end end end - \ No newline at end of file diff --git a/app/controllers/api/v1/establishments_controller.rb b/app/controllers/api/v1/establishments_controller.rb index 67f822d..ade7fe1 100644 --- a/app/controllers/api/v1/establishments_controller.rb +++ b/app/controllers/api/v1/establishments_controller.rb @@ -6,7 +6,11 @@ def index establishments = Establishment.all render json: establishments, status: :ok end + def by_city + city = params[:city] + establishments = Establishment.where(city: city) + render json: establishments, status: :ok + end end end end - \ No newline at end of file diff --git a/app/controllers/api/v1/services_controller.rb b/app/controllers/api/v1/services_controller.rb index adf297b..33e0f58 100644 --- a/app/controllers/api/v1/services_controller.rb +++ b/app/controllers/api/v1/services_controller.rb @@ -3,10 +3,19 @@ module Api module V1 class ServicesController < ApplicationController def index - services = Service.all - render json: services, status: :ok + services = Service.includes(:establishment).all + + render json: services.as_json( + only: [:id, :doctor_profile_id, :name, :day_of_week, :start_time, :end_time, :slot_length_in_minutes, :amount_cents, :amount_currency], + methods: :city_name + ), status: :ok + end + def by_city + city = params[:city] + services = Service.includes(:establishment).by_city(city) + + render json: services.as_json(include: { establishment: { only: :city } }) end end end end - \ No newline at end of file diff --git a/app/models/doctor_profile.rb b/app/models/doctor_profile.rb index 033f7b3..db0180d 100644 --- a/app/models/doctor_profile.rb +++ b/app/models/doctor_profile.rb @@ -11,4 +11,4 @@ class DoctorProfile < ApplicationRecord # Define the association with Service has_many :services accepts_nested_attributes_for :services -end \ No newline at end of file +end diff --git a/app/models/payment.rb b/app/models/payment.rb index d7493ce..a7b119b 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -11,4 +11,3 @@ class Payment < ApplicationRecord validates :stripe_id, presence: true, uniqueness: true validates :appointment_id, presence: true, uniqueness: true end - diff --git a/app/models/service.rb b/app/models/service.rb index 67bd1b7..3bb27e7 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -4,6 +4,14 @@ class Service < ApplicationRecord enum day_of_week: { Monday: 0, Tuesday: 1, Wednesday: 2, Thursday: 3, Friday: 4, Saturday: 5, Sunday: 6 } monetize :amount_cents + # Method to include city name in JSON response + def city_name + establishment&.city + end + + # Scope to filter services by city name + scope :by_city, ->(city_name) { joins(:establishment).where(establishments: { city: city_name }) } + validates :name, presence: true validates :day_of_week, presence: true validates :amount_cents, presence: true @@ -11,4 +19,3 @@ class Service < ApplicationRecord validates :start_time, presence: true validates :end_time, presence: true end - diff --git a/config/routes.rb b/config/routes.rb index 5f395e2..5db3116 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,10 +18,20 @@ namespace :api do namespace :v1 do + resources :doctors, only: [:index] do + collection do + get 'all_data' + end + end get 'doctors/by_city', to: 'doctors#by_city' post 'doctors', to: 'doctors#create' resources :services, only: [:index] # This line adds the GET /api/v1/services route resources :establishments, only: [:index] # This line adds the GET /api/v1/establishments route + + # Custom route for establishments by city + get 'establishments/by_city', to: 'establishments#by_city' + # Custom route for services by city + get 'services/by_city', to: 'services#by_city' end end end diff --git a/db/migrate/20241104013732_remove_city_from_doctor_profiles.rb b/db/migrate/20241104013732_remove_city_from_doctor_profiles.rb new file mode 100644 index 0000000..af6c45b --- /dev/null +++ b/db/migrate/20241104013732_remove_city_from_doctor_profiles.rb @@ -0,0 +1,5 @@ +class RemoveCityFromDoctorProfiles < ActiveRecord::Migration[7.1] + def change + remove_column :doctor_profiles, :city, :string + end +end diff --git a/db/migrate/20241104023408_add_address_to_establishments.rb b/db/migrate/20241104023408_add_address_to_establishments.rb new file mode 100644 index 0000000..e5a5588 --- /dev/null +++ b/db/migrate/20241104023408_add_address_to_establishments.rb @@ -0,0 +1,5 @@ +class AddAddressToEstablishments < ActiveRecord::Migration[7.1] + def change + add_column :establishments, :address, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 28bca48..4d516c4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_10_29_134608) do +ActiveRecord::Schema[7.1].define(version: 2024_11_04_023408) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -63,7 +63,6 @@ t.integer "year_of_experience" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "city" t.index ["degree_id"], name: "index_doctor_profiles_on_degree_id" t.index ["institute_id"], name: "index_doctor_profiles_on_institute_id" t.index ["specialization_id"], name: "index_doctor_profiles_on_specialization_id" @@ -78,6 +77,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "city" + t.string "address" t.index ["address_id"], name: "index_establishments_on_address_id" end From 688867352152e13166eceedd3d64559813fe1c71 Mon Sep 17 00:00:00 2001 From: rray524 Date: Wed, 6 Nov 2024 15:45:17 +0530 Subject: [PATCH 3/5] api-get-doctors-services-establishments-1.1 --- app/controllers/api/v1/doctors_controller.rb | 17 ++++++++--------- .../api/v1/establishments_controller.rb | 1 - app/controllers/api/v1/services_controller.rb | 1 - app/models/service.rb | 4 ++-- config/routes.rb | 8 ++++---- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/app/controllers/api/v1/doctors_controller.rb b/app/controllers/api/v1/doctors_controller.rb index 57b12c3..50c7a04 100644 --- a/app/controllers/api/v1/doctors_controller.rb +++ b/app/controllers/api/v1/doctors_controller.rb @@ -1,18 +1,17 @@ -# app/controllers/api/v1/doctor_controller.rb module Api module V1 class DoctorsController < ApplicationController def all_data - # Fetch all doctor profiles + doctor_profiles = DoctorProfile.includes(:services, :establishments) - # Fetch all services + services = Service.includes(:doctor_profile, :establishment) - # Fetch all establishments + establishments = Establishment.all - # Render the response with structured data + render json: { doctors: doctor_profiles.map { |doctor| { @@ -130,19 +129,19 @@ def index def by_city city = params[:city] - # Fetch all establishments in the specified city + establishments = Establishment.where(city: city) - # Fetch doctor profiles through DoctorEstablishment join table + doctor_profiles = DoctorProfile.joins(:doctor_establishments) .where(doctor_establishments: { establishment_id: establishments.pluck(:id) }) - # Fetch services related to doctors and establishments in the specified city + services = Service.joins(:doctor_profile, :establishment) .where(doctor_profiles: { id: doctor_profiles.pluck(:id) }) .where(establishments: { city: city }) - # Format the services to include the city from the establishment + formatted_services = services.map do |service| { id: service.id, diff --git a/app/controllers/api/v1/establishments_controller.rb b/app/controllers/api/v1/establishments_controller.rb index ade7fe1..76ed440 100644 --- a/app/controllers/api/v1/establishments_controller.rb +++ b/app/controllers/api/v1/establishments_controller.rb @@ -1,4 +1,3 @@ -# app/controllers/api/v1/establishments_controller.rb module Api module V1 class EstablishmentsController < ApplicationController diff --git a/app/controllers/api/v1/services_controller.rb b/app/controllers/api/v1/services_controller.rb index 33e0f58..7b704b8 100644 --- a/app/controllers/api/v1/services_controller.rb +++ b/app/controllers/api/v1/services_controller.rb @@ -1,4 +1,3 @@ -# app/controllers/api/v1/services_controller.rb module Api module V1 class ServicesController < ApplicationController diff --git a/app/models/service.rb b/app/models/service.rb index 3bb27e7..88ea2a6 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -4,12 +4,12 @@ class Service < ApplicationRecord enum day_of_week: { Monday: 0, Tuesday: 1, Wednesday: 2, Thursday: 3, Friday: 4, Saturday: 5, Sunday: 6 } monetize :amount_cents - # Method to include city name in JSON response + def city_name establishment&.city end - # Scope to filter services by city name + scope :by_city, ->(city_name) { joins(:establishment).where(establishments: { city: city_name }) } validates :name, presence: true diff --git a/config/routes.rb b/config/routes.rb index 5db3116..7abb513 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,12 +25,12 @@ end get 'doctors/by_city', to: 'doctors#by_city' post 'doctors', to: 'doctors#create' - resources :services, only: [:index] # This line adds the GET /api/v1/services route - resources :establishments, only: [:index] # This line adds the GET /api/v1/establishments route + resources :services, only: [:index] + resources :establishments, only: [:index] - # Custom route for establishments by city + get 'establishments/by_city', to: 'establishments#by_city' - # Custom route for services by city + get 'services/by_city', to: 'services#by_city' end end From 75af14d28b454e46f127a418ce25115b5afdb282 Mon Sep 17 00:00:00 2001 From: rray524 Date: Fri, 8 Nov 2024 01:20:46 +0530 Subject: [PATCH 4/5] api-get-doctors-services-establishments-1.2 --- app/controllers/api/v1/doctors_controller.rb | 54 ++++++++++--------- .../api/v1/establishments_controller.rb | 12 +++-- app/controllers/api/v1/services_controller.rb | 20 +++++-- app/models/address.rb | 2 + app/models/doctor_establishment.rb | 1 + app/models/doctor_profile.rb | 1 + app/models/establishment.rb | 2 + app/models/institute.rb | 1 + app/models/patient_profile.rb | 1 + app/models/registration_council.rb | 1 + app/models/service.rb | 10 ++-- config/routes.rb | 1 + ...41029134526_add_city_to_doctor_profiles.rb | 5 -- ...241029134608_add_city_to_establishments.rb | 5 -- ...013732_remove_city_from_doctor_profiles.rb | 5 -- ...104023408_add_address_to_establishments.rb | 5 -- db/schema.rb | 6 +-- 17 files changed, 70 insertions(+), 62 deletions(-) delete mode 100644 db/migrate/20241029134526_add_city_to_doctor_profiles.rb delete mode 100644 db/migrate/20241029134608_add_city_to_establishments.rb delete mode 100644 db/migrate/20241104013732_remove_city_from_doctor_profiles.rb delete mode 100644 db/migrate/20241104023408_add_address_to_establishments.rb diff --git a/app/controllers/api/v1/doctors_controller.rb b/app/controllers/api/v1/doctors_controller.rb index 50c7a04..0a01acc 100644 --- a/app/controllers/api/v1/doctors_controller.rb +++ b/app/controllers/api/v1/doctors_controller.rb @@ -1,15 +1,12 @@ +# frozen_string_literal: true module Api module V1 class DoctorsController < ApplicationController def all_data - doctor_profiles = DoctorProfile.includes(:services, :establishments) - - - services = Service.includes(:doctor_profile, :establishment) - - - establishments = Establishment.all + doctor_profiles = DoctorProfile.includes(services: { establishment: :address }, establishments: :address) + services = Service.includes(:doctor_profile, establishment: :address) + establishments = Establishment.includes(:address) render json: { @@ -27,8 +24,8 @@ def all_data { id: establishment.id, name: establishment.name, - city: establishment.city, - address: establishment.address + city: establishment.address&.city, + address: "#{establishment.address&.address_line_1}, #{establishment.address&.address_line_2}" } }, services: doctor.services.map { |service| @@ -41,8 +38,8 @@ def all_data start_time: service.start_time, end_time: service.end_time, establishment_id: service.establishment_id, - city: service.establishment.city, # Accessing city from the establishment - doctor_name: doctor.name # Accessing doctor's name + city: service.establishment&.address&.city, + doctor_name: doctor.name } } } @@ -57,7 +54,7 @@ def all_data start_time: service.start_time, end_time: service.end_time, establishment_id: service.establishment_id, - city: service.establishment.city, + city: service.establishment&.address&.city, doctor_name: service.doctor_profile.name } }, @@ -65,8 +62,8 @@ def all_data { id: establishment.id, name: establishment.name, - city: establishment.city, - address: establishment.address + city: establishment.address&.city, + address: "#{establishment.address&.address_line_1}, #{establishment.address&.address_line_2}" } } }, status: :ok @@ -87,10 +84,9 @@ def create end end def index - # Fetch all doctor profiles and preload related establishments and services - doctor_profiles = DoctorProfile.includes(:services, :establishments) - - # Render the response with nested data + + doctor_profiles = DoctorProfile.includes(services: { establishment: :address }, establishments: :address) + render json: doctor_profiles.map { |doctor| { id: doctor.id, @@ -105,8 +101,8 @@ def index { id: establishment.id, name: establishment.name, - city: establishment.city, - address: establishment.address # Include other fields as necessary + city: establishment.address&.city, + address: "#{establishment.address&.address_line_1}, #{establishment.address&.address_line_2}" } }, services: doctor.services.map { |service| @@ -120,7 +116,7 @@ def index start_time: service.start_time, end_time: service.end_time, establishment_id: service.establishment_id, - city: establishment&.city + city: service.establishment&.address&.city } } } @@ -130,7 +126,8 @@ def by_city city = params[:city] - establishments = Establishment.where(city: city) + establishments = Establishment.joins(:address) + .where(addresses: { city: city }) doctor_profiles = DoctorProfile.joins(:doctor_establishments) @@ -139,7 +136,7 @@ def by_city services = Service.joins(:doctor_profile, :establishment) .where(doctor_profiles: { id: doctor_profiles.pluck(:id) }) - .where(establishments: { city: city }) + .where(establishments: { id: establishments.pluck(:id) }) formatted_services = services.map do |service| @@ -152,13 +149,20 @@ def by_city start_time: service.start_time, end_time: service.end_time, establishment_id: service.establishment_id, - city: service.establishment.city, + city: service.establishment.address.city, doctor_name: service.doctor_profile.name } end render json: { doctors: doctor_profiles, - establishments: establishments, + establishments: establishments.map { |establishment| + { + id: establishment.id, + name: establishment.name, + city: establishment.address.city, + address: "#{establishment.address.address_line_1}, #{establishment.address.address_line_2}" + } + }, services: formatted_services } end diff --git a/app/controllers/api/v1/establishments_controller.rb b/app/controllers/api/v1/establishments_controller.rb index 76ed440..f185f6b 100644 --- a/app/controllers/api/v1/establishments_controller.rb +++ b/app/controllers/api/v1/establishments_controller.rb @@ -1,15 +1,17 @@ +# frozen_string_literal: true module Api module V1 class EstablishmentsController < ApplicationController def index - establishments = Establishment.all - render json: establishments, status: :ok + establishments = Establishment.includes(:address).all + render json: establishments.as_json(include: :address), status: :ok end + def by_city city = params[:city] - establishments = Establishment.where(city: city) - render json: establishments, status: :ok - end + establishments = Establishment.joins(:address).where(addresses: { city: city }) + render json: establishments.as_json(include: :address), status: :ok + end end end end diff --git a/app/controllers/api/v1/services_controller.rb b/app/controllers/api/v1/services_controller.rb index 7b704b8..00e2fb6 100644 --- a/app/controllers/api/v1/services_controller.rb +++ b/app/controllers/api/v1/services_controller.rb @@ -1,9 +1,10 @@ +# frozen_string_literal: true module Api module V1 class ServicesController < ApplicationController def index - services = Service.includes(:establishment).all - + services = Service.includes(establishment: :address).all + render json: services.as_json( only: [:id, :doctor_profile_id, :name, :day_of_week, :start_time, :end_time, :slot_length_in_minutes, :amount_cents, :amount_currency], methods: :city_name @@ -11,10 +12,19 @@ def index end def by_city city = params[:city] - services = Service.includes(:establishment).by_city(city) - - render json: services.as_json(include: { establishment: { only: :city } }) + services = Service.by_city(city) + + render json: services.as_json( + include: { + establishment: { + include: { address: { only: :city } }, + only: [:id, :name] + } + }, + only: [:id, :name, :doctor_profile_id, :day_of_week, :start_time, :end_time, :amount_cents, :amount_currency] + ), status: :ok end + end end end diff --git a/app/models/address.rb b/app/models/address.rb index a19addc..6ac5ed5 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true class Address < ApplicationRecord + has_many :establishments end diff --git a/app/models/doctor_establishment.rb b/app/models/doctor_establishment.rb index 8d1106e..94565df 100644 --- a/app/models/doctor_establishment.rb +++ b/app/models/doctor_establishment.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true class DoctorEstablishment < ApplicationRecord belongs_to :doctor_profile belongs_to :establishment diff --git a/app/models/doctor_profile.rb b/app/models/doctor_profile.rb index db0180d..c6f2e68 100644 --- a/app/models/doctor_profile.rb +++ b/app/models/doctor_profile.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true class DoctorProfile < ApplicationRecord belongs_to :specialization, optional: true belongs_to :degree, optional: true diff --git a/app/models/establishment.rb b/app/models/establishment.rb index a8fc9a0..5f2407f 100644 --- a/app/models/establishment.rb +++ b/app/models/establishment.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true class Establishment < ApplicationRecord + belongs_to :address end diff --git a/app/models/institute.rb b/app/models/institute.rb index e13f9a3..ccf7d97 100644 --- a/app/models/institute.rb +++ b/app/models/institute.rb @@ -1,2 +1,3 @@ +# frozen_string_literal: true class Institute < ApplicationRecord end diff --git a/app/models/patient_profile.rb b/app/models/patient_profile.rb index a98b3f7..1df020a 100644 --- a/app/models/patient_profile.rb +++ b/app/models/patient_profile.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true class PatientProfile < ApplicationRecord belongs_to :address, optional: true has_one :user diff --git a/app/models/registration_council.rb b/app/models/registration_council.rb index ee3f00a..ee9da36 100644 --- a/app/models/registration_council.rb +++ b/app/models/registration_council.rb @@ -1,2 +1,3 @@ +# frozen_string_literal: true class RegistrationCouncil < ApplicationRecord end diff --git a/app/models/service.rb b/app/models/service.rb index 88ea2a6..e6fa9f8 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true class Service < ApplicationRecord belongs_to :doctor_profile belongs_to :establishment @@ -5,13 +6,14 @@ class Service < ApplicationRecord monetize :amount_cents + scope :by_city, ->(city) { + joins(establishment: :address).where(addresses: { city: city }) + } + def city_name - establishment&.city + establishment&.address&.city end - - scope :by_city, ->(city_name) { joins(:establishment).where(establishments: { city: city_name }) } - validates :name, presence: true validates :day_of_week, presence: true validates :amount_cents, presence: true diff --git a/config/routes.rb b/config/routes.rb index 7abb513..dc578f1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true Rails.application.routes.draw do devise_for :users, controllers: { sessions: 'users/sessions', diff --git a/db/migrate/20241029134526_add_city_to_doctor_profiles.rb b/db/migrate/20241029134526_add_city_to_doctor_profiles.rb deleted file mode 100644 index 4213aef..0000000 --- a/db/migrate/20241029134526_add_city_to_doctor_profiles.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddCityToDoctorProfiles < ActiveRecord::Migration[7.1] - def change - add_column :doctor_profiles, :city, :string - end -end diff --git a/db/migrate/20241029134608_add_city_to_establishments.rb b/db/migrate/20241029134608_add_city_to_establishments.rb deleted file mode 100644 index 58c6d4d..0000000 --- a/db/migrate/20241029134608_add_city_to_establishments.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddCityToEstablishments < ActiveRecord::Migration[7.1] - def change - add_column :establishments, :city, :string - end -end diff --git a/db/migrate/20241104013732_remove_city_from_doctor_profiles.rb b/db/migrate/20241104013732_remove_city_from_doctor_profiles.rb deleted file mode 100644 index af6c45b..0000000 --- a/db/migrate/20241104013732_remove_city_from_doctor_profiles.rb +++ /dev/null @@ -1,5 +0,0 @@ -class RemoveCityFromDoctorProfiles < ActiveRecord::Migration[7.1] - def change - remove_column :doctor_profiles, :city, :string - end -end diff --git a/db/migrate/20241104023408_add_address_to_establishments.rb b/db/migrate/20241104023408_add_address_to_establishments.rb deleted file mode 100644 index e5a5588..0000000 --- a/db/migrate/20241104023408_add_address_to_establishments.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddAddressToEstablishments < ActiveRecord::Migration[7.1] - def change - add_column :establishments, :address, :string - end -end diff --git a/db/schema.rb b/db/schema.rb index 4d516c4..9aa10f3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_11_04_023408) do +ActiveRecord::Schema[7.1].define(version: 2024_11_07_142809) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -76,8 +76,6 @@ t.string "maps_location" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "city" - t.string "address" t.index ["address_id"], name: "index_establishments_on_address_id" end @@ -120,6 +118,8 @@ t.bigint "doctor_profile_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "rating", null: false + t.text "comment" t.index ["doctor_profile_id"], name: "index_reviews_on_doctor_profile_id" t.index ["patient_profile_id"], name: "index_reviews_on_patient_profile_id" end From 805a2487ce209d8b4c3b165a6884204cbb38b746 Mon Sep 17 00:00:00 2001 From: rray524 Date: Fri, 8 Nov 2024 01:39:03 +0530 Subject: [PATCH 5/5] api-get-doctors-services-establishments-1.4 --- db/schema.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 0a566bb..e7ebb7e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_11_07_142809) do +ActiveRecord::Schema[7.1].define(version: 2024_11_07_200448) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -76,8 +76,6 @@ t.string "maps_location" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "city" - t.string "address" t.index ["address_id"], name: "index_establishments_on_address_id" end @@ -122,8 +120,6 @@ t.datetime "updated_at", null: false t.integer "rating", null: false t.text "comment" - t.integer "rating", null: false - t.text "comment" t.index ["doctor_profile_id"], name: "index_reviews_on_doctor_profile_id" t.index ["patient_profile_id"], name: "index_reviews_on_patient_profile_id" end