feat: added PersonDTO for putting objects inside the db (doing that with the standard Person where parent1 is a Person object, not an id doesn't work)
This commit is contained in:
parent
d997ab43cb
commit
2cf9d05cec
16
pom.xml
16
pom.xml
@ -25,11 +25,6 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
@ -41,14 +36,9 @@
|
|||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.springfox</groupId>
|
<groupId>org.springdoc</groupId>
|
||||||
<artifactId>springfox-boot-starter</artifactId>
|
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||||
<version>3.0.0</version>
|
<version>2.0.2</version>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>io.springfox</groupId>
|
|
||||||
<artifactId>springfox-swagger-ui</artifactId>
|
|
||||||
<version>3.0.0</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
package com.example.prapro2spring.config;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import springfox.documentation.builders.PathSelectors;
|
|
||||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
|
||||||
import springfox.documentation.spi.DocumentationType;
|
|
||||||
import springfox.documentation.spring.web.plugins.Docket;
|
|
||||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableSwagger2
|
|
||||||
public class SwaggerConfig {
|
|
||||||
@Bean
|
|
||||||
public Docket api() {
|
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
|
||||||
.select()
|
|
||||||
.apis(RequestHandlerSelectors.basePackage("com.example.prapro2spring.controller"))
|
|
||||||
.paths(PathSelectors.any())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,9 @@
|
|||||||
package com.example.prapro2spring.controller;
|
package com.example.prapro2spring.controller;
|
||||||
|
|
||||||
import com.example.prapro2spring.model.Person;
|
import com.example.prapro2spring.model.Person;
|
||||||
|
import com.example.prapro2spring.dto.PersonDTO;
|
||||||
import com.example.prapro2spring.service.PersonService;
|
import com.example.prapro2spring.service.PersonService;
|
||||||
|
import com.example.prapro2spring.repository.PeopleRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@ -11,38 +13,44 @@ import java.util.List;
|
|||||||
@RequestMapping("/api/persons")
|
@RequestMapping("/api/persons")
|
||||||
public class PersonController {
|
public class PersonController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PeopleRepository peopleRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
private PersonService personService;
|
private PersonService personService;
|
||||||
|
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Person> getAllPersons() {
|
public List<Person> getAllPersons() {
|
||||||
return personService.findAll();
|
return peopleRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public Person getPersonById(@PathVariable Long id) {
|
public Person getPersonById(@PathVariable Long id) {
|
||||||
return personService.findById(id).orElse(null);
|
return peopleRepository.findById(id).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Person createPerson(@RequestBody Person person) {
|
public PersonDTO createPerson(@RequestBody PersonDTO person) {
|
||||||
|
person.setId(null);
|
||||||
|
// print person.parent1
|
||||||
|
System.out.println(person.getParent1());
|
||||||
return personService.save(person);
|
return personService.save(person);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public Person updatePerson(@PathVariable Long id, @RequestBody Person personDetails) {
|
public Person updatePerson(@PathVariable Long id, @RequestBody Person personDetails) {
|
||||||
Person person = personService.findById(id).orElse(null);
|
Person person = peopleRepository.findById(id).orElse(null);
|
||||||
if (person != null) {
|
if (person != null) {
|
||||||
person.setParent1(personDetails.getParent1());
|
person.setParent1(personDetails.getParent1());
|
||||||
person.setParent2(personDetails.getParent2());
|
person.setParent2(personDetails.getParent2());
|
||||||
person.setBirthTimestamp(personDetails.getBirthTimestamp());
|
person.setBirthTimestamp(personDetails.getBirthTimestamp());
|
||||||
return personService.save(person);
|
return peopleRepository.save(person);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public void deletePerson(@PathVariable Long id) {
|
public void deletePerson(@PathVariable Long id) {
|
||||||
personService.deleteById(id);
|
peopleRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
57
src/main/java/com/example/prapro2spring/dto/PersonDTO.java
Normal file
57
src/main/java/com/example/prapro2spring/dto/PersonDTO.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package com.example.prapro2spring.dto;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "people")
|
||||||
|
public class PersonDTO {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(name = "parent1Id")
|
||||||
|
private Long parent1;
|
||||||
|
|
||||||
|
@Column(name = "parent2Id")
|
||||||
|
private Long parent2;
|
||||||
|
|
||||||
|
@Column(name = "birth_timestamp")
|
||||||
|
private java.time.LocalDateTime birthTimestamp;
|
||||||
|
|
||||||
|
// getters and setters
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getParent1() {
|
||||||
|
return parent1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParent1(Long parent1) {
|
||||||
|
this.parent1 = parent1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getParent2() {
|
||||||
|
return parent2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParent2(Long parent2) {
|
||||||
|
this.parent2 = parent2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getBirthTimestamp() {
|
||||||
|
return birthTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBirthTimestamp(LocalDateTime birthTimestamp) {
|
||||||
|
this.birthTimestamp = birthTimestamp;
|
||||||
|
}
|
||||||
|
}
|
@ -20,7 +20,7 @@ public class Person {
|
|||||||
@JoinColumn(name = "parent2Id")
|
@JoinColumn(name = "parent2Id")
|
||||||
private Person parent2;
|
private Person parent2;
|
||||||
|
|
||||||
@Column(name = "birthTimestamp")
|
@Column(name = "birth_timestamp")
|
||||||
private java.time.LocalDateTime birthTimestamp;
|
private java.time.LocalDateTime birthTimestamp;
|
||||||
|
|
||||||
// getters and setters
|
// getters and setters
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package com.example.prapro2spring.service;
|
package com.example.prapro2spring.service;
|
||||||
|
|
||||||
|
import com.example.prapro2spring.dto.PersonDTO;
|
||||||
import com.example.prapro2spring.model.Person;
|
import com.example.prapro2spring.model.Person;
|
||||||
|
import com.example.prapro2spring.repository.PeopleRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Repository
|
@Service
|
||||||
public interface PersonService extends JpaRepository<Person, Long> {
|
public interface PersonService extends JpaRepository<PersonDTO, Long> {
|
||||||
}
|
}
|
@ -2,6 +2,7 @@ spring.datasource.url=jdbc:postgresql://db.twawwfuogpwjclumcsdl.supabase.co:5432
|
|||||||
spring.datasource.username=postgres
|
spring.datasource.username=postgres
|
||||||
spring.datasource.password=uyi3SpF2Hodw78UJ
|
spring.datasource.password=uyi3SpF2Hodw78UJ
|
||||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||||
|
spring.jpa.properties.hibernate.default_schema=people_scoreboard
|
||||||
|
|
||||||
spring.jpa.hibernate.ddl-auto=update
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
spring.jpa.show-sql=true
|
spring.jpa.show-sql=true
|
Loading…
Reference in New Issue
Block a user