Skip to content

Commit

Permalink
getAll operation for medication
Browse files Browse the repository at this point in the history
  • Loading branch information
ituitis20-karadagd20 committed Jan 1, 2024
1 parent 2784d12 commit 5818722
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/medications")
public class MedicationController {
Expand All @@ -26,6 +28,18 @@ public ResponseEntity<MedicationDTO> getMedicationById(@PathVariable Integer id)
}
}

@GetMapping("/all")
public ResponseEntity<List<MedicationDTO>> getAllMedications() {
List<MedicationDTO> response = medicationService.getAllMedications();

if (!response.isEmpty()) {
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}


@PostMapping
public ResponseEntity<MedicationDTO> postMedication(@RequestBody CreateOrUpdateMedicationDTO medicationDto){
MedicationDTO postedMedication = medicationService.postMedication(medicationDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ResponseEntity<ScheduleDTO> getScheduleById(@PathVariable Integer id) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@DeleteMapping("/{id}")
public ResponseEntity<ScheduleDTO> deleteSchedule(@PathVariable Integer id) {
ScheduleDTO deletedSchedule = scheduleService.deleteSchedule(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
Expand All @@ -28,6 +30,17 @@ public MedicationDTO getMedicationById(Integer id) {
return medication != null ? medicationMapper.convertToDto(medication) : null;
}

public List<MedicationDTO> getAllMedications() {
List<Medication> medicationList = medicationRepository.findAll();
List<MedicationDTO> medicationDtoList = new ArrayList<>();

for (Medication medication : medicationList) {
medicationDtoList.add(medicationMapper.convertToDto(medication));
}

return medicationDtoList;
}

public MedicationDTO postMedication(CreateOrUpdateMedicationDTO medicationDto){
// Create a new medication from the dto.
Medication newMedication = medicationMapper.convertToEntity(medicationDto);
Expand Down

0 comments on commit 5818722

Please sign in to comment.