Update the indicator controller to have CRUD operations.
This commit is contained in:
+56
-11
@@ -1,7 +1,11 @@
|
||||
package space.kklochko.spring_rest_example.controllers;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import space.kklochko.spring_rest_example.db.repositories.IndicatorRepository;
|
||||
import space.kklochko.spring_rest_example.models.Indicator;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
@@ -13,22 +17,63 @@ import java.util.UUID;
|
||||
@RequestMapping("/api/v1/indicators")
|
||||
public class IndicatorController {
|
||||
@Autowired
|
||||
Indicator indicator;
|
||||
@GetMapping("/{indicatorId}")
|
||||
EntityManager manager;
|
||||
|
||||
@Autowired
|
||||
IndicatorRepository indicatorRepository;
|
||||
|
||||
@PostMapping
|
||||
@ResponseBody
|
||||
public Indicator getIndicator(@PathVariable UUID indicatorId) {
|
||||
return indicator;
|
||||
public ResponseEntity<Object> create(@RequestBody Indicator indicator) {
|
||||
Indicator isExist = indicatorRepository.read(indicator.getId());
|
||||
|
||||
if(isExist != null)
|
||||
return ResponseEntity.status(500).body("Failed to create the indicator, because the id is already used!!!");
|
||||
|
||||
boolean successful = indicatorRepository.create(indicator);
|
||||
|
||||
if(!successful)
|
||||
return ResponseEntity.status(500).body("Failed to create the indicator");
|
||||
|
||||
return ResponseEntity.status(201).body("The indicator created successfully");
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ResponseBody
|
||||
public Indicator get(@PathVariable UUID id) {
|
||||
return indicatorRepository.read(id);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ResponseBody
|
||||
public List<Indicator> getIndicators() {
|
||||
UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366ab");
|
||||
Timestamp timestamp = Timestamp.valueOf("2023-10-18 14:30:00");
|
||||
Indicator indicator = new Indicator(id, "Blood2", "Fine2", timestamp);
|
||||
public List<Indicator> getAll() {
|
||||
return indicatorRepository.readAll();
|
||||
}
|
||||
|
||||
List<Indicator> indicators = new ArrayList<>();
|
||||
indicators.add(indicator);
|
||||
return indicators;
|
||||
@PutMapping
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> update(@RequestBody Indicator indicator) {
|
||||
Indicator isExist = indicatorRepository.read(indicator.getId());
|
||||
|
||||
if(isExist == null)
|
||||
return ResponseEntity.status(500).body("Failed to update the indicator, because the indicator does not exist!!!");
|
||||
|
||||
boolean successful = indicatorRepository.update(indicator);
|
||||
|
||||
if(!successful)
|
||||
return ResponseEntity.status(500).body("Failed to update the indicator.");
|
||||
|
||||
return ResponseEntity.status(200).body("The indicator updated successfully");
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> delete(@PathVariable UUID id) {
|
||||
boolean successful = indicatorRepository.delete(id);
|
||||
|
||||
if(!successful)
|
||||
return ResponseEntity.status(500).body("Failed to delete the indicator, because the indicator does not exist!!!");
|
||||
|
||||
return ResponseEntity.status(201).body("The indicator deleted successfully");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user