Refactors presentation and application layer.
This commit is contained in:
@@ -5,6 +5,11 @@
|
|||||||
<projects>
|
<projects>
|
||||||
</projects>
|
</projects>
|
||||||
<buildSpec>
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
@@ -30,5 +35,6 @@
|
|||||||
<nature>org.springframework.ide.eclipse.core.springnature</nature>
|
<nature>org.springframework.ide.eclipse.core.springnature</nature>
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
</projectDescription>
|
</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;
|
package com.rogelioorts.training.spring.ddd.services;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.rogelioorts.training.spring.ddd.entities.Movie;
|
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.repositories.MoviesRepository;
|
||||||
import com.rogelioorts.training.spring.ddd.value.objects.Filter;
|
import com.rogelioorts.training.spring.ddd.value.objects.Filter;
|
||||||
|
import com.rogelioorts.training.spring.ddd.value.objects.impl.FiltersData;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class MoviesService {
|
public class MoviesService {
|
||||||
@@ -15,14 +19,38 @@ public class MoviesService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private MoviesRepository moviesRepository;
|
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();
|
List<Movie> movies = moviesRepository.findAll();
|
||||||
|
|
||||||
for(Filter filter: filters) {
|
if(filtersIds != null) {
|
||||||
|
for(Filter filter: getFilters(filtersIds, filterData)) {
|
||||||
movies = filter.apply(movies);
|
movies = filter.apply(movies);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 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>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
</buildSpec>
|
</buildSpec>
|
||||||
<natures>
|
<natures>
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import com.rogelioorts.training.spring.ddd.entities.Movie;
|
|||||||
|
|
||||||
public interface Filter {
|
public interface Filter {
|
||||||
|
|
||||||
|
public static final String CENSORING = "censoring";
|
||||||
|
|
||||||
List<Movie> apply(List<Movie> movies);
|
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>
|
||||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="ignore_optional_problems" value="true"/>
|
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
|
|||||||
@@ -5,6 +5,11 @@
|
|||||||
<projects>
|
<projects>
|
||||||
</projects>
|
</projects>
|
||||||
<buildSpec>
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
@@ -30,5 +35,6 @@
|
|||||||
<nature>org.springframework.ide.eclipse.core.springnature</nature>
|
<nature>org.springframework.ide.eclipse.core.springnature</nature>
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
</projectDescription>
|
</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 java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
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.Movie;
|
||||||
import com.rogelioorts.training.spring.ddd.entities.MovieApi;
|
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.FiltersApiMapper;
|
||||||
import com.rogelioorts.training.spring.ddd.mappers.MoviesApiMapper;
|
import com.rogelioorts.training.spring.ddd.mappers.MoviesApiMapper;
|
||||||
import com.rogelioorts.training.spring.ddd.services.MoviesService;
|
import com.rogelioorts.training.spring.ddd.services.MoviesService;
|
||||||
@@ -22,15 +25,15 @@ public class MoviesController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private MoviesService moviesService;
|
private MoviesService moviesService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FiltersApiMapper filtersMapper;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MoviesApiMapper moviesMapper;
|
private MoviesApiMapper moviesMapper;
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET)
|
@Autowired
|
||||||
public List<MovieApi> findAll(@RequestParam(required = false) List<String> filters, @RequestParam(required = false) String level) {
|
private FiltersApiMapper filtersApiMapper;
|
||||||
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());
|
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,45 +1,30 @@
|
|||||||
package com.rogelioorts.training.spring.ddd.mappers;
|
package com.rogelioorts.training.spring.ddd.mappers;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import org.mapstruct.Mapper;
|
||||||
import java.util.List;
|
import org.mapstruct.Mapping;
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import com.rogelioorts.training.spring.ddd.entities.FiltersDataApi;
|
||||||
|
|
||||||
import com.rogelioorts.training.spring.ddd.factories.FiltersFactory;
|
|
||||||
import com.rogelioorts.training.spring.ddd.value.objects.CensoringLevel;
|
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
|
@Mapper(componentModel = "spring")
|
||||||
public class FiltersApiMapper {
|
public interface FiltersApiMapper {
|
||||||
|
|
||||||
public List<Filter> map(List<String> rawFilters, String censoringLevel) {
|
@Mapping(source = "level", target = "censoringLevel")
|
||||||
List<Filter> filters = new ArrayList<>();
|
FiltersData map(FiltersDataApi data);
|
||||||
|
|
||||||
if(rawFilters != null) {
|
default CensoringLevel map(String value) {
|
||||||
for(String filterId : rawFilters) {
|
if (value == null) {
|
||||||
filters.add(map(filterId, censoringLevel));
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return filters;
|
switch(value) {
|
||||||
}
|
case "censored":
|
||||||
|
|
||||||
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;
|
return CensoringLevel.CENSORED;
|
||||||
case "UNCENSORED":
|
case "uncensored":
|
||||||
return CensoringLevel.UNCENSORED;
|
return CensoringLevel.UNCENSORED;
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Not valid censoring level \"" + censoringLevel + "\"");
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user