0% found this document useful (0 votes)
16 views20 pages

Crud

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views20 pages

Crud

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

CRUD (Create Read Update Delete) Operations

1. CREATE:
The requirement that needed to be implemented as Create operation from CRUD using both
MVC (Model View Controller) and REST (Representational State Transfer) is:
Identifier HMS 001_01
Title Register New Patients.

Implementation By MVC:
 Model:

public class Patient {


private String name;
private int age;
private String gender;

public Patient(String name, int age, String gender) {


this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

 Controller:
import java.util.ArrayList;

import java.util.List;

public class PatientController {

private List<Patient> patients;

public PatientController() {

this.patients = new ArrayList<>();

public Patient registerNewPatient(String name, int age, String gender) {

Patient newPatient = new Patient(name, age, gender);

patients.add(newPatient);

return newPatient;

}
 View

public class Main {


public static void main(String[] args) {
PatientController controller = new PatientController();

Patient patient1 = controller.registerNewPatient("John Doe", 30, "Male");


Patient patient2 = controller.registerNewPatient("Jane Smith", 25, "Female");
Patient patient3 = controller.registerNewPatient("Michael Johnson", 40, "Male");

System.out.println("List of Registered Patients:");


}
}
Implementation By REST:
 Model:
public class Patient {

private String name;

private int age;

private String gender;

public Patient(String name, int age, String gender) {

this.name = name;

this.age = age;

this.gender = gender;

public String getName() {

return name;

public int getAge() {

return age;

public void setName(String name) {

this.name = name;

public void setAge(int age) {

this.age = age;

}
 REST Controller:

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class PatientController {

@Autowired

private PatientRepository patientRepository;

@PostMapping("/patients")

public Patient registerNewPatient(@RequestBody Patient patient) {

Patient savedPatient = patientRepository.save(patient);

return savedPatient;

}
2. Read
The requirement that needed to be implemented as READ operation from CRUD using both
MVC (Model View Controller) and REST (Representational State Transfer) is:
Identifier HMS 001_25
Title Generate Patient Visit Summaries.

Implementation By MVC:
 Model:

public class PatientVisit {


 private String patientName; Controller:
private String visitDate;
import java.util.ArrayList;
private String diagnosis;
import java.util.List;

public PatientVisit(String patientName, String visitDate,


String diagnosis)
public class PatientVisitController { {

private PatientVisitServicethis.patientName
visitService; = patientName;
this.visitDate = visitDate;{
public List<PatientVisit> generateVisitSummaries()
this.diagnosis
List<PatientVisit> visitData = diagnosis;
= visitService.getPatientVisitData();
}
List<PatientVisit> summaries = new ArrayList<>();
public String getVisitDate() {
for (PatientVisit visit : visitData) {
return visitDate;
String patientName = visit.getPatientName();
}
String visitDate = visit.getVisitDate();
public void setVisitDate(String visitDate) {
String diagnosis = visit.getDiagnosis();
this.visitDate = visitDate;
String summary = "Patient: " + patientName + ", Visit Date: " + visitDate + ", Diagnosis: " + diagnosis;
}
PatientVisit summaryVisit = new PatientVisit(patientName, visitDate, diagnosis);
}
summaries.add(summaryVisit);

return summaries;

}
 View:

import java.util.List;

public class VisitSummaryView {

public void displayVisitSummaries(List<PatientVisit> summaries) {

for (PatientVisit visit : summaries) {

System.out.println("Patient: " + visit.getPatientName());

System.out.println("Visit Date: " + visit.getVisitDate());

System.out.println("Diagnosis: " + visit.getDiagnosis());

System.out.println("---------------------------------");

}
Implementation By REST:
 REST Controller

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class PatientVisitRestController {

@Autowired

private PatientVisitService visitService;

@GetMapping("/visit-summaries")

public List<PatientVisit> getVisitSummaries() {

return visitService.generateVisitSummaries();

}
 Service:

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.List;

@Service

public class PatientVisitService {

@Autowired

private PatientVisitRepository visitRepository;

public List<PatientVisit> generateVisitSummaries() {

List<PatientVisit> visitData = visitRepository.findAll();

List<PatientVisit> summaries = new ArrayList<>();

for (PatientVisit visit : visitData) {

String patientName = visit.getPatientName();

String visitDate = visit.getVisitDate();

String diagnosis = visit.getDiagnosis();

String summary = "Patient: " + patientName + ", Visit Date: " + visitDate + ", Diagnosis: " + diagnosis;

PatientVisit summaryVisit = new PatientVisit(patientName, visitDate, diagnosis);

summaries.add(summaryVisit);

return summaries;

}
3. Update
The requirement that needed to be implemented as Update operation from CRUD using both
MVC (Model View Controller) and REST (Representational State Transfer) is:
Identifier HMS 002_02
Title Edit Registered Doctor’s Details.

Implementation By MVC:
 Model

public class Doctor {


private String id;
private String name;
private String specialization;

public Doctor(String id, String name, String specialization) {


this.id = id;
this.name = name;
this.specialization = specialization;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
 Controller

import java.util.HashMap;
import java.util.Map;

public class DoctorController {


private Map<String, Doctor> doctors = new HashMap<>();

public void addDoctor(Doctor doctor) {


doctors.put(doctor.getId(), doctor);
}

public void updateDoctor(String id, Doctor updatedDoctor) {


if (doctors.containsKey(id)) {
doctors.put(id, updatedDoctor);
} else {
System.out.println("Doctor with ID " + id + " not found.");
}
}

public Doctor getDoctor(String id) {


return doctors.get(id);
}
}
 View:

public class Main {


public static void main(String[] args) {
DoctorController controller = new DoctorController();

Doctor doctor = new Doctor("1", "Dr. John", "Cardiologist");


controller.addDoctor(doctor);

Doctor updatedDoctor = new Doctor("1", "Dr. John Doe", "Neurologist");


controller.updateDoctor("1", updatedDoctor);

// Retrieving updated doctor's details


Doctor retrievedDoctor = controller.getDoctor("1");
System.out.println("Updated Doctor's Details:");
System.out.println("ID: " + retrievedDoctor.getId());
System.out.println("Name: " + retrievedDoctor.getName());
System.out.println("Specialization: " + retrievedDoctor.getSpecialization());
}
}
Implementation By REST:
 REST Controller:

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

@RestController

@RequestMapping("/doctors")

public class DoctorRestController {

@Autowired

private DoctorService doctorService;

@PutMapping("/{id}")

public Doctor updateDoctor(@PathVariable String id, @RequestBody Doctor updatedDoctor)


{

return doctorService.updateDoctor(id, updatedDoctor);

}
 Service

import org.springframework.stereotype.Service;

import java.util.HashMap;

import java.util.Map;

@Service

public class DoctorService {

private Map<String, Doctor> doctors = new HashMap<>();

public DoctorService() {

// Sample data for demonstration

Doctor doctor1 = new Doctor("1", "Dr. John", "Cardiologist");

doctors.put(doctor1.getId(), doctor1);

public Doctor updateDoctor(String id, Doctor updatedDoctor) {

if (doctors.containsKey(id)) {

Doctor doctor = doctors.get(id);

doctor.setName(updatedDoctor.getName());

doctor.setSpecialization(updatedDoctor.getSpecialization());

return doctor;

} else {

throw new IllegalArgumentException("Doctor with ID " + id + " not found.");

}
4. Delete
The requirement that needed to be implemented as Delete operation from CRUD using both
MVC (Model View Controller) and REST (Representational State Transfer) is:
Identifier HMS 002_03
Title Delete Registered Doctor from the System

Implementation By MVC:
 Model
public class Doctor {
private String id;
private String name;
private String specialization;

public Doctor(String id, String name, String specialization) {


this.id = id;
this.name = name;
this.specialization = specialization;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
 Controller:
import java.util.HashMap;
import java.util.Map;

public class DoctorController {


private Map<String, Doctor> doctors = new HashMap<>();

public void addDoctor(Doctor doctor) {


doctors.put(doctor.getId(), doctor);
}

public void deleteDoctor(String id) {


if (doctors.containsKey(id)) {
doctors.remove(id);
System.out.println("Doctor with ID " + id + " has been deleted.");
} else {
System.out.println("Doctor with ID " + id + " not found.");
}
}

public Doctor getDoctor(String id) {


return doctors.get(id);
}
}
 View

public class Main {


public static void main(String[] args) {
DoctorController controller = new DoctorController();

Doctor doctor1 = new Doctor("1", "Dr. John", "Cardiologist");


Doctor doctor2 = new Doctor("2", "Dr. Alice", "Dermatologist");
Doctor doctor3 = new Doctor("3", "Dr. Smith", "Neurologist");

controller.addDoctor(doctor1);
controller.addDoctor(doctor2);
controller.addDoctor(doctor3);

System.out.println("List of Doctors before deletion:");


displayDoctors(controller);

controller.deleteDoctor("2");

System.out.println("\nList of Doctors after deletion:");


displayDoctors(controller);
}
}
Implementation By REST
 REST Controller

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/doctors")

public class DoctorRestController {

@Autowired

private DoctorService doctorService;

@DeleteMapping("/{id}")

public String deleteDoctor(@PathVariable String id) {

doctorService.deleteDoctor(id);

return "Doctor with ID " + id + " has been deleted.";

}
 Service

import org.springframework.stereotype.Service;

import java.util.HashMap;

import java.util.Map;

@Service

public class DoctorService {

private Map<String, Doctor> doctors = new HashMap<>();

public DoctorService() {

// Sample data for demonstration

Doctor doctor1 = new Doctor("1", "Dr. John", "Cardiologist");

doctors.put(doctor1.getId(), doctor1);

public void deleteDoctor(String id) {

if (doctors.containsKey(id)) {

doctors.remove(id);

} else {

throw new IllegalArgumentException("Doctor with ID " + id + " not found.");

You might also like