-
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 #56 from neuefische/ISSUE-55
ISSUE-55: Add multiuser support.
- Loading branch information
Showing
17 changed files
with
466 additions
and
103 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
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
35 changes: 35 additions & 0 deletions
35
backend/src/main/java/com/example/backend/controller/RatingController.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,35 @@ | ||
package com.example.backend.controller; | ||
|
||
import com.example.backend.dto.CreateRatingRequest; | ||
import com.example.backend.dto.RatingResponse; | ||
import com.example.backend.model.Rating; | ||
import com.example.backend.service.RatingService; | ||
import lombok.AllArgsConstructor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
@RequestMapping("/api/rating") | ||
public class RatingController { | ||
|
||
private final RatingService ratingService; | ||
|
||
@PostMapping | ||
public void save(@RequestBody @NotNull CreateRatingRequest request, @AuthenticationPrincipal OAuth2User user) { | ||
String login = user.getAttributes().get("login").toString(); | ||
Rating rating = request.toRating(login); | ||
|
||
ratingService.save(rating); | ||
} | ||
|
||
@GetMapping("/{movieId}") | ||
public RatingResponse get(@AuthenticationPrincipal OAuth2User user, @PathVariable String movieId) { | ||
String login = user.getAttributes().get("login").toString(); | ||
Rating rating = ratingService.get(login, movieId); | ||
|
||
return RatingResponse.from(rating); | ||
} | ||
} |
11 changes: 2 additions & 9 deletions
11
backend/src/main/java/com/example/backend/dto/CreateMovieRequest.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,19 +1,12 @@ | ||
package com.example.backend.dto; | ||
|
||
import com.example.backend.model.Actor; | ||
import com.example.backend.model.Movie; | ||
import jakarta.validation.constraints.Max; | ||
import jakarta.validation.constraints.Min; | ||
import lombok.NonNull; | ||
|
||
|
||
public record CreateMovieRequest( | ||
@NonNull String name, | ||
boolean isWatched, | ||
@Min(Movie.MIN_RATING) @Max(Movie.MAX_RATING) Integer rating | ||
|
||
@NonNull String name | ||
) { | ||
public Movie toMovie() { | ||
return Movie.builder().name(name).isWatched(isWatched).rating(rating).build(); | ||
return Movie.builder().name(name).build(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/com/example/backend/dto/CreateRatingRequest.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,18 @@ | ||
package com.example.backend.dto; | ||
|
||
import com.example.backend.model.Movie; | ||
import com.example.backend.model.Rating; | ||
import jakarta.validation.constraints.Max; | ||
import jakarta.validation.constraints.Min; | ||
import lombok.NonNull; | ||
|
||
|
||
public record CreateRatingRequest( | ||
@NonNull String movieId, | ||
boolean isWatched, | ||
@Min(Rating.MIN_RATING) @Max(Rating.MAX_RATING) Integer rating | ||
) { | ||
public Rating toRating(String userId) { | ||
return Rating.builder().userId(userId).movieId(movieId).isWatched(isWatched).rating(rating).build(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/com/example/backend/dto/MovieRatingResponse.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,18 @@ | ||
package com.example.backend.dto; | ||
|
||
import com.example.backend.model.Movie; | ||
import com.example.backend.model.Rating; | ||
import lombok.Builder; | ||
import lombok.NonNull; | ||
|
||
@Builder | ||
public record MovieRatingResponse(@NonNull String id, @NonNull String movieName, Integer rating, boolean isWatched) { | ||
public static MovieRatingResponse from(Rating rating, Movie movie) { | ||
return MovieRatingResponse.builder() | ||
.id(movie.getId()) | ||
.movieName(movie.getName()) | ||
.rating(rating.getRating()) | ||
.isWatched(rating.isWatched()) | ||
.build(); | ||
} | ||
} |
6 changes: 1 addition & 5 deletions
6
backend/src/main/java/com/example/backend/dto/MovieResponse.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,19 +1,15 @@ | ||
package com.example.backend.dto; | ||
|
||
import com.example.backend.model.Movie; | ||
import jakarta.annotation.Nullable; | ||
import lombok.Builder; | ||
import lombok.NonNull; | ||
|
||
@Builder | ||
public record MovieResponse(@NonNull String id, @NonNull String name, boolean isWatched, | ||
@Nullable Integer rating) { | ||
public record MovieResponse(@NonNull String id, @NonNull String name) { | ||
public static MovieResponse from(Movie movie) { | ||
return MovieResponse.builder() | ||
.id(movie.getId()) | ||
.name(movie.getName()) | ||
.isWatched(movie.isWatched()) | ||
.rating(movie.getRating()) | ||
.build(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/com/example/backend/dto/RatingResponse.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,19 @@ | ||
package com.example.backend.dto; | ||
|
||
import com.example.backend.model.Rating; | ||
import jakarta.annotation.Nullable; | ||
import lombok.Builder; | ||
import lombok.NonNull; | ||
|
||
@Builder | ||
public record RatingResponse(String userId, String movieId, Integer rating, | ||
boolean isWatched) { | ||
public static RatingResponse from(Rating rating) { | ||
return RatingResponse.builder() | ||
.userId(rating.getUserId()) | ||
.movieId(rating.getMovieId()) | ||
.rating(rating.getRating()) | ||
.isWatched(rating.isWatched()) | ||
.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
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/Rating.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 lombok.*; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@With | ||
@Data | ||
@Document("Rating") | ||
public class Rating { | ||
public static final int MIN_RATING = 1; | ||
public static final int MAX_RATING = 10; | ||
|
||
@Id | ||
private String id; | ||
|
||
private String movieId; | ||
|
||
private String userId; | ||
|
||
private Integer rating; | ||
|
||
private boolean isWatched; | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/com/example/backend/model/RatingRepository.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,18 @@ | ||
package com.example.backend.model; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface RatingRepository extends MongoRepository<Rating, String> { | ||
Optional<Rating> findFirstByUserIdAndMovieId(String userId, String movieId); | ||
|
||
List<Rating> findAllByUserIdAndIsWatchedIsFalse(String userId); | ||
|
||
List<Rating> findAllByUserIdAndIsWatchedIsTrue(String userId); | ||
|
||
List<Rating> findAllByMovieIdAndUserId(List<String> movieId, String userId); | ||
} |
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
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/com/example/backend/service/RatingService.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,25 @@ | ||
package com.example.backend.service; | ||
|
||
import com.example.backend.model.Rating; | ||
import com.example.backend.model.RatingRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RatingService { | ||
private final RatingRepository ratingRepository; | ||
|
||
public void save(Rating rating) { | ||
ratingRepository | ||
.findFirstByUserIdAndMovieId(rating.getUserId(), rating.getMovieId()) | ||
.ifPresentOrElse( | ||
(entity) -> ratingRepository.save(entity.withRating(rating.getRating()).withWatched(rating.isWatched())), | ||
() -> ratingRepository.save(rating) | ||
); | ||
} | ||
|
||
public Rating get(String userId, String movieId) { | ||
return ratingRepository.findFirstByUserIdAndMovieId(userId, movieId).orElseThrow(); | ||
} | ||
} |
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.