an image of code for some reason
I am trying to have a little API run. Everything runs fine but I have to make an addition and am confused. I have to have it so in the URL when I type "localhost:8080/api/v1/{illness}" it will display the names of the people with that illness...help. I have included the two classes that I have. I didn't include my patient class. It's just the constructors and getters/setters for name, gender, illness, and id. I also didn't include the main well because its not needed
package com.sw409.Patientdemo.controller;
import java.util.List;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.sw409.Patientdemo.model.Patient;
import com.sw409.Patientdemo.service.PatientService;
@RestController
public class PatientController {
PatientService patService = new PatientService();
// CREATE
@PostMapping("api/v1/patients")
public Patient addPatient(@RequestBody Patient p) {
return patService.addPatient(p);
}
//GET ILLNESS (HELP!!!!!!!!!!!!!!!!!!!!!!!)
@PostMapping("api/v1/patients/{illness}")
public void patientIllness() {
return patService.patientIllness(name, illness)
}
// READ
@GetMapping("api/v1/patients")
public List<Patient> getPatient() {
return patService.getPatients();
}
// UPDATE
@PatchMapping("api/v1/patient/{patid}")
public void updatePatient(@PathVariable("patid") Integer id, @RequestBody Patient p) {
patService.updatePatient(id, p);
}
// DELETE
@DeleteMapping("api/v1/patient/{patid}")
public void deletePatient(@PathVariable("patid") Integer id) {
patService.deletePatient(id);
}
}
package com.sw409.Patientdemo.service;
import java.util.*;
import com.sw409.Patientdemo.model.Patient;
public class PatientService {
List<Patient> patientList = new ArrayList<>();
// Create
public Patient addPatient(Patient pat) {
patientList.add(pat);
return pat;
}
public List<Patient> getPatients() {
return patientList;
}
//(HELPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP)
public void patientIllness(String illness) {
for (int i = 0; i < patientList.size(); i ) {
if (patientList.get(i).equals(illness){
}
}
}
public void updatePatient(Integer id, Patient p) {
for (int i = 0; i < patientList.size(); i ) {
if (patientList.get(i).getId().equals(id)) {
patientList.set(i, p);
}
}
}
public void deletePatient(Integer id) {
for (int i = 0; i < patientList.size(); i ) {
if (patientList.get(i).getId().equals(id)) {
patientList.remove(i);
}
}
}
}
package com.sw409.Patientdemo.model;
public class Patient {
String name;
Integer id;
String gender;
String illness;
public Patient(String name, Integer id, String gender, String illness) {
super();
this.name = name;
this.id = id;
this.gender = gender;
this.illness = illness;
}
public Patient() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getIllness() {
return illness;
}
public void setIllness(String illness) {
this.illness = illness;
}
}
CodePudding user response:
Controller
In case you want to get the value of something you should use GET instead of POST because POST use for creating or saving something
Your code:
@PostMapping("api/v1/patients/{illness}")
public void patientIllness() {
return patService.patientIllness(name, illness)
}
- The parameter is missing
patientIllness()
that's why it cannot pass the value to service - The return type is void so you still cannot get anything
Suggestion:
@GetMapping("api/v1/patients/{illness}")
public List<Patient> patientIllness(@PathVariable String illness) {
return patService.patientIllness(illness);
}
- Add paramiter in
patientIllness(String illness)
and you use it with path param so it will bepatientIllness(@PathVariable String illness)
- The return type should be something that you want to know if you want to know the List of Patients, return should be
List<Patient>
Service
Your code:
public void patientIllness(String illness) {
for (int i = 0; i < patientList.size(); i ) {
if (patientList.get(i).equals(illness){
}
}
}
- The return type is void so you have change it if you want to return something to controller
- It's worng in this code
patientList.get(i).equals(illness)
Type ofpatientList.get(i)
is Patient and you compare it with String
Suggestion1:
public List<Patient> patientIllness(String illness) {
List<Patient> result = new ArrayList<>();
for (int i = 0; i < patientList.size(); i ) {
if (patientList.get(i).getIllness().equals(illness)) {
result.add(patientList.get(i));
}
}
return result;
}
- The return type will be
List<Patient>
- You should compare with the same type
patientList.get(i).getIllness().equals(illness)
Suggestion2: you can change to for each
public List<Patient> patientIllness(String illness) {
List<Patient> result = new ArrayList<>();
for (Patient patient : patientList) {
if (patient.getIllness().equals(illness)) {
result.add(patient);
}
}
return result;
}
Suggestion3: You can change it to use steam, filter, and collect it to list
public List<Patient> patientIllness(String illness) {
return patientList.stream()
.filter(patient -> patient.getIllness().equals(illness))
.collect(Collectors.toList());
}
CodePudding user response:
First of all you need to change your controller function to something like this:
@PostMapping("api/v1/patients/{illness}")
public List<Patient> patientIllness(@PathVariable String illness) {
return patService.patientIllness(illness)
}
I don't understand why you called your service function with two arguments, because it can accept only one. You also need to make this function return list of patients instead of void. And illness is just a path variable, you use it in different functions, so you should understand it. And in your patient service you just need to use filter function, so it will look similiar to this:
public List<Patient> patientIllness(String illness) {
return patientList.stream().filter(patient->patient.getIllness().equals(illness)).collect(Collectors.toList());
}