Compare commits

...

3 Commits

@ -4,6 +4,7 @@ import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;
import jakarta.persistence.EntityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ -11,6 +12,8 @@ import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import space.kklochko.spring_rest_example.db.factories.EntityManagerConnection;
import space.kklochko.spring_rest_example.db.repositories.IndicatorRepository;
import space.kklochko.spring_rest_example.models.Indicator;
@EnableWebMvc
@ -22,11 +25,13 @@ public class WebConfig implements WebMvcConfigurer {
converters.add(new MappingJackson2HttpMessageConverter());
}
@Bean("indicator")
public Indicator getIndicator() {
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);
return indicator;
@Bean
public EntityManager manager() {
return (new EntityManagerConnection("spring_rest_example")).connect();
}
@Bean
public IndicatorRepository indicatorRepository(EntityManager manager) {
return new IndicatorRepository(manager);
}
}

@ -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);
List<Indicator> indicators = new ArrayList<>();
indicators.add(indicator);
return indicators;
public List<Indicator> getAll() {
return indicatorRepository.readAll();
}
@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");
}
}

@ -3,19 +3,19 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="jpa_hospital_example">
<persistence-unit name="spring_rest_example">
<description>
Persistence unit for the Jakarta Persistence of the JPA Hospital Example.
Persistence unit for the Jakarta Persistence of the Spring REST Example.
</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>space.kklochko.jpa_hospital_example.models.Department</class>
<class>space.kklochko.jpa_hospital_example.models.Patient</class>
<class>space.kklochko.jpa_hospital_example.models.Indicator</class>
<class>space.kklochko.spring_rest_example.models.Department</class>
<class>space.kklochko.spring_rest_example.models.Patient</class>
<class>space.kklochko.spring_rest_example.models.Indicator</class>
<properties>
<property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb" />
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://postgres:5432/testdb" />
<property name="jakarta.persistence.jdbc.user" value="user" />
<property name="jakarta.persistence.jdbc.password" value="testpassword" />
@ -29,13 +29,13 @@
<persistence-unit name="testing">
<description>
Test persistence unit for the Jakarta Persistence of the JPA Hospital Example.
Test persistence unit for the Jakarta Persistence of the Spring REST Example.
</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>space.kklochko.jpa_hospital_example.models.Department</class>
<class>space.kklochko.jpa_hospital_example.models.Patient</class>
<class>space.kklochko.jpa_hospital_example.models.Indicator</class>
<class>space.kklochko.spring_rest_example.models.Department</class>
<class>space.kklochko.spring_rest_example.models.Patient</class>
<class>space.kklochko.spring_rest_example.models.Indicator</class>
<properties>
<property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver" />

Loading…
Cancel
Save