Spring Boot Patient Application



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;
    }

}

Answer

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());
}


Source: stackoverflow