-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from neuefische/ISSUE-12
ISSUE-12: Create a POST create new relation actor-movie-relation endpoint and cover it with tests.
- Loading branch information
Showing
18 changed files
with
388 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/com/example/backend/controller/MovieActorController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.example.backend.controller; | ||
|
||
import com.example.backend.dto.ActorResponse; | ||
import com.example.backend.dto.MovieActorRequest; | ||
import com.example.backend.service.MovieActorService; | ||
import lombok.AllArgsConstructor; | ||
import lombok.NonNull; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.servlet.resource.NoResourceFoundException; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
@RequestMapping("/api/movie-actor") | ||
public class MovieActorController { | ||
|
||
private final MovieActorService movieActorService; | ||
|
||
@PostMapping() | ||
public void addActorById(@RequestBody @NonNull MovieActorRequest movieActorRequest) { | ||
movieActorService.addActor(movieActorRequest.movieId(), movieActorRequest.actorId()); | ||
} | ||
|
||
@GetMapping("/{movieId}") | ||
public List<ActorResponse> getActorsByMovieId(@PathVariable @NonNull Long movieId) { | ||
return movieActorService.getActorsByMovieId(movieId).stream().map(ActorResponse::from).toList(); | ||
} | ||
|
||
/** | ||
* Prevents the frontend fallback response from being returned if the request path is invalid. | ||
* | ||
* @throws NoResourceFoundException | ||
*/ | ||
@GetMapping("/") | ||
public void getByMovieIdInvalid() throws NoResourceFoundException { | ||
throw new NoResourceFoundException(HttpMethod.GET, "/api/actor-movie/"); | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/com/example/backend/dto/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.example.backend.dto; | ||
|
||
public record ErrorResponse(String message) { | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/com/example/backend/dto/MovieActorRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.backend.dto; | ||
|
||
import lombok.NonNull; | ||
|
||
public record MovieActorRequest(@NonNull Long movieId, @NonNull Long actorId) { | ||
} |
47 changes: 47 additions & 0 deletions
47
backend/src/main/java/com/example/backend/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.example.backend.exception; | ||
|
||
import com.example.backend.dto.ErrorResponse; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.servlet.resource.NoResourceFoundException; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.logging.Logger; | ||
|
||
@ControllerAdvice | ||
@Log4j2 | ||
public class GlobalExceptionHandler { | ||
private static final String GENERIC_ERROR_MESSAGE = "Something went wrong"; | ||
|
||
@ExceptionHandler | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public ErrorResponse handleGlobalException(NoSuchElementException exception) { | ||
log.info(exception.getMessage()); | ||
return new ErrorResponse(exception.getMessage()); | ||
} | ||
|
||
@ExceptionHandler | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public ErrorResponse handleGlobalException(HttpMessageNotReadableException exception) { | ||
log.info(exception.getMessage()); | ||
return new ErrorResponse(exception.getMessage()); | ||
} | ||
|
||
@ExceptionHandler | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
public ErrorResponse handleGlobalException(NoResourceFoundException exception) { | ||
log.info(exception.getMessage()); | ||
return new ErrorResponse(exception.getMessage()); | ||
} | ||
|
||
@ExceptionHandler | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public ErrorResponse handleGlobalException(Exception exception) { | ||
log.error(exception); | ||
return new ErrorResponse(GENERIC_ERROR_MESSAGE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
backend/src/main/java/com/example/backend/model/ActorRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
package com.example.backend.model; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface ActorRepository extends JpaRepository<Actor, Long> { | ||
List<Actor> findByNameStartingWith(String prefix); | ||
|
||
@Query("SELECT a FROM Actor a JOIN a.movies m WHERE m.id = :movieId") | ||
List<Actor> findByMovieId(@Param("movieId") Long movieId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
backend/src/main/java/com/example/backend/model/MovieActorRelation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.backend.model; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@With | ||
@Data | ||
@Table( | ||
uniqueConstraints = { | ||
@UniqueConstraint(columnNames = {"actor_id", "movie_id"}) | ||
} | ||
) | ||
public class MovieActorRelation { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH}) | ||
private Actor actor; | ||
|
||
@ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH}) | ||
private Movie movie; | ||
} |
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/com/example/backend/model/MovieActorRelationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.backend.model; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface MovieActorRelationRepository extends JpaRepository<MovieActorRelation, Long> { | ||
List<MovieActorRelation> findByMovieId(Long movieId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
backend/src/main/java/com/example/backend/service/MovieActorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.example.backend.service; | ||
|
||
import com.example.backend.model.*; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MovieActorService { | ||
private final MovieActorRelationRepository movieActorRelationRepository; | ||
|
||
private final ActorRepository actorRepository; | ||
|
||
private final MovieRepository movieRepository; | ||
|
||
public List<Actor> getActorsByMovieId(Long movieId) { | ||
return movieActorRelationRepository.findByMovieId(movieId) | ||
.stream() | ||
.map(MovieActorRelation::getActor) | ||
.toList(); | ||
} | ||
|
||
public void addActor(Long movieId, Long actorId) { | ||
Movie movie = movieRepository.findById(movieId).orElseThrow(); | ||
Actor actor = actorRepository.findById(actorId).orElseThrow(); | ||
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actor).movie(movie).actor(actor).build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.