Update the indicator controller to have CRUD operations.

main
KKlochko 1 year ago
parent b202457f10
commit 0ad4e95079

@ -1,7 +1,11 @@
package space.kklochko.spring_rest_example.controllers; 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.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import space.kklochko.spring_rest_example.db.repositories.IndicatorRepository;
import space.kklochko.spring_rest_example.models.Indicator; import space.kklochko.spring_rest_example.models.Indicator;
import java.sql.Timestamp; import java.sql.Timestamp;
@ -13,22 +17,63 @@ import java.util.UUID;
@RequestMapping("/api/v1/indicators") @RequestMapping("/api/v1/indicators")
public class IndicatorController { public class IndicatorController {
@Autowired @Autowired
Indicator indicator; EntityManager manager;
@GetMapping("/{indicatorId}")
@Autowired
IndicatorRepository indicatorRepository;
@PostMapping
@ResponseBody @ResponseBody
public Indicator getIndicator(@PathVariable UUID indicatorId) { public ResponseEntity<Object> create(@RequestBody Indicator indicator) {
return 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 @GetMapping
@ResponseBody @ResponseBody
public List<Indicator> getIndicators() { public List<Indicator> getAll() {
UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366ab"); return indicatorRepository.readAll();
Timestamp timestamp = Timestamp.valueOf("2023-10-18 14:30:00"); }
Indicator indicator = new Indicator(id, "Blood2", "Fine2", timestamp);
@PutMapping
List<Indicator> indicators = new ArrayList<>(); @ResponseBody
indicators.add(indicator); public ResponseEntity<Object> update(@RequestBody Indicator indicator) {
return indicators; 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");
} }
} }

Loading…
Cancel
Save