repository valid

This commit is contained in:
uPagge 2021-03-22 18:11:57 +03:00
parent 5904a2ff60
commit f42e720332
No known key found for this signature in database
GPG Key ID: 964B40928E4C9088
6 changed files with 66 additions and 3 deletions

1
.gitignore vendored
View File

@ -81,3 +81,4 @@ crashlytics-build.properties
fabric.properties
.idea/httpRequests
.idea/caches/build_file_checksums.ser
/data/

View File

@ -0,0 +1,28 @@
package org.sadtech.example.springvalidation.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.sadtech.example.springvalidation.dto.PersonDto;
import org.sadtech.example.springvalidation.repository.PersonRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/valid-repa/person")
@Tag(name = "Пользователи системы 3", description = "Валидация на уровне репозитория")
public class PersonControllerValidRepository {
private final PersonRepository personRepository;
@PostMapping
@Operation(summary = "Сохранение пользователя")
public ResponseEntity<PersonDto> save(@RequestBody PersonDto personDto) {
return ResponseEntity.ok(personRepository.save(personDto));
}
}

View File

@ -3,15 +3,26 @@ package org.sadtech.example.springvalidation.dto;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
@Entity
@Getter
@Setter
@Table(name = "person")
public class PersonDto {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@ -19,8 +30,10 @@ public class PersonDto {
@Min(1)
@Max(10)
@Column(name = "number")
private int numberBetweenOneAndTen;
@Column(name = "ip_address")
@Pattern(regexp = "^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$")
private String ipAddress;

View File

@ -0,0 +1,8 @@
package org.sadtech.example.springvalidation.repository;
import org.sadtech.example.springvalidation.dto.PersonDto;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PersonRepository extends JpaRepository<PersonDto, Long> {
}

View File

@ -1,3 +1,8 @@
spring:
liquibase:
change-log: classpath:db/changelog/changelog.xml
change-log: classpath:db/changelog/changelog.xml
datasource:
url: jdbc:h2:file:./data/validation;DB_CLOSE_DELAY=-1;AUTO_SERVER=TRUE
password: password
username: sa
driver-class-name: org.h2.Driver

View File

@ -3,6 +3,14 @@
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="create-person" author="uPagge">
<createTable tableName="person">
<column name="id" type="int" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="number" type="int"/>
<column name="name" type="varchar(64)"/>
<column name="ip_address" type="varchar(64)"/>
</createTable>
</changeSet>
</databaseChangeLog>