Skip to content

Commit

Permalink
refactor: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Oct 1, 2023
1 parent 0d96dea commit 9e69bb6
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@
package ch.xxx.moviemanager.adapter.config;

import java.io.IOException;
import java.util.Optional;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.GenericFilterBean;

import ch.xxx.moviemanager.usecase.service.JwtTokenService;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.GenericFilterBean;

import ch.xxx.moviemanager.usecase.service.JwtTokenService;

public class JwtTokenFilter extends GenericFilterBean {

public class JwtTokenFilter extends GenericFilterBean {

private JwtTokenService jwtTokenProvider;

public JwtTokenFilter(JwtTokenService jwtTokenProvider) {
Expand All @@ -42,11 +40,11 @@ public JwtTokenFilter(JwtTokenService jwtTokenProvider) {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
throws IOException, ServletException {

String token = jwtTokenProvider.resolveToken((HttpServletRequest) req);
if (token != null && jwtTokenProvider.validateToken(token)) {
Authentication auth = token != null ? jwtTokenProvider.getAuthentication(token) : null;
SecurityContextHolder.getContext().setAuthentication(auth);
}
Optional<String> tokenOpt = jwtTokenProvider.resolveToken((HttpServletRequest) req);
tokenOpt.stream().filter(myToken -> jwtTokenProvider.validateToken(myToken)).findFirst().ifPresentOrElse(
token -> SecurityContextHolder.getContext()
.setAuthentication(jwtTokenProvider.getAuthentication(token)),
() -> SecurityContextHolder.getContext().setAuthentication(null));

filterChain.doFilter(req, res);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
*/
package ch.xxx.moviemanager.adapter.config;

import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.sql.internal.DdlTypeImpl;
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;

public class PGSQLMapDialect extends PostgreSQLDialect {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ActorController(ActorService service, DefaultMapper mapper, UserDetailSer
public List<ActorDto> getActorSearch(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr,
@PathVariable("name") String name) throws InterruptedException {
List<ActorDto> actors = this.service.findActor(name, bearerStr).stream()
.map(a -> this.mapper.convertOnlyActor(a)).toList();
.map(this.mapper::convertOnlyActor).toList();
return actors;
}

Expand All @@ -65,7 +65,7 @@ public ResponseEntity<ActorDto> getActorSearchById(
Optional<ActorDto> actorOpt = this.service.findActorById(id, bearerStr).stream()
.filter(myActor -> myActor.getCasts().stream()
.filter(c -> c.getMovie().getUsers().contains(currentUser)).findFirst().isPresent())
.map(a -> this.mapper.convert(a)).findFirst();
.map(this.mapper::convert).findFirst();
return actorOpt.stream().map(myActor -> new ResponseEntity<ActorDto>(myActor, HttpStatus.OK)).findFirst()
.orElse(new ResponseEntity<ActorDto>(new ActorDto(), HttpStatus.NOT_FOUND));
}
Expand All @@ -74,7 +74,7 @@ public ResponseEntity<ActorDto> getActorSearchById(
"page" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<ActorDto> getPagesByNumber(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr,
@RequestParam("page") Integer page) throws InterruptedException {
List<ActorDto> actors = this.service.findActorsByPage(page, bearerStr).stream().map(a -> this.mapper.convert(a))
List<ActorDto> actors = this.service.findActorsByPage(page, bearerStr).stream().map(this.mapper::convert)
.collect(Collectors.toList());
return actors;
}
Expand All @@ -83,14 +83,14 @@ public List<ActorDto> getPagesByNumber(@RequestHeader(value = HttpHeaders.AUTHOR
public List<ActorDto> getActorsByCriteria(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr,
@RequestBody ActorFilterCriteriaDto filterCriteria) {
return this.service.findActorsByFilterCriteria(bearerStr, filterCriteria).stream()
.map(m -> this.mapper.convert(m)).toList();
.map(this.mapper::convert).toList();
}

@RequestMapping(value = "/searchterm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public List<ActorDto> postSearchTerm(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr,
SearchTermDto searchTermDto) {
List<ActorDto> results = this.service.findActorsBySearchTerm(bearerStr, searchTermDto).stream()
.map(myActor -> this.mapper.convert(myActor)).toList();
.map(this.mapper::convert).toList();
return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ public AuthenticationController(UserDetailService userDetailService) {
@PostMapping("/authorize")
public AuthCheckDto postAuthorize(@RequestBody AuthCheckDto authcheck, @RequestHeader Map<String, String> header) {
String tokenRoles = this.userDetailService.getTokenRoles(header).role();
if (tokenRoles != null && tokenRoles.contains(Role.USERS.name()) && !tokenRoles.contains(Role.GUEST.name())) {
return new AuthCheckDto(authcheck.getPath(), true);
} else {
return new AuthCheckDto(authcheck.getPath(), false);
}
return (tokenRoles != null && tokenRoles.contains(Role.USERS.name()) && !tokenRoles.contains(Role.GUEST.name())) ?
new AuthCheckDto(authcheck.getPath(), true) : new AuthCheckDto(authcheck.getPath(), false);
}

@PostMapping("/signin")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ public ResponseEntity<Boolean> deleteMovieById(@RequestHeader(value = HttpHeader
public List<MovieDto> getGeneresById(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr,
@PathVariable("id") Long id) throws InterruptedException {
List<MovieDto> movies = this.service.findMoviesByGenereId(id, bearerStr).stream()
.map(m -> this.mapper.convertMovieWithGenere(m)).toList();
.map(this.mapper::convertMovieWithGenere).toList();
return movies;
}

@RequestMapping(value = "/generes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<GenereDto> getGeneres() throws InterruptedException {
List<GenereDto> generes = this.service.findAllGeneres().stream().map(gen -> this.mapper.convert(gen)).toList();
List<GenereDto> generes = this.service.findAllGeneres().stream().map(this.mapper::convert).toList();
return generes;
}

@RequestMapping(value = "/pages", params = {
"page" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<MovieDto> getPagesByNumber(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr,
@RequestParam("page") Integer page) throws InterruptedException {
List<MovieDto> movies = this.service.findMoviesByPage(page, bearerStr).stream().map(m -> this.mapper.convert(m))
List<MovieDto> movies = this.service.findMoviesByPage(page, bearerStr).stream().map(this.mapper::convert)
.toList();
return movies;
}
Expand All @@ -95,14 +95,14 @@ public List<MovieDto> getPagesByNumber(@RequestHeader(value = HttpHeaders.AUTHOR
public List<MovieDto> getMoviesByCriteria(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr,
@RequestBody MovieFilterCriteriaDto filterCriteria) {
return this.service.findMoviesByFilterCriteria(bearerStr, filterCriteria).stream()
.map(m -> this.mapper.convert(m)).toList();
.map(this.mapper::convert).toList();
}

@RequestMapping(value = "/searchterm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public List<MovieDto> postSearchTerm(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr,
SearchTermDto searchTermDto) {
List<MovieDto> results = this.service.findMoviesBySearchTerm(bearerStr, searchTermDto).stream()
.map(myMovie -> this.mapper.convert(myMovie)).toList();
.map(this.mapper::convert).toList();
return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,35 @@ public List<Actor> findByFilterCriteria(ActorFilterCriteriaDto filterCriteriaDto
CriteriaQuery<Actor> cq = this.entityManager.getCriteriaBuilder().createQuery(Actor.class);
Root<Actor> cActor = cq.from(Actor.class);
List<Predicate> predicates = new ArrayList<>();
if (filterCriteriaDto.getBirthdayFrom() != null) {
predicates.add(this.entityManager.getCriteriaBuilder().greaterThanOrEqualTo(cActor.<Date>get("birthday"),
CommonUtils.convert(filterCriteriaDto.getBirthdayFrom())));
}
if (filterCriteriaDto.getBirthdayTo() != null) {
predicates.add(this.entityManager.getCriteriaBuilder().lessThanOrEqualTo(cActor.<Date>get("birthday"),
CommonUtils.convert(filterCriteriaDto.getBirthdayTo())));
}
if (filterCriteriaDto.getDead()) {
predicates.add(this.entityManager.getCriteriaBuilder().isNotNull(cActor.<Date>get("deathday")));
}
if (filterCriteriaDto.getGender() != null && !filterCriteriaDto.getGender().equals(Gender.Unknown)) {
predicates.add(this.entityManager.getCriteriaBuilder().equal(cActor.get("gender"),
filterCriteriaDto.getGender().getCode()));
}
if (filterCriteriaDto.getName() != null && filterCriteriaDto.getName().trim().length() > 2) {
predicates.add(this.entityManager.getCriteriaBuilder().like(
this.entityManager.getCriteriaBuilder().lower(cActor.get("name")),
String.format("%%%s%%", filterCriteriaDto.getName().toLowerCase())));
}
Optional.ofNullable(filterCriteriaDto.getBirthdayFrom())
.ifPresent(x -> predicates.add(this.entityManager.getCriteriaBuilder().greaterThanOrEqualTo(
cActor.<Date>get("birthday"), CommonUtils.convert(filterCriteriaDto.getBirthdayFrom()))));
Optional.ofNullable(filterCriteriaDto.getBirthdayTo())
.ifPresent(x -> predicates.add(this.entityManager.getCriteriaBuilder().lessThanOrEqualTo(
cActor.<Date>get("birthday"), CommonUtils.convert(filterCriteriaDto.getBirthdayTo()))));
Optional.ofNullable(filterCriteriaDto.getDead()).ifPresent(
x -> predicates.add(this.entityManager.getCriteriaBuilder().isNotNull(cActor.<Date>get("deathday"))));
Optional.ofNullable(filterCriteriaDto.getGender()).stream().filter(myGender -> !Gender.Unknown.equals(myGender))
.findFirst().ifPresent(x -> predicates.add(this.entityManager.getCriteriaBuilder()
.equal(cActor.get("gender"), filterCriteriaDto.getGender().getCode())));
Optional.ofNullable(filterCriteriaDto.getName()).stream().filter(myName -> myName.trim().length() > 2)
.findFirst()
.ifPresent(x -> predicates.add(this.entityManager.getCriteriaBuilder().like(
this.entityManager.getCriteriaBuilder().lower(cActor.get("name")),
String.format("%%%s%%", filterCriteriaDto.getName().toLowerCase()))));
if (filterCriteriaDto.getPopularity() > 0) {
predicates.add(this.entityManager.getCriteriaBuilder().greaterThanOrEqualTo(cActor.get("popularity"),
Double.valueOf(Integer.valueOf(filterCriteriaDto.getPopularity()).toString())));
}
if (filterCriteriaDto.getMovieCharacter() != null
&& filterCriteriaDto.getMovieCharacter().trim().length() > 2) {
Metamodel m = this.entityManager.getMetamodel();
EntityType<Actor> actor_ = m.entity(Actor.class);
predicates.add(this.entityManager.getCriteriaBuilder()
.like(this.entityManager.getCriteriaBuilder()
.lower(cActor.join(actor_.getDeclaredList("casts", Cast.class)).get("movieChar")),
String.format("%%%s%%", filterCriteriaDto.getMovieCharacter().toLowerCase())));
}
Optional.ofNullable(filterCriteriaDto.getMovieCharacter()).stream()
.filter(myCharacter -> myCharacter.trim().length() > 2).findFirst().ifPresent(x -> {
Metamodel m = this.entityManager.getMetamodel();
EntityType<Actor> actor_ = m.entity(Actor.class);
predicates.add(this.entityManager.getCriteriaBuilder()
.like(this.entityManager.getCriteriaBuilder()
.lower(cActor.join(actor_.getDeclaredList("casts", Cast.class)).get("movieChar")),
String.format("%%%s%%", filterCriteriaDto.getMovieCharacter().toLowerCase())));
});
// user check
Metamodel m = this.entityManager.getMetamodel();
EntityType<Actor> actor_ = m.entity(Actor.class);
Expand All @@ -126,19 +122,20 @@ public List<Actor> findByFilterCriteria(ActorFilterCriteriaDto filterCriteriaDto
}

public List<Actor> findActorsByPhrase(SearchPhraseDto searchPhraseDto) {
List<Actor> resultList = List.of();
if (searchPhraseDto.getPhrase() != null && searchPhraseDto.getPhrase().trim().length() > 2) {
final List<Actor> resultList = new ArrayList<>();
Optional.ofNullable(searchPhraseDto.getPhrase()).stream().filter(myPhrase -> myPhrase.trim().length() > 2).findFirst().ifPresent(x -> {
SearchSession searchSession = Search.session(this.entityManager);
resultList = searchSession.search(Actor.class).where(f -> f.phrase().field("biography")
.matching(searchPhraseDto.getPhrase()).slop(searchPhraseDto.getOtherWordsInPhrase())).fetchHits(1000);
}
resultList.addAll(searchSession.search(Actor.class).where(f -> f.phrase().field("biography")
.matching(searchPhraseDto.getPhrase()).slop(searchPhraseDto.getOtherWordsInPhrase()))
.fetchHits(1000));
});
return resultList;
}

public List<Actor> findActorsBySearchStrings(List<SearchStringDto> searchStrings) {
StringBuilder stringBuilder = new StringBuilder();
searchStrings.forEach(myDto -> stringBuilder.append(" ").append(myDto.getOperator().value).append(" ")
.append(myDto.getSearchString()));
.append(myDto.getSearchString()));
List<Actor> resultList = Search.session(this.entityManager).search(Actor.class)
.where(f -> f.simpleQueryString().field("biography").matching(stringBuilder.substring(2)))
.fetchHits(1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,28 @@ public List<Movie> findByFilterCriteria(MovieFilterCriteriaDto filterCriteriaDto
CriteriaQuery<Movie> cq = this.entityManager.getCriteriaBuilder().createQuery(Movie.class);
Root<Movie> cMovie = cq.from(Movie.class);
List<Predicate> predicates = new ArrayList<>();
if (filterCriteriaDto.getReleaseFrom() != null) {
predicates.add(this.entityManager.getCriteriaBuilder().greaterThanOrEqualTo(cMovie.<Date>get("releaseDate"),
CommonUtils.convert(filterCriteriaDto.getReleaseFrom())));
}
if (filterCriteriaDto.getReleaseTo() != null) {
predicates.add(this.entityManager.getCriteriaBuilder().lessThanOrEqualTo(cMovie.<Date>get("releaseDate"),
CommonUtils.convert(filterCriteriaDto.getReleaseTo())));
}
if (filterCriteriaDto.getMovieTitle() != null && filterCriteriaDto.getMovieTitle().trim().length() > 2) {
predicates.add(this.entityManager.getCriteriaBuilder().like(
this.entityManager.getCriteriaBuilder().lower(cMovie.get("title")),
String.format("%%%s%%", filterCriteriaDto.getMovieTitle().toLowerCase())));
}
if (filterCriteriaDto.getMovieActor() != null && filterCriteriaDto.getMovieActor().trim().length() > 2) {
Metamodel m = this.entityManager.getMetamodel();
EntityType<Movie> movie_ = m.entity(Movie.class);
predicates
.add(this.entityManager
.getCriteriaBuilder().like(
Optional.ofNullable(filterCriteriaDto.getReleaseFrom())
.ifPresent(x -> predicates.add(this.entityManager.getCriteriaBuilder().greaterThanOrEqualTo(
cMovie.<Date>get("releaseDate"), CommonUtils.convert(filterCriteriaDto.getReleaseFrom()))));
Optional.ofNullable(filterCriteriaDto.getReleaseTo())
.ifPresent(x -> predicates.add(this.entityManager.getCriteriaBuilder().lessThanOrEqualTo(
cMovie.<Date>get("releaseDate"), CommonUtils.convert(filterCriteriaDto.getReleaseTo()))));
Optional.ofNullable(filterCriteriaDto.getMovieTitle()).stream().filter(myTitle -> myTitle.trim().length() > 2)
.findFirst()
.ifPresent(x -> predicates.add(this.entityManager.getCriteriaBuilder().like(
this.entityManager.getCriteriaBuilder().lower(cMovie.get("title")),
String.format("%%%s%%", filterCriteriaDto.getMovieTitle().toLowerCase()))));
Optional.ofNullable(filterCriteriaDto.getMovieActor()).stream().filter(myActor -> myActor.trim().length() > 2)
.findFirst().ifPresent(x -> {
Metamodel m = this.entityManager.getMetamodel();
EntityType<Movie> movie_ = m.entity(Movie.class);
predicates
.add(this.entityManager.getCriteriaBuilder().like(
this.entityManager.getCriteriaBuilder()
.lower(cMovie.join(movie_.getDeclaredList("cast", Cast.class))
.get("characterName")),
String.format("%%%s%%", filterCriteriaDto.getMovieActor().toLowerCase())));
}
});
if (!filterCriteriaDto.getSelectedGeneres().isEmpty()) {
Metamodel m = this.entityManager.getMetamodel();
EntityType<Movie> movie_ = m.entity(Movie.class);
Expand Down Expand Up @@ -159,8 +157,9 @@ public List<Movie> findByFilterCriteria(MovieFilterCriteriaDto filterCriteriaDto
public List<Movie> findMoviesByPhrase(SearchPhraseDto searchPhraseDto) {
List<Movie> resultList = List.of();
if (searchPhraseDto.getPhrase() != null && searchPhraseDto.getPhrase().trim().length() > 2) {
resultList = Search.session(this.entityManager).search(Movie.class).where(f -> f.phrase().field("overview")
.matching(searchPhraseDto.getPhrase()).slop(searchPhraseDto.getOtherWordsInPhrase()))
resultList = Search
.session(this.entityManager).search(Movie.class).where(f -> f.phrase().field("overview")
.matching(searchPhraseDto.getPhrase()).slop(searchPhraseDto.getOtherWordsInPhrase()))
.fetchHits(1000);
}
return resultList;
Expand Down
Loading

0 comments on commit 9e69bb6

Please sign in to comment.