Adds presentation layer.
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
package com.rogelioorts.training.spring.ddd.services;
|
||||||
|
|
||||||
|
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.repositories.MoviesRepository;
|
||||||
|
import com.rogelioorts.training.spring.ddd.value.objects.Filter;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MoviesService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MoviesRepository moviesRepository;
|
||||||
|
|
||||||
|
public List<Movie> findAll(List<Filter> filters) {
|
||||||
|
List<Movie> movies = moviesRepository.findAll();
|
||||||
|
|
||||||
|
for(Filter filter: filters) {
|
||||||
|
movies = filter.apply(movies);
|
||||||
|
}
|
||||||
|
|
||||||
|
return movies;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import com.rogelioorts.training.spring.ddd.entities.Movie;
|
|||||||
import com.rogelioorts.training.spring.ddd.value.objects.CensoringLevel;
|
import com.rogelioorts.training.spring.ddd.value.objects.CensoringLevel;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class FeignMoviesMapper {
|
public class MoviesFeignMapper {
|
||||||
|
|
||||||
public Movie map(FeignMovie in) {
|
public Movie map(FeignMovie in) {
|
||||||
Movie out = new Movie();
|
Movie out = new Movie();
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
|
|
||||||
import com.rogelioorts.training.spring.ddd.entities.PageFeignMovie;
|
import com.rogelioorts.training.spring.ddd.entities.PageFeignMovie;
|
||||||
|
|
||||||
@FeignClient("${movies-server.url}")
|
@FeignClient(name = "MoviesFeign", url = "${movies-server.url}")
|
||||||
public interface MoviesFeignRepository {
|
public interface MoviesFeignRepository {
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, path = "/movies")
|
@RequestMapping(method = RequestMethod.GET, path = "/movies")
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.rogelioorts.training.spring.ddd.entities.Movie;
|
import com.rogelioorts.training.spring.ddd.entities.Movie;
|
||||||
import com.rogelioorts.training.spring.ddd.mappers.FeignMoviesMapper;
|
import com.rogelioorts.training.spring.ddd.mappers.MoviesFeignMapper;
|
||||||
import com.rogelioorts.training.spring.ddd.repositories.MoviesFeignRepository;
|
import com.rogelioorts.training.spring.ddd.repositories.MoviesFeignRepository;
|
||||||
import com.rogelioorts.training.spring.ddd.repositories.MoviesRepository;
|
import com.rogelioorts.training.spring.ddd.repositories.MoviesRepository;
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ public class MoviesFeignRepositoryImpl implements MoviesRepository {
|
|||||||
private MoviesFeignRepository repository;
|
private MoviesFeignRepository repository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FeignMoviesMapper mapper;
|
private MoviesFeignMapper mapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Movie> findAll() {
|
public List<Movie> findAll() {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="target/generated-sources/annotations"/>
|
||||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
@@ -19,6 +20,7 @@
|
|||||||
</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>
|
||||||
|
|||||||
@@ -17,6 +17,10 @@
|
|||||||
<artifactId>spring-ddd-domain</artifactId>
|
<artifactId>spring-ddd-domain</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-web</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-webmvc</artifactId>
|
<artifactId>spring-webmvc</artifactId>
|
||||||
@@ -25,5 +29,36 @@
|
|||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-beans</artifactId>
|
<artifactId>spring-beans</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mapstruct</groupId>
|
||||||
|
<artifactId>mapstruct-jdk8</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.7.0</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.mapstruct</groupId>
|
||||||
|
<artifactId>mapstruct-processor</artifactId>
|
||||||
|
<version>${mapstruct.version}</version>
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
</project>
|
</project>
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.rogelioorts.training.spring.ddd.controllers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
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.Movie;
|
||||||
|
import com.rogelioorts.training.spring.ddd.entities.MovieApi;
|
||||||
|
import com.rogelioorts.training.spring.ddd.mappers.FiltersApiMapper;
|
||||||
|
import com.rogelioorts.training.spring.ddd.mappers.MoviesApiMapper;
|
||||||
|
import com.rogelioorts.training.spring.ddd.services.MoviesService;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/movies")
|
||||||
|
public class MoviesController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MoviesService moviesService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FiltersApiMapper filtersMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MoviesApiMapper moviesMapper;
|
||||||
|
|
||||||
|
@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));
|
||||||
|
|
||||||
|
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 MovieApi {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
private String image;
|
||||||
|
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
private double punctuation;
|
||||||
|
|
||||||
|
private String censoringLevel;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.rogelioorts.training.spring.ddd.mappers;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
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.Filter;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 + "\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.rogelioorts.training.spring.ddd.mappers;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
|
||||||
|
import com.rogelioorts.training.spring.ddd.entities.Movie;
|
||||||
|
import com.rogelioorts.training.spring.ddd.entities.MovieApi;
|
||||||
|
|
||||||
|
@Mapper(componentModel = "spring")
|
||||||
|
public interface MoviesApiMapper {
|
||||||
|
|
||||||
|
@Mapping(source = "movieId", target = "id")
|
||||||
|
MovieApi map(Movie movie);
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user