2

In my Spring boot project, I have two tables names- doctors and patients. In these tables, I have following attributes-

enter image description here

enter image description here

Now, the thing is I want to create a many to many relation between these two tables for appointment as one doctor can have many patients and one patient can have appointment of multiple doctors. So, to handle this problem, I have created another table named appointment which will have doctorId and patientId as foreign key.

I need to create appointment by using JSON request body like below-

enter image description here

So, for this purpose I have created a model class like below-

Appointment.java

@Entity
@Table(name = "appointments")
public class Appointment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private Long appointedDoctorId;

    @NotNull
    private Long appointedPatientId;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "doctorId", nullable = false)
    private Doctor doctor;


    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "patientId", nullable = false)
    private Patient patient;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public Long getAppointedDoctorId() {
        return appointedDoctorId;
    }

    public void setAppointedDoctorId(Long appointedDoctorId) {
        this.appointedDoctorId = appointedDoctorId;
    }

    public Long getAppointedPatientId() {
        return appointedPatientId;
    }

    public void setAppointedPatientId(Long appointedPatientId) {
        this.appointedPatientId = appointedPatientId;
    }

    public Doctor getDoctor() {
        return doctor;
    }

    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }
}

Here's the Repository class-

AppointmentRepository .java

@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {

    Page<Appointment> findByDoctorId(Long id, Pageable pageable);

}

Here's my Service interface-

AppointmentService .java

public interface AppointmentService {

    public Page<Appointment> getAllAppointmentByDoctorId(@PathVariable(value = "doctorId") Long id, Pageable pageable);

    public Appointment createAppointment(@Valid @RequestBody Appointment createAppointmentRequest);

}

Here's the implementation of my Service class-

AppointmentServiceImpl.java

@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    AppointmentRepository appointmentRepository;


    @Override
    public Page<Appointment> getAllAppointmentByDoctorId(Long id, Pageable pageable) {
        return appointmentRepository.findByDoctorId(id, pageable);
    }

    @Override
    public Appointment createAppointment(@Valid Appointment createAppointmentRequest) {
        return appointmentRepository.save(createAppointmentRequest);
    }
}

And at last I have this Controller class-

Appointment.java

@RestController
@RequestMapping("/api")
public class AppointmentController {

    @Autowired
    AppointmentService appointmentService;

    @GetMapping("/doctors/{doctorId}/appointments")
    public Page<Appointment> getAllAppointmentsByDoctorId(@PathVariable(value = "id") Long id, Pageable pageable){
        return appointmentService.getAllAppointmentByDoctorId(id, pageable);
    }


    @PostMapping("/insert/new/appointments")
    public Appointment createAppointment(@Valid Appointment createAppointmentRequest) {
        return appointmentService.createAppointment(createAppointmentRequest);
    }

}

But whenever I run the project and do POST request for creating the Appointment using the mentioned request body, it is showing the following response-

enter image description here

So, I need to know what's the problem in my code and how can I create an appointment POST request by giving the doctorId and patientId like the way I mentioned in JSON RequestBody request.

S. M. Asif
  • 3,437
  • 10
  • 34
  • 58

0 Answers0