Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit makes the `milages` association lazy (the default) and also disables the open session in view interceptor. By storing the last recorded milage on the bike entity itself, the need for eagerly accessing the collection goes away. The commit also removes `@Transactional` from the bikes controller while introducing a dedicated service for manipulating bikes. The service takes care of the transaction handling from there on and reads the milages in a controlled way when manipulating them. One fresh wound is the test of the biker controller. It loads the service into the test slice now and passes the mocked repository. This shall be changed at some later point.
- Loading branch information
1 parent
2c7cb85
commit 9c670d7
Showing
9 changed files
with
185 additions
and
111 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
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,108 @@ | ||
/* | ||
* Copyright 2019 michael-simons.eu. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ac.simons.biking2.bikes; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* @author Michael J. Simons | ||
* @since 2019-11-02 | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor(access = AccessLevel.PACKAGE) | ||
class BikeService { | ||
|
||
private final BikeRepository bikeRepository; | ||
|
||
@Transactional | ||
public BikeEntity createBike(final BikeCmd newBike) { | ||
|
||
final BikeEntity bike = new BikeEntity(newBike.getName(), newBike.boughtOnAsLocalDate()); | ||
bike.setColor(newBike.getColor()); | ||
bike.addMilage(newBike.boughtOnAsLocalDate().withDayOfMonth(1), 0); | ||
|
||
return this.bikeRepository.save(bike); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<BikeEntity> getBikes(final boolean all) { | ||
|
||
List<BikeEntity> rv; | ||
if (all) { | ||
rv = bikeRepository.findAll(Sort.by("boughtOn", "decommissionedOn", "name").ascending()); | ||
} else { | ||
rv = bikeRepository.findByDecommissionedOnIsNull(Sort.by("name").ascending()); | ||
} | ||
return rv; | ||
} | ||
|
||
@Transactional | ||
public MilageEntity createMilage(final Integer id, final NewMilageCmd cmd) { | ||
|
||
final BikeEntity bike = bikeRepository.findById(id).orElseThrow(BikeNotFoundException::new); | ||
|
||
MilageEntity rv; | ||
if (bike.getDecommissionedOn() != null) { | ||
throw new BikeAlreadyDecommissionedException(); | ||
} else { | ||
rv = bike.addMilage(cmd.recordedOnAsLocalDate(), cmd.getAmount()); | ||
this.bikeRepository.save(bike); | ||
} | ||
|
||
return rv; | ||
} | ||
|
||
@Transactional | ||
public BikeEntity updateBike(final Integer id, final BikeCmd updatedBike) { | ||
|
||
final BikeEntity bike = bikeRepository.findById(id).orElseThrow(BikeNotFoundException::new); | ||
|
||
if (bike.getDecommissionedOn() != null) { | ||
throw new BikeAlreadyDecommissionedException(); | ||
} else { | ||
bike.setColor(updatedBike.getColor()); | ||
bike.decommission(updatedBike.decommissionedOnAsLocalDate()); | ||
} | ||
return bike; | ||
} | ||
|
||
@Transactional | ||
public BikeEntity updateBikeStory(final Integer id, final StoryCmd newStory) { | ||
|
||
final BikeEntity bike = bikeRepository.findById(id).orElseThrow(BikeNotFoundException::new); | ||
|
||
if (bike.getDecommissionedOn() != null) { | ||
throw new BikeAlreadyDecommissionedException(); | ||
} else { | ||
bike.setStory(Optional.ofNullable(newStory).map(c -> new BikeEntity.Link(c.getUrl(), c.getLabel())).orElse(null)); | ||
} | ||
return bike; | ||
} | ||
|
||
static class BikeNotFoundException extends RuntimeException { | ||
} | ||
|
||
static class BikeAlreadyDecommissionedException extends RuntimeException { | ||
} | ||
} |
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
Oops, something went wrong.