Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
41-boot-tdd-solution - added CarServiceCache.java
Browse files Browse the repository at this point in the history
  • Loading branch information
sashinpivotal committed Feb 4, 2021
1 parent b9cde1f commit 195a23e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.pivotal.workshop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -14,7 +13,6 @@ public CarService(CarRepository carRepository) {
this.carRepository = carRepository;
}

@Cacheable("car-cache")
public Car getCarDetails(String name) {
Car car = carRepository.findByName(name);
if (car == null){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.pivotal.workshop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

@Configuration
@EnableCaching
@Service
public class CarServiceCache {

private CarRepository carRepository;

@Autowired
public CarServiceCache(CarRepository carRepository) {
this.carRepository = carRepository;
}

@Cacheable("car-cache")
public Car getCarDetails(String name) {
Car car = carRepository.findByName(name);
if (car == null){
throw new CarNotFoundException();
}

// Dummy business logic we want to unit-test
car.setType(car.getType() + "1");

return car;
}

public Car addCarDetails(Car car) {
return carRepository.save(car);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class SpringBootTddApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class CarServiceCachingSliceTests {

@Autowired
private CarService carService;
private CarServiceCache carService;

@Autowired
private CacheManager cacheManager;
Expand Down

0 comments on commit 195a23e

Please sign in to comment.