|
|
# DTOs
|
|
|
Librador provides an interface for several different types of DTOs: `Create`, `Patch`, `View`, and `Id`.
|
|
|
|
|
|
## Create
|
|
|
`Create` is used to create new entities. A create class should have (at lease the required) fields of the class to be created and the class to be created itself. For example: if we want to create a `Dog` entity with a name and age, we can create the following `Create` class:
|
|
|
```
|
|
|
@Data
|
|
|
@Builder
|
|
|
@NoArgsConstructor
|
|
|
@AllArgsConstructor
|
|
|
@EqualsAndHashCode(callSuper = false)
|
|
|
public class DogCreateDTO extends Create<Dog> {
|
|
|
@NotBlank
|
|
|
private String name;
|
|
|
@Min(0)
|
|
|
private int age;
|
|
|
|
|
|
@Override
|
|
|
public Class<Dog> clazz() {
|
|
|
return Dog.class;
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
Then we can create a `Dog` from a `DogCreateDTO` by calling `apply()`.
|
|
|
|
|
|
## Patch
|
|
|
`Patch` is used to edit already existing entities. A patch needs the fields that need to be changed and an implementation of how to edit the entity. Helper methods for this are provided. An example of a patch class:
|
|
|
```
|
|
|
@Data
|
|
|
@Builder
|
|
|
@NoArgsConstructor
|
|
|
@AllArgsConstructor
|
|
|
@EqualsAndHashCode(callSuper = false)
|
|
|
public class DogPatchDTO extends Patch<Dog> {
|
|
|
private String name;
|
|
|
private Integer age;
|
|
|
|
|
|
@Override
|
|
|
public void applyOneToOne() {
|
|
|
updateNonNull(name, data::setName);
|
|
|
updateNonNull(age, data::setName);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
To change the entity we can again use the `apply()` method.
|
|
|
|
|
|
## View
|
|
|
`View` is used to convert an entity to some object that is used to give to the user. Information can be filtered out or added to View DTOs. An example of a View DTO:
|
|
|
```
|
|
|
@Data
|
|
|
@Builder
|
|
|
@NoArgsConstructor
|
|
|
@AllArgsContstructor
|
|
|
@EqualsAndHashCode(callSuper = false)
|
|
|
public class DogViewDTO extends View<Dog> {
|
|
|
private String name;
|
|
|
private Integer age;
|
|
|
}
|
|
|
```
|
|
|
To convert an entity to a view, we use `View.convert`. In this example `View.convert(dog, DogViewDTO.class)`.
|
|
|
|
|
|
## Id
|
|
|
`Id` is used in Create and Patch to refer to data that is already stored and we therefore do not want to store the entire data in the DTO. To use the same example, if a Dog has an Owner that we already have in the database, the Create would look like:
|
|
|
```
|
|
|
@Data
|
|
|
@Builder
|
|
|
@NoArgsConstructor
|
|
|
@AllArgsConstructor
|
|
|
@EqualsAndHashCode(callSuper = false)
|
|
|
public class DogCreateDTO extends Create<Dog> {
|
|
|
@NotBlank
|
|
|
private String name;
|
|
|
@Min(0)
|
|
|
private int age;
|
|
|
@NotNull
|
|
|
private OwnerIdDTO owner;
|
|
|
}
|
|
|
```
|
|
|
OwnerIdDTO:
|
|
|
```
|
|
|
@Data
|
|
|
@SuperBuilder
|
|
|
@NoArgsConstructor
|
|
|
@EqualsAndHashCode(callSuper = true)
|
|
|
public class OwnerIdDTO extends IdDTO<Owner, Long> {
|
|
|
public Class<? extends CrudRepostory<Owner, Long>> repositoryClass() {
|
|
|
return OwnerRepository.class;
|
|
|
}
|
|
|
|
|
|
public Class<? extends Owner> targetClass() {
|
|
|
return Owner.class;
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
`apply` will then automatically work and fetch Owner from the database provided the IdDTO is configured. This can be done in the `configure` method of `LibradorConfiguration` with:
|
|
|
```
|
|
|
builder.register(SCEditionIdDTO.class, SCEdition.class);
|
|
|
```
|
|
|
|
|
|
|