From 1bd733e834ce859868af3a85e7ed1714cfe1c5ea Mon Sep 17 00:00:00 2001 From: Angular2guy Date: Sun, 14 Jul 2024 10:14:51 +0200 Subject: [PATCH] refactor: actor controller --- .../adapter/controller/ActorController.java | 18 +++++++----------- .../usecase/service/ActorService.java | 4 +++- .../controller/ActorControllerTest.java | 5 ----- .../usecase/service/ActorServiceTest.java | 7 +++++++ 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/backend/src/main/java/ch/xxx/moviemanager/adapter/controller/ActorController.java b/backend/src/main/java/ch/xxx/moviemanager/adapter/controller/ActorController.java index c1c9942..9b37240 100644 --- a/backend/src/main/java/ch/xxx/moviemanager/adapter/controller/ActorController.java +++ b/backend/src/main/java/ch/xxx/moviemanager/adapter/controller/ActorController.java @@ -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; @@ -52,8 +51,8 @@ public ActorController(ActorService service, DefaultMapper mapper, UserDetailSer @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public List getActorSearch(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr, @PathVariable("name") String name) throws InterruptedException { - List actors = this.service.findActor(name, bearerStr).stream() - .map(this.mapper::convertOnlyActor).toList(); + List actors = this.service.findActor(name, bearerStr).stream().map(this.mapper::convertOnlyActor) + .toList(); return actors; } @@ -61,11 +60,8 @@ public List getActorSearch(@RequestHeader(value = HttpHeaders.AUTHORIZ public ResponseEntity getActorSearchById( @RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr, @PathVariable("id") Long id) throws InterruptedException { - final User currentUser = this.auds.getCurrentUser(bearerStr); - Optional 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 actorOpt = this.service.findActorById(id, bearerStr).stream().map(this.mapper::convert) + .findFirst(); return actorOpt.stream().map(myActor -> new ResponseEntity(myActor, HttpStatus.OK)).findFirst() .orElse(new ResponseEntity(new ActorDto(), HttpStatus.NOT_FOUND)); } @@ -82,10 +78,10 @@ public List getPagesByNumber(@RequestHeader(value = HttpHeaders.AUTHOR @RequestMapping(value = "/filter-criteria", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public List 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 postSearchTerm(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String bearerStr, SearchTermDto searchTermDto) { diff --git a/backend/src/main/java/ch/xxx/moviemanager/usecase/service/ActorService.java b/backend/src/main/java/ch/xxx/moviemanager/usecase/service/ActorService.java index 1b953e7..7b58562 100644 --- a/backend/src/main/java/ch/xxx/moviemanager/usecase/service/ActorService.java +++ b/backend/src/main/java/ch/xxx/moviemanager/usecase/service/ActorService.java @@ -66,7 +66,9 @@ public List findActorsByPage(Integer page, String bearerStr) { public Optional findActorById(Long id, String bearerStr) { final User user = this.auds.getCurrentUser(bearerStr); Optional 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; } diff --git a/backend/src/test/java/ch/xxx/moviemanager/adapter/controller/ActorControllerTest.java b/backend/src/test/java/ch/xxx/moviemanager/adapter/controller/ActorControllerTest.java index 58beeec..32cc123 100644 --- a/backend/src/test/java/ch/xxx/moviemanager/adapter/controller/ActorControllerTest.java +++ b/backend/src/test/java/ch/xxx/moviemanager/adapter/controller/ActorControllerTest.java @@ -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) diff --git a/backend/src/test/java/ch/xxx/moviemanager/usecase/service/ActorServiceTest.java b/backend/src/test/java/ch/xxx/moviemanager/usecase/service/ActorServiceTest.java index 59dd5bf..e6e1fad 100644 --- a/backend/src/test/java/ch/xxx/moviemanager/usecase/service/ActorServiceTest.java +++ b/backend/src/test/java/ch/xxx/moviemanager/usecase/service/ActorServiceTest.java @@ -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; @@ -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 actorOpt = actorService.findActorById(1L, "YYY");