-
Notifications
You must be signed in to change notification settings - Fork 14
/
FavoriteService.java
120 lines (104 loc) · 4.49 KB
/
FavoriteService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package neoflix.services;
import neoflix.AppUtils;
import neoflix.Params;
import org.neo4j.driver.Driver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FavoriteService {
private final Driver driver;
private final List<Map<String,Object>> popular;
private final List<Map<String,Object>> users;
private final Map<String,List<Map<String,Object>>> userFavorites = new HashMap<>();
/**
* The constructor expects an instance of the Neo4j Driver, which will be
* used to interact with Neo4j.
*
* @param driver
*/
public FavoriteService(Driver driver) {
this.driver = driver;
this.popular = AppUtils.loadFixtureList("popular");
this.users = AppUtils.loadFixtureList("users");
}
/**
* This method should retrieve a list of movies that have an incoming :HAS_FAVORITE
* relationship from a User node with the supplied `userId`.
*
* Results should be ordered by the `sort` parameter, and in the direction specified
* in the `order` parameter.
* Results should be limited to the number passed as `limit`.
* The `skip` variable should be used to skip a certain number of rows.
*
* @param userId The unique ID of the user
* @param params Query params for pagination and sorting
* @return List<Movie> An list of Movie objects
*/
// tag::all[]
public List<Map<String, Object>> all(String userId, Params params) {
// TODO: Open a new session
// TODO: Retrieve a list of movies favorited by the user
// TODO: Close session
return AppUtils.process(userFavorites.getOrDefault(userId, List.of()),params);
}
// end::all[]
/**
* This method should create a `:HAS_FAVORITE` relationship between
* the User and Movie ID nodes provided.
*
* If either the user or movie cannot be found, a `NotFoundError` should be thrown.
*
* @param userId The unique ID for the User node
* @param movieId The unique tmdbId for the Movie node
* @return Map<String,Object></String,Object> The updated movie record with `favorite` set to true
*/
// tag::add[]
public Map<String,Object> add(String userId, String movieId) {
// TODO: Open a new Session
// TODO: Create HAS_FAVORITE relationship within a Write Transaction
// TODO: Close the session
// TODO: Return movie details and `favorite` property
var foundMovie = popular.stream().filter(m -> movieId.equals(m.get("tmdbId"))).findAny();
if (users.stream().anyMatch(u -> u.get("userId").equals(userId)) || foundMovie.isEmpty()) {
throw new RuntimeException("Couldn't create a favorite relationship for User %s and Movie %s".formatted(userId, movieId));
}
var movie = foundMovie.get();
var favorites = userFavorites.computeIfAbsent(userId, (k) -> new ArrayList<>());
if (!favorites.contains(movie)) {
favorites.add(movie);
}
var copy = new HashMap<>(movie);
copy.put("favorite", true);
return copy;
}
// end::add[]
/*
*This method should remove the `:HAS_FAVORITE` relationship between
* the User and Movie ID nodes provided.
* If either the user, movie or the relationship between them cannot be found,
* a `NotFoundError` should be thrown.
* @param userId The unique ID for the User node
* @param movieId The unique tmdbId for the Movie node
* @return Map<String,Object></String,Object> The updated movie record with `favorite` set to true
*/
// tag::remove[]
public Map<String,Object> remove(String userId, String movieId) {
// TODO: Open a new Session
// TODO: Delete the HAS_FAVORITE relationship within a Write Transaction
// TODO: Close the session
// TODO: Return movie details and `favorite` property
if (users.stream().anyMatch(u -> u.get("userId").equals(userId))) {
throw new RuntimeException("Couldn't remove a favorite relationship for User %s and Movie %s".formatted(userId, movieId));
}
var movie = popular.stream().filter(m -> movieId.equals(m.get("tmdbId"))).findAny().get();
var favorites = userFavorites.computeIfAbsent(userId, (k) -> new ArrayList<>());
if (favorites.contains(movie)) {
favorites.remove(movie);
}
var copy = new HashMap<>(movie);
copy.put("favorite", false);
return copy;
}
// end::remove[]
}