Skip to content

Commit

Permalink
refactor: actor controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Jul 14, 2024
1 parent a2a761e commit 1bd733e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import ch.xxx.moviemanager.domain.model.dto.ActorDto;
import ch.xxx.moviemanager.domain.model.dto.ActorFilterCriteriaDto;
import ch.xxx.moviemanager.domain.model.dto.SearchTermDto;
import ch.xxx.moviemanager.domain.model.entity.User;
import ch.xxx.moviemanager.usecase.mapper.DefaultMapper;
import ch.xxx.moviemanager.usecase.service.ActorService;
import ch.xxx.moviemanager.usecase.service.UserDetailService;
Expand All @@ -52,20 +51,17 @@ public ActorController(ActorService service, DefaultMapper mapper, UserDetailSer
@RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
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(this.mapper::convertOnlyActor).toList();
List<ActorDto> actors = this.service.findActor(name, bearerStr).stream().map(this.mapper::convertOnlyActor)
.toList();
return actors;
}

@RequestMapping(value = "/id/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ActorDto> getActorSearchById(
@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr, @PathVariable("id") Long id)
throws InterruptedException {
final User currentUser = this.auds.getCurrentUser(bearerStr);
Optional<ActorDto> actorOpt = this.service.findActorById(id, bearerStr).stream()
.filter(myActor -> myActor.getCasts().stream()
.filter(c -> c.getMovie().getUsers().contains(currentUser)).findFirst().isPresent())
.map(this.mapper::convert).findFirst();
Optional<ActorDto> actorOpt = this.service.findActorById(id, bearerStr).stream().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 @@ -82,10 +78,10 @@ public List<ActorDto> getPagesByNumber(@RequestHeader(value = HttpHeaders.AUTHOR
@RequestMapping(value = "/filter-criteria", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public List<ActorDto> getActorsByCriteria(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr,
@RequestBody ActorFilterCriteriaDto filterCriteria) {
return this.service.findActorsByFilterCriteria(bearerStr, filterCriteria).stream()
.map(this.mapper::convert).toList();
return this.service.findActorsByFilterCriteria(bearerStr, filterCriteria).stream().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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public List<Actor> findActorsByPage(Integer page, String bearerStr) {
public Optional<Actor> findActorById(Long id, String bearerStr) {
final User user = this.auds.getCurrentUser(bearerStr);
Optional<Actor> result = this.actorRep.findById(id)
.filter(myActor -> myActor.getUsers().stream().anyMatch(myUser -> user.getId().equals(myUser.getId())));
.filter(myActor -> myActor.getUsers().stream().anyMatch(myUser -> user.getId().equals(myUser.getId())))
.filter(myActor -> myActor.getCasts().stream()
.filter(c -> c.getMovie().getUsers().contains(user)).findFirst().isPresent());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ public void testGetActorSearchByName() throws Exception {
@Test
public void testGetActorSearchByIdFound() throws Exception {
Actor actor = createTestActor();
var cast = new Cast();
var movie = new Movie();
movie.getUsers().add(this.auds.getCurrentUser("XXX"));
cast.setMovie(movie);
actor.getCasts().add(cast);
Mockito.when(service.findActorById(any(), any())).thenReturn(Optional.of(actor));

mockMvc.perform(get("/rest/actor/id/{id}", 1L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import ch.xxx.moviemanager.domain.model.dto.SearchTermDto;
import ch.xxx.moviemanager.domain.model.entity.Actor;
import ch.xxx.moviemanager.domain.model.entity.ActorRepository;
import ch.xxx.moviemanager.domain.model.entity.Cast;
import ch.xxx.moviemanager.domain.model.entity.Movie;
import ch.xxx.moviemanager.domain.model.entity.User;
import ch.xxx.moviemanager.usecase.mapper.DefaultMapper;

Expand Down Expand Up @@ -86,6 +88,11 @@ public void testFindActorById() {
Actor myActor = createTestActorEntity();
User user = new User();
user.setId(1L);
var cast = new Cast();
var movie = new Movie();
movie.getUsers().add(user);
cast.setMovie(movie);
myActor.getCasts().add(cast);
Mockito.when(userDetailService.getCurrentUser("YYY")).thenReturn(user);
Mockito.when(actorRep.findById(any())).thenReturn(Optional.of(myActor));
Optional<Actor> actorOpt = actorService.findActorById(1L, "YYY");
Expand Down

0 comments on commit 1bd733e

Please sign in to comment.