forked from spring-petclinic/spring-petclinic-microservices
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing record class to only customer-service to start with (spri…
…ng-petclinic#253) Introducing record to the petclinic customer-service microservice
- Loading branch information
1 parent
98d8e38
commit eaf1cc0
Showing
8 changed files
with
79 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...rvice/src/main/java/org/springframework/samples/petclinic/customers/web/OwnerRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.springframework.samples.petclinic.customers.web; | ||
|
||
import jakarta.validation.constraints.Digits; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record OwnerRequest(@NotBlank String firstName, | ||
@NotBlank String lastName, | ||
@NotBlank String address, | ||
@NotBlank String city, | ||
@NotBlank | ||
@Digits(fraction = 0, integer = 12) | ||
String telephone | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,36 +15,30 @@ | |
*/ | ||
package org.springframework.samples.petclinic.customers.web; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.Date; | ||
|
||
import org.springframework.format.annotation.DateTimeFormat; | ||
import org.springframework.samples.petclinic.customers.model.Pet; | ||
import org.springframework.samples.petclinic.customers.model.PetType; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* @author [email protected] on 2016-12-05. | ||
*/ | ||
@Data | ||
class PetDetails { | ||
|
||
private long id; | ||
record PetDetails( | ||
|
||
private String name; | ||
long id, | ||
|
||
private String owner; | ||
String name, | ||
|
||
@DateTimeFormat(pattern = "yyyy-MM-dd") | ||
private Date birthDate; | ||
String owner, | ||
|
||
private PetType type; | ||
@DateTimeFormat(pattern = "yyyy-MM-dd") | ||
Date birthDate, | ||
|
||
PetDetails(Pet pet) { | ||
this.id = pet.getId(); | ||
this.name = pet.getName(); | ||
this.owner = pet.getOwner().getFirstName() + " " + pet.getOwner().getLastName(); | ||
this.birthDate = pet.getBirthDate(); | ||
this.type = pet.getType(); | ||
PetType type | ||
) { | ||
public PetDetails(Pet pet) { | ||
this(pet.getId(), pet.getName(), pet.getOwner().getFirstName() + " " + pet.getOwner().getLastName(), pet.getBirthDate(), pet.getType()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,26 +15,20 @@ | |
*/ | ||
package org.springframework.samples.petclinic.customers.web; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.Date; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import jakarta.validation.constraints.Size; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import java.util.Date; | ||
|
||
/** | ||
* @author [email protected] on 2016-12-05. | ||
*/ | ||
@Data | ||
class PetRequest { | ||
private int id; | ||
|
||
@JsonFormat(pattern = "yyyy-MM-dd") | ||
private Date birthDate; | ||
|
||
@Size(min = 1) | ||
private String name; | ||
record PetRequest(int id, | ||
@JsonFormat(pattern = "yyyy-MM-dd") | ||
Date birthDate, | ||
@Size(min = 1) | ||
String name, | ||
int typeId | ||
) { | ||
|
||
private int typeId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...vice/src/main/java/org/springframework/samples/petclinic/customers/web/mapper/Mapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.springframework.samples.petclinic.customers.web.mapper; | ||
|
||
public interface Mapper<R, E> { | ||
E map(E response, R request); | ||
} |
19 changes: 19 additions & 0 deletions
19
...in/java/org/springframework/samples/petclinic/customers/web/mapper/OwnerEntityMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.springframework.samples.petclinic.customers.web.mapper; | ||
|
||
import org.springframework.samples.petclinic.customers.model.Owner; | ||
import org.springframework.samples.petclinic.customers.web.OwnerRequest; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class OwnerEntityMapper implements Mapper<OwnerRequest, Owner> { | ||
// This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct. | ||
@Override | ||
public Owner map(final Owner owner, final OwnerRequest request) { | ||
owner.setAddress(request.address()); | ||
owner.setCity(request.city()); | ||
owner.setTelephone(request.telephone()); | ||
owner.setFirstName(request.firstName()); | ||
owner.setLastName(request.lastName()); | ||
return owner; | ||
} | ||
} |