-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract repository for meal plan
- Loading branch information
Showing
9 changed files
with
85 additions
and
70 deletions.
There are no files selected for viewing
11 changes: 3 additions & 8 deletions
11
src/main/java/nu/peg/svmeal/application/MealController.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
10 changes: 3 additions & 7 deletions
10
src/main/java/nu/peg/svmeal/application/RestaurantController.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
8 changes: 8 additions & 0 deletions
8
src/main/java/nu/peg/svmeal/domain/repository/MealPlanRepository.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,8 @@ | ||
package nu.peg.svmeal.domain.repository; | ||
|
||
import nu.peg.svmeal.domain.model.MealPlansDto; | ||
import nu.peg.svmeal.domain.model.RestaurantDto; | ||
|
||
public interface MealPlanRepository { | ||
MealPlansDto getMealPlans(RestaurantDto restaurant); | ||
} |
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
53 changes: 53 additions & 0 deletions
53
src/main/java/nu/peg/svmeal/infrastructure/repository/SvMealPlanRepository.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,53 @@ | ||
package nu.peg.svmeal.infrastructure.repository; | ||
|
||
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import nu.peg.svmeal.domain.exceptions.ExternalException; | ||
import nu.peg.svmeal.domain.exceptions.MealPlanParsingException; | ||
import nu.peg.svmeal.domain.model.MealPlansDto; | ||
import nu.peg.svmeal.domain.model.RestaurantDto; | ||
import nu.peg.svmeal.domain.repository.MealPlanRepository; | ||
import nu.peg.svmeal.infrastructure.converter.DocumentMealPlanParser; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import static nu.peg.svmeal.infrastructure.config.CacheNames.MEAL_PLAN; | ||
import static nu.peg.svmeal.infrastructure.config.CircuitBreakers.SV_MENU; | ||
|
||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class SvMealPlanRepository implements MealPlanRepository { | ||
private static final String NO_MEALPLAN_AVAILABLE_ERROR = "No meal plan available"; | ||
|
||
private final RestTemplate restTemplate; | ||
private final DocumentMealPlanParser docParser; | ||
|
||
@Override | ||
@Cacheable(MEAL_PLAN) | ||
@CircuitBreaker(name = SV_MENU) | ||
public MealPlansDto getMealPlans(RestaurantDto restaurant) { | ||
log.debug("Scraping meal plan for restaurant {}", restaurant); | ||
|
||
ResponseEntity<String> response = restTemplate.getForEntity(restaurant.getLink(), String.class); | ||
|
||
if (response.getStatusCode() != HttpStatus.OK) { | ||
throw new ExternalException("Error while fetching meal plan"); | ||
} | ||
|
||
Document document = Jsoup.parse(response.getBody()); | ||
MealPlansDto dto = docParser.convert(document); | ||
|
||
if (dto == null) { | ||
throw new MealPlanParsingException(NO_MEALPLAN_AVAILABLE_ERROR); | ||
} | ||
|
||
return dto; | ||
} | ||
} |
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
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