Skip to content

Commit 7783f73

Browse files
committed
Refactor repositories to use dedicated mapping methods for Owners, Pets, and Vets
1 parent 7fc8aa8 commit 7783f73

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.jooq.DSLContext;
2323
import org.jooq.Field;
24+
import org.jooq.Record7;
2425
import org.springframework.samples.petclinic.system.JooqHelper;
2526
import org.springframework.samples.petclinic.system.Page;
2627
import org.springframework.samples.petclinic.system.Pageable;
@@ -96,12 +97,16 @@ public Page<Owner> findByLastNameStartingWith(String lastName, Pageable pageable
9697
new Field[] { OWNERS.ID }, pageable.pageSize(), pageable.getOffset())
9798
.fetch(it -> {
9899
ref.totalOwners = (Integer) it.get("total_rows");
99-
return new Owner(it.get(OWNERS.ID), it.get(OWNERS.FIRST_NAME), it.get(OWNERS.LAST_NAME),
100-
it.get(OWNERS.ADDRESS), it.get(OWNERS.CITY), it.get(OWNERS.TELEPHONE), it.get(MULTISET_PETS));
100+
return toOwner(it);
101101
});
102102
return new Page<>(owners, pageable, ref.totalOwners);
103103
}
104104

105+
private static Owner toOwner(org.jooq.Record row) {
106+
return new Owner(row.get(OWNERS.ID), row.get(OWNERS.FIRST_NAME), row.get(OWNERS.LAST_NAME),
107+
row.get(OWNERS.ADDRESS), row.get(OWNERS.CITY), row.get(OWNERS.TELEPHONE), row.get(MULTISET_PETS));
108+
}
109+
105110
/**
106111
* Retrieve an {@link Owner} from the data store by id.
107112
* <p>
@@ -121,10 +126,14 @@ public Optional<Owner> findById(@Nonnull Integer id) {
121126
MULTISET_PETS_WITH_VISITS)
122127
.from(OWNERS)
123128
.where(OWNERS.ID.eq(id))
124-
.fetchOptional(it -> new Owner(it.get(OWNERS.ID), it.get(OWNERS.FIRST_NAME), it.get(OWNERS.LAST_NAME),
125-
it.get(OWNERS.ADDRESS), it.get(OWNERS.CITY), it.get(OWNERS.TELEPHONE),
126-
it.get(MULTISET_PETS_WITH_VISITS)));
129+
.fetchOptional(OwnerRepository::toOwner);
130+
131+
}
127132

133+
private static Owner toOwner(Record7<Integer, String, String, String, String, String, List<Pet>> row) {
134+
return new Owner(row.get(OWNERS.ID), row.get(OWNERS.FIRST_NAME), row.get(OWNERS.LAST_NAME),
135+
row.get(OWNERS.ADDRESS), row.get(OWNERS.CITY), row.get(OWNERS.TELEPHONE),
136+
row.get(MULTISET_PETS_WITH_VISITS));
128137
}
129138

130139
public Integer save(Owner owner) {

src/main/java/org/springframework/samples/petclinic/owner/PetRepository.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public void update(Pet pet) {
4848

4949
@Transactional(readOnly = true)
5050
public Optional<Pet> findById(Integer petId) {
51-
return dsl.select()
52-
.from(PETS)
53-
.join(PETS.types_())
54-
.where(PETS.ID.eq(petId))
55-
.fetchOptional(it -> new Pet(it.get(PETS.ID), it.get(PETS.NAME), it.get(PETS.BIRTH_DATE),
56-
new PetType(it.get(PETS.TYPE_ID), it.get(TYPES.NAME))));
51+
return dsl.select().from(PETS).join(PETS.types_()).where(PETS.ID.eq(petId)).fetchOptional(PetRepository::toPet);
52+
}
53+
54+
private static Pet toPet(org.jooq.Record row) {
55+
return new Pet(row.get(PETS.ID), row.get(PETS.NAME), row.get(PETS.BIRTH_DATE),
56+
new PetType(row.get(PETS.TYPE_ID), row.get(TYPES.NAME)));
5757
}
5858

5959
}

src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.jooq.DSLContext;
1919
import org.jooq.Field;
20+
import org.jooq.Record4;
2021
import org.jooq.exception.DataAccessException;
2122
import org.springframework.cache.annotation.Cacheable;
2223
import org.springframework.samples.petclinic.system.JooqHelper;
@@ -76,8 +77,12 @@ public List<Vet> findAll() throws DataAccessException {
7677
.from(VETS)
7778
.leftJoin(VETS.vetSpecialties())
7879
.orderBy(VETS.ID)
79-
.fetch(it -> new Vet(it.get(VETS.ID), it.get(VETS.FIRST_NAME), it.get(VETS.LAST_NAME),
80-
new HashSet<>(it.get(MULTISET_SPECIALITIES))));
80+
.fetch(VetRepository::toVet);
81+
}
82+
83+
private static Vet toVet(Record4<Integer, String, String, List<Specialty>> row) {
84+
return new Vet(row.get(VETS.ID), row.get(VETS.FIRST_NAME), row.get(VETS.LAST_NAME),
85+
new HashSet<>(row.get(MULTISET_SPECIALITIES)));
8186
}
8287

8388
/**
@@ -98,10 +103,14 @@ public Page<Vet> findAll(Pageable pageable) throws DataAccessException {
98103
new Field[] { VETS.ID }, pageable.pageSize(), pageable.getOffset())
99104
.fetch(it -> {
100105
ref.totalVets = (Integer) it.get("total_rows");
101-
return new Vet(it.get(VETS.ID), it.get(VETS.FIRST_NAME), it.get(VETS.LAST_NAME),
102-
new HashSet<>(it.get(MULTISET_SPECIALITIES)));
106+
return toVet(it);
103107
});
104108
return new Page<>(vets, pageable, ref.totalVets);
105109
}
106110

111+
private static Vet toVet(org.jooq.Record row) {
112+
return new Vet(row.get(VETS.ID), row.get(VETS.FIRST_NAME), row.get(VETS.LAST_NAME),
113+
new HashSet<>(row.get(MULTISET_SPECIALITIES)));
114+
}
115+
107116
}

0 commit comments

Comments
 (0)