Skip to content

Commit

Permalink
refactor: use records, sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jmesserli committed Oct 31, 2023
1 parent bd174de commit 81d1bb7
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 93 deletions.
17 changes: 0 additions & 17 deletions Dockerfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public AvailabilityDto getMealPlanAvailability(

private RestaurantDto findRestaurant(String shortcut) {
return restaurantService.getRestaurantDtos().stream()
.filter(rest -> rest.getShortcut().equalsIgnoreCase(shortcut))
.filter(rest -> rest.shortcut().equalsIgnoreCase(shortcut))
.findFirst()
.orElseThrow(
() ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
package nu.peg.svmeal.domain.model;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class AvailabilityDto {
private boolean available;
}
public record AvailabilityDto(boolean available) {}
30 changes: 21 additions & 9 deletions src/main/java/nu/peg/svmeal/domain/model/DietaryRestriction.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
package nu.peg.svmeal.domain.model;

import static java.util.function.Function.identity;

import java.util.Optional;
import java.util.Set;

public enum DietaryRestriction {
VEGETARIAN,
VEGAN;
VEGAN,
//
;

public static DietaryRestriction determineDietaryRestrictionByClassNames(
Set<String> classNames) {
public static DietaryRestriction determineDietaryRestrictionByClassNames(Set<String> classNames) {
if (classNames == null) {
return null;
}

if (classNames.contains("label-vegetarian")) {
return VEGETARIAN;
} else if (classNames.contains("label-vegan")) {
return VEGAN;
}
return null;
return classNames.stream()
.map(DietaryRestriction::fromLabelName)
.filter(Optional::isPresent)
.findFirst()
.flatMap(identity())
.orElse(null);
}

private static Optional<DietaryRestriction> fromLabelName(String labelName) {
return switch (labelName) {
case "label-vegetarian" -> Optional.of(VEGETARIAN);
case "label-vegan" -> Optional.of(VEGAN);
default -> Optional.empty();
};
}
}
13 changes: 3 additions & 10 deletions src/main/java/nu/peg/svmeal/domain/model/MealPlanDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class MealPlanDto implements Serializable {
@JsonSerialize(using = ToStringSerializer.class)
private LocalDate date;

private List<MenuOfferDto> offers;
}
public record MealPlanDto(
@JsonSerialize(using = ToStringSerializer.class) LocalDate date, List<MenuOfferDto> offers)
implements Serializable {}
9 changes: 2 additions & 7 deletions src/main/java/nu/peg/svmeal/domain/model/MealPlansDto.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package nu.peg.svmeal.domain.model;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class MealPlansDto {
private Map<LocalDate, MealPlanDto> plans;
}
public record MealPlansDto(Map<LocalDate, MealPlanDto> plans) implements Serializable {}
17 changes: 7 additions & 10 deletions src/main/java/nu/peg/svmeal/domain/model/MenuOfferDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import java.io.Serializable;
import java.util.List;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class MenuOfferDto implements Serializable {
private String title;
private List<String> trimmings;

private PriceDto price;
private String provenance;
private DietaryRestriction dietaryRestriction;
}
public record MenuOfferDto(
String title,
List<String> trimmings,
PriceDto price,
String provenance,
DietaryRestriction dietaryRestriction)
implements Serializable {}
10 changes: 2 additions & 8 deletions src/main/java/nu/peg/svmeal/domain/model/PriceDto.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package nu.peg.svmeal.domain.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;

@Data
@AllArgsConstructor
public class PriceDto {
private double internalPrice;
private double externalPrice;
}
public record PriceDto(double internalPrice, double externalPrice) implements Serializable {}
15 changes: 3 additions & 12 deletions src/main/java/nu/peg/svmeal/domain/model/RestaurantDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class RestaurantDto implements Serializable {
private String name;
private String link;
private String shortcut;

@JsonProperty("public")
private boolean isPublic;
}
public record RestaurantDto(
String name, String link, String shortcut, @JsonProperty("public") boolean isPublic)
implements Serializable {}
6 changes: 3 additions & 3 deletions src/main/java/nu/peg/svmeal/domain/service/MealService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AvailabilityDto getAvailability(int dayOffset, RestaurantDto restaurant)
} catch (ExternalException | MealPlanParsingException e) {
log.warn(
"Meal plan unavailable for restaurant {} with dayOffset {}",
restaurant.getShortcut(),
restaurant.shortcut(),
dayOffset);
log.warn("Exception: ", e);
}
Expand All @@ -53,13 +53,13 @@ public MealPlanDto getMealPlan(int dayOffset, RestaurantDto restaurant) {
final var mealPlans = repository.getMealPlans(restaurant);

final LocalDate offsetDate =
mealPlans.getPlans().keySet().stream()
mealPlans.plans().keySet().stream()
.sorted()
.skip(dayOffset)
.findFirst()
.orElseThrow(() -> new MealPlanParsingException(NO_MEAL_PLAN_FOR_DAY));

return mealPlans.getPlans().get(offsetDate);
return mealPlans.plans().get(offsetDate);
}

public MealPlansDto getMealPlans(RestaurantDto restaurant) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package nu.peg.svmeal.infrastructure.config;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class CircuitBreakers {
public static final String SV_SEARCH = "svSearch";
public static final String SV_MENU = "svMenu";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import nu.peg.svmeal.domain.model.*;
import nu.peg.svmeal.infrastructure.util.DateUtil;
import org.jsoup.nodes.Document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
* TODO Short summary
*
* @author Joel Messerli @26.02.2017
*/
/** Converts a list of price elements to an internal and external price */
@Slf4j
@Component
public class ElementsToPriceDtoConverter implements Converter<Elements, PriceDto> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SvMealPlanRepository implements MealPlanRepository {
public MealPlansDto getMealPlans(RestaurantDto restaurant) {
log.debug("Scraping meal plan for restaurant {}", restaurant);

ResponseEntity<String> response = restTemplate.getForEntity(restaurant.getLink(), String.class);
ResponseEntity<String> response = restTemplate.getForEntity(restaurant.link(), String.class);

if (response.getStatusCode() != HttpStatus.OK) {
throw new ExternalException("Error while fetching meal plan");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ private static LocalDate parseDayMonthStringWithYear(String dayMonthDate, int ye

public static Supplier<LocalDate> dateSequenceGenerator(LocalDate startDate) {
final LocalDate[] lastDate = {startDate.minusDays(1)};
return () -> (lastDate[0] = lastDate[0].plusDays(1));
return () -> {
lastDate[0] = lastDate[0].plusDays(1);
return lastDate[0];
};
}

private DateUtil() {}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/nu/peg/svmeal/infrastructure/util/HttpUtil.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package nu.peg.svmeal.infrastructure.util;

import java.util.Map;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class HttpUtil {

public static HttpEntity<MultiValueMap<String, String>> getPostFormData(
Expand Down

0 comments on commit 81d1bb7

Please sign in to comment.