Skip to content

Commit

Permalink
Introducing record class to only customer-service to start with (#253)
Browse files Browse the repository at this point in the history
Introducing record to the petclinic customer-service microservice
  • Loading branch information
shobhakamath authored Jan 5, 2024
1 parent 81a1cc0 commit 39f6670
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public void addPet(Pet pet) {
@Override
public String toString() {
return new ToStringCreator(this)

.append("id", this.getId())
.append("lastName", this.getLastName())
.append("firstName", this.getFirstName())
Expand Down
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
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
package org.springframework.samples.petclinic.customers.web;

import io.micrometer.core.annotation.Timed;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.samples.petclinic.customers.web.mapper.OwnerEntityMapper;
import org.springframework.samples.petclinic.customers.model.Owner;
import org.springframework.samples.petclinic.customers.model.OwnerRepository;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import java.util.List;
import java.util.Optional;

Expand All @@ -43,13 +44,15 @@
class OwnerResource {

private final OwnerRepository ownerRepository;
private final OwnerEntityMapper ownerEntityMapper;

/**
* Create Owner
*/
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Owner createOwner(@Valid @RequestBody Owner owner) {
public Owner createOwner(@Valid @RequestBody OwnerRequest ownerRequest) {
Owner owner = ownerEntityMapper.map(new Owner(), ownerRequest);
return ownerRepository.save(owner);
}

Expand All @@ -74,16 +77,10 @@ public List<Owner> findAll() {
*/
@PutMapping(value = "/{ownerId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateOwner(@PathVariable("ownerId") @Min(1) int ownerId, @Valid @RequestBody Owner ownerRequest) {
final Optional<Owner> owner = ownerRepository.findById(ownerId);
final Owner ownerModel = owner.orElseThrow(() -> new ResourceNotFoundException("Owner "+ownerId+" not found"));
public void updateOwner(@PathVariable("ownerId") @Min(1) int ownerId, @Valid @RequestBody OwnerRequest ownerRequest) {
final Owner ownerModel = ownerRepository.findById(ownerId).orElseThrow(() -> new ResourceNotFoundException("Owner " + ownerId + " not found"));

// This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct.
ownerModel.setFirstName(ownerRequest.getFirstName());
ownerModel.setLastName(ownerRequest.getLastName());
ownerModel.setCity(ownerRequest.getCity());
ownerModel.setAddress(ownerRequest.getAddress());
ownerModel.setTelephone(ownerRequest.getTelephone());
ownerEntityMapper.map(ownerModel, ownerRequest);
log.info("Saving owner {}", ownerModel);
ownerRepository.save(ownerModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
package org.springframework.samples.petclinic.customers.web;

import io.micrometer.core.annotation.Timed;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.samples.petclinic.customers.model.*;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.Min;
import java.util.List;
import java.util.Optional;

/**
* @author Juergen Hoeller
Expand Down Expand Up @@ -54,8 +53,8 @@ public Pet processCreationForm(
@RequestBody PetRequest petRequest,
@PathVariable("ownerId") @Min(1) int ownerId) {

final Optional<Owner> optionalOwner = ownerRepository.findById(ownerId);
Owner owner = optionalOwner.orElseThrow(() -> new ResourceNotFoundException("Owner "+ownerId+" not found"));
Owner owner = ownerRepository.findById(ownerId)
.orElseThrow(() -> new ResourceNotFoundException("Owner " + ownerId + " not found"));

final Pet pet = new Pet();
owner.addPet(pet);
Expand All @@ -65,17 +64,17 @@ public Pet processCreationForm(
@PutMapping("/owners/*/pets/{petId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void processUpdateForm(@RequestBody PetRequest petRequest) {
int petId = petRequest.getId();
int petId = petRequest.id();
Pet pet = findPetById(petId);
save(pet, petRequest);
}

private Pet save(final Pet pet, final PetRequest petRequest) {

pet.setName(petRequest.getName());
pet.setBirthDate(petRequest.getBirthDate());
pet.setName(petRequest.name());
pet.setBirthDate(petRequest.birthDate());

petRepository.findPetTypeById(petRequest.getTypeId())
petRepository.findPetTypeById(petRequest.typeId())
.ifPresent(pet::setType);

log.info("Saving pet {}", pet);
Expand All @@ -84,16 +83,14 @@ private Pet save(final Pet pet, final PetRequest petRequest) {

@GetMapping("owners/*/pets/{petId}")
public PetDetails findPet(@PathVariable("petId") int petId) {
return new PetDetails(findPetById(petId));
Pet pet = findPetById(petId);
return new PetDetails(pet);
}


private Pet findPetById(int petId) {
Optional<Pet> pet = petRepository.findById(petId);
if (!pet.isPresent()) {
throw new ResourceNotFoundException("Pet "+petId+" not found");
}
return pet.get();
return petRepository.findById(petId)
.orElseThrow(() -> new ResourceNotFoundException("Pet " + petId + " not found"));
}

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

0 comments on commit 39f6670

Please sign in to comment.