Refactors presentation and application layer.
This commit is contained in:
@@ -5,6 +5,11 @@
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
@@ -30,5 +35,6 @@
|
||||
<nature>org.springframework.ide.eclipse.core.springnature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.rogelioorts.training.spring.ddd.exceptions;
|
||||
|
||||
public class BadRequestException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public BadRequestException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,17 @@
|
||||
package com.rogelioorts.training.spring.ddd.services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.rogelioorts.training.spring.ddd.entities.Movie;
|
||||
import com.rogelioorts.training.spring.ddd.exceptions.BadRequestException;
|
||||
import com.rogelioorts.training.spring.ddd.factories.FiltersFactory;
|
||||
import com.rogelioorts.training.spring.ddd.repositories.MoviesRepository;
|
||||
import com.rogelioorts.training.spring.ddd.value.objects.Filter;
|
||||
import com.rogelioorts.training.spring.ddd.value.objects.impl.FiltersData;
|
||||
|
||||
@Service
|
||||
public class MoviesService {
|
||||
@@ -15,14 +19,38 @@ public class MoviesService {
|
||||
@Autowired
|
||||
private MoviesRepository moviesRepository;
|
||||
|
||||
public List<Movie> findAll(List<Filter> filters) {
|
||||
public List<Movie> findAll(List<String> filtersIds, FiltersData filterData) throws BadRequestException {
|
||||
List<Movie> movies = moviesRepository.findAll();
|
||||
|
||||
for(Filter filter: filters) {
|
||||
movies = filter.apply(movies);
|
||||
if(filtersIds != null) {
|
||||
for(Filter filter: getFilters(filtersIds, filterData)) {
|
||||
movies = filter.apply(movies);
|
||||
}
|
||||
}
|
||||
|
||||
return movies;
|
||||
}
|
||||
|
||||
private List<Filter> getFilters(List<String> filtersIds, FiltersData filterData) throws BadRequestException {
|
||||
List<Filter> result = new ArrayList<>();
|
||||
|
||||
for(String filterId : filtersIds) {
|
||||
result.add(getFilter(filterId, filterData));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Filter getFilter(String filterId, FiltersData filterData) throws BadRequestException {
|
||||
if(Filter.CENSORING.equals(filterId)) {
|
||||
if(filterData == null || filterData.getCensoringLevel() == null) {
|
||||
throw new BadRequestException("Censoring level cannot be empty if there is a \"censoring\" filter.");
|
||||
}
|
||||
|
||||
return FiltersFactory.censoringFilter(filterData.getCensoringLevel());
|
||||
} else {
|
||||
throw new BadRequestException("Not valid filter \"" + filterId + "\"");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
|
||||
@@ -5,6 +5,8 @@ import java.util.List;
|
||||
import com.rogelioorts.training.spring.ddd.entities.Movie;
|
||||
|
||||
public interface Filter {
|
||||
|
||||
public static final String CENSORING = "censoring";
|
||||
|
||||
List<Movie> apply(List<Movie> movies);
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.rogelioorts.training.spring.ddd.value.objects.impl;
|
||||
|
||||
import com.rogelioorts.training.spring.ddd.value.objects.CensoringLevel;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@Getter
|
||||
@Setter
|
||||
public class FiltersData {
|
||||
|
||||
private CensoringLevel censoringLevel;
|
||||
|
||||
}
|
||||
@@ -20,7 +20,6 @@
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
@@ -30,5 +35,6 @@
|
||||
<nature>org.springframework.ide.eclipse.core.springnature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.rogelioorts.training.spring.ddd.config;
|
||||
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import com.rogelioorts.training.spring.ddd.entities.ErrorResponse;
|
||||
import com.rogelioorts.training.spring.ddd.exceptions.BadRequestException;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@ControllerAdvice
|
||||
@Slf4j
|
||||
public class CustomExceptionHandler {
|
||||
|
||||
@ExceptionHandler(BadRequestException.class)
|
||||
public @ResponseBody ErrorResponse badRequestException(BadRequestException exception) {
|
||||
return new ErrorResponse("BAD_REQUEST", exception.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
public @ResponseBody ErrorResponse defaultHandler(Exception ex) {
|
||||
log.error("Error handled in controller: " + ex.getMessage(), ex);
|
||||
return new ErrorResponse("INTERNAL_SERVER", ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,13 +4,16 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.rogelioorts.training.spring.ddd.entities.FiltersDataApi;
|
||||
import com.rogelioorts.training.spring.ddd.entities.Movie;
|
||||
import com.rogelioorts.training.spring.ddd.entities.MovieApi;
|
||||
import com.rogelioorts.training.spring.ddd.exceptions.BadRequestException;
|
||||
import com.rogelioorts.training.spring.ddd.mappers.FiltersApiMapper;
|
||||
import com.rogelioorts.training.spring.ddd.mappers.MoviesApiMapper;
|
||||
import com.rogelioorts.training.spring.ddd.services.MoviesService;
|
||||
@@ -23,14 +26,14 @@ public class MoviesController {
|
||||
private MoviesService moviesService;
|
||||
|
||||
@Autowired
|
||||
private FiltersApiMapper filtersMapper;
|
||||
private MoviesApiMapper moviesMapper;
|
||||
|
||||
@Autowired
|
||||
private MoviesApiMapper moviesMapper;
|
||||
private FiltersApiMapper filtersApiMapper;
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public List<MovieApi> findAll(@RequestParam(required = false) List<String> filters, @RequestParam(required = false) String level) {
|
||||
List<Movie> movies = moviesService.findAll(filtersMapper.map(filters, level));
|
||||
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public List<MovieApi> findAll(@RequestParam(required = false) List<String> filters, FiltersDataApi data) throws BadRequestException {
|
||||
List<Movie> movies = moviesService.findAll(filters, filtersApiMapper.map(data));
|
||||
|
||||
return movies.stream().map(moviesMapper::map).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.rogelioorts.training.spring.ddd.entities;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class ErrorResponse {
|
||||
|
||||
private boolean error;
|
||||
|
||||
private String errorCode;
|
||||
|
||||
private String message;
|
||||
|
||||
public ErrorResponse(String errorCode, String message) {
|
||||
this.error = true;
|
||||
this.errorCode = errorCode;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.rogelioorts.training.spring.ddd.entities;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class FiltersDataApi {
|
||||
|
||||
private String level;
|
||||
|
||||
}
|
||||
@@ -1,46 +1,31 @@
|
||||
package com.rogelioorts.training.spring.ddd.mappers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.rogelioorts.training.spring.ddd.factories.FiltersFactory;
|
||||
import com.rogelioorts.training.spring.ddd.entities.FiltersDataApi;
|
||||
import com.rogelioorts.training.spring.ddd.value.objects.CensoringLevel;
|
||||
import com.rogelioorts.training.spring.ddd.value.objects.Filter;
|
||||
import com.rogelioorts.training.spring.ddd.value.objects.impl.FiltersData;
|
||||
|
||||
@Component
|
||||
public class FiltersApiMapper {
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface FiltersApiMapper {
|
||||
|
||||
public List<Filter> map(List<String> rawFilters, String censoringLevel) {
|
||||
List<Filter> filters = new ArrayList<>();
|
||||
|
||||
if(rawFilters != null) {
|
||||
for(String filterId : rawFilters) {
|
||||
filters.add(map(filterId, censoringLevel));
|
||||
}
|
||||
}
|
||||
|
||||
return filters;
|
||||
}
|
||||
@Mapping(source = "level", target = "censoringLevel")
|
||||
FiltersData map(FiltersDataApi data);
|
||||
|
||||
private Filter map(String filterId, String censoringLevel) {
|
||||
if("censoring".equals(filterId)) {
|
||||
return FiltersFactory.censoringFilter(mapCensoringLevel(censoringLevel));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Not valid filter ID \"" + filterId + "\"");
|
||||
}
|
||||
}
|
||||
|
||||
private CensoringLevel mapCensoringLevel(String censoringLevel) {
|
||||
switch(censoringLevel) {
|
||||
case "CENSORED":
|
||||
return CensoringLevel.CENSORED;
|
||||
case "UNCENSORED":
|
||||
return CensoringLevel.UNCENSORED;
|
||||
default:
|
||||
throw new IllegalArgumentException("Not valid censoring level \"" + censoringLevel + "\"");
|
||||
}
|
||||
}
|
||||
default CensoringLevel map(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch(value) {
|
||||
case "censored":
|
||||
return CensoringLevel.CENSORED;
|
||||
case "uncensored":
|
||||
return CensoringLevel.UNCENSORED;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user