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:
Marcin Alchimowicz 2024-01-17 19:11:19 +01:00
parent d997ab43cb
commit 2cf9d05cec
7 changed files with 82 additions and 45 deletions

16
pom.xml
View File

@ -25,11 +25,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@ -41,14 +36,9 @@
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>

View File

@ -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();
}
}

View File

@ -1,7 +1,9 @@
package com.example.prapro2spring.controller;
import com.example.prapro2spring.model.Person;
import com.example.prapro2spring.dto.PersonDTO;
import com.example.prapro2spring.service.PersonService;
import com.example.prapro2spring.repository.PeopleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -11,38 +13,44 @@ import java.util.List;
@RequestMapping("/api/persons")
public class PersonController {
@Autowired
private PeopleRepository peopleRepository;
@Autowired
private PersonService personService;
@GetMapping
public List<Person> getAllPersons() {
return personService.findAll();
return peopleRepository.findAll();
}
@GetMapping("/{id}")
public Person getPersonById(@PathVariable Long id) {
return personService.findById(id).orElse(null);
return peopleRepository.findById(id).orElse(null);
}
@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);
}
@PutMapping("/{id}")
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) {
person.setParent1(personDetails.getParent1());
person.setParent2(personDetails.getParent2());
person.setBirthTimestamp(personDetails.getBirthTimestamp());
return personService.save(person);
return peopleRepository.save(person);
}
return null;
}
@DeleteMapping("/{id}")
public void deletePerson(@PathVariable Long id) {
personService.deleteById(id);
peopleRepository.deleteById(id);
}
}

View 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;
}
}

View File

@ -20,7 +20,7 @@ public class Person {
@JoinColumn(name = "parent2Id")
private Person parent2;
@Column(name = "birthTimestamp")
@Column(name = "birth_timestamp")
private java.time.LocalDateTime birthTimestamp;
// getters and setters

View File

@ -1,9 +1,12 @@
package com.example.prapro2spring.service;
import com.example.prapro2spring.dto.PersonDTO;
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.stereotype.Repository;
import org.springframework.stereotype.Service;
@Repository
public interface PersonService extends JpaRepository<Person, Long> {
@Service
public interface PersonService extends JpaRepository<PersonDTO, Long> {
}

View File

@ -2,6 +2,7 @@ spring.datasource.url=jdbc:postgresql://db.twawwfuogpwjclumcsdl.supabase.co:5432
spring.datasource.username=postgres
spring.datasource.password=uyi3SpF2Hodw78UJ
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.default_schema=people_scoreboard
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true