Skip to content

Commit 0f908b8

Browse files
see all users
store game now it's a post method
1 parent bc83bae commit 0f908b8

1 file changed

Lines changed: 165 additions & 182 deletions

File tree

  • src/main/java/com/apporelbotna/gameserver/persistencewsclient

src/main/java/com/apporelbotna/gameserver/persistencewsclient/GameDAO.java

Lines changed: 165 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -20,186 +20,169 @@
2020

2121
public class GameDAO
2222
{
23-
// Stucky server
24-
public static final String SERVER_URL = "http://172.16.2.94:8082/";
25-
// public static final String SERVER_URL = "http://localhost:8082/";
26-
27-
RestTemplate restTemplate = new RestTemplate();
28-
29-
public GameDAO()
30-
{
31-
// Empty constructor
32-
}
33-
34-
public boolean finishMatch(Match... matches)
35-
{
36-
for ( Match match : matches )
37-
{
38-
ResponseEntity< ? > response = restTemplate.postForEntity( SERVER_URL + "/match", match, null );
39-
if ( !response.getStatusCode().equals( HttpStatus.CREATED ) )
40-
{
41-
return false;
42-
}
43-
}
44-
return true;
45-
}
46-
47-
public boolean isUserLoggeable(String email, String tokenString)
48-
{
49-
UserWrapper wrapper = new UserWrapper( new User( email ), new Token( tokenString ) );
50-
51-
ResponseEntity< ? > response = restTemplate.postForEntity( SERVER_URL + "/auth", wrapper, null );
52-
return( response.getStatusCode().equals( HttpStatus.OK ) );
53-
}
54-
55-
public User validateUser(String email, String tokenString)
56-
{
57-
return isUserLoggeable( email, tokenString ) ? getUserInformation( email ) : null;
58-
}
59-
60-
public Token login(String email, String password)
61-
{
62-
return restTemplate.getForObject( SERVER_URL + "/login/" + email + "/" + password, Token.class );
63-
}
64-
65-
// saca todos los juegos de un usuario
66-
public List< Game > findAllGamesByUser(User user)
67-
{
68-
String userEmail = user.getId();
69-
ResponseEntity< List< Game > > responseWS
70-
= restTemplate.exchange( SERVER_URL + "/user/game/" + userEmail,
71-
HttpMethod.GET,
72-
null,
73-
new ParameterizedTypeReference< List< Game > >()
74-
{ //
75-
} );
76-
return responseWS.getBody();
77-
}
78-
79-
public boolean createUser(User user, String password)
80-
{
81-
RegisterUser userToRegister = new RegisterUser( user, password );
82-
83-
ResponseEntity< ? > response = restTemplate.postForEntity( SERVER_URL + "/user/", userToRegister, null );
84-
85-
return response.getStatusCode().equals( HttpStatus.CREATED );
86-
}
87-
88-
public boolean createGame(Game game)
89-
{
90-
ResponseEntity< ? > response = restTemplate.postForEntity( SERVER_URL + "/game/", game, null );
91-
92-
return response.getStatusCode().equals( HttpStatus.CREATED );
93-
}
94-
95-
public void storeGameToUser(User user, int idGame)
96-
{
97-
Map< String , String > params = new HashMap<>();
98-
params.put( "idGame", String.valueOf( idGame ) );
99-
100-
restTemplate.put( SERVER_URL + "/game/{idGame}", user, params );
101-
}
102-
103-
public void updateUserBasicInformatio(User user)
104-
{
105-
Map< String , String > params = new HashMap<>();
106-
107-
restTemplate.put( SERVER_URL + "/user", user, params );
108-
}
109-
110-
public User getUserInformation(String email)
111-
{
112-
return restTemplate.getForObject( SERVER_URL + "/user/" + email, User.class );
113-
}
114-
115-
/**
116-
*
117-
* @param email
118-
* @param gameID
119-
* @return time in miliseconds if player never played to this game result will
120-
* be <b>0.0</b>
121-
*/
122-
public float gameTimePlayedByGame(String email, int gameID)
123-
{
124-
return restTemplate.getForObject( SERVER_URL + "/user/" + email + "/game/" + gameID + "/time/", Float.class );
125-
}
126-
127-
public List< RankingPointsTO > getRankingPointsByGameAndUser(int gameId)
128-
{
129-
ResponseEntity< List< RankingPointsTO > > responseWS
130-
= restTemplate.exchange( SERVER_URL + "/ranking/" + gameId,
131-
HttpMethod.GET,
132-
null,
133-
new ParameterizedTypeReference< List< RankingPointsTO > >()
134-
{ //
135-
} );
136-
return responseWS.getBody();
137-
}
138-
139-
public List< Game > getAllGames()
140-
{
141-
ResponseEntity< List< Game > > responseWS
142-
= restTemplate.exchange( SERVER_URL + "/game/",
143-
HttpMethod.GET,
144-
null,
145-
new ParameterizedTypeReference< List< Game > >()
146-
{ //
147-
} );
148-
return responseWS.getBody();
149-
}
150-
151-
public List< String > getAllGenre()
152-
{
153-
ResponseEntity< List< String > > responseWS
154-
= restTemplate.exchange( SERVER_URL + "/genre/",
155-
HttpMethod.GET,
156-
null,
157-
new ParameterizedTypeReference< List< String > >()
158-
{ //
159-
} );
160-
return responseWS.getBody();
161-
}
162-
163-
public List< User > findUsersLikeName(String userInput)
164-
{
165-
ResponseEntity< List< User > > responseWS
166-
= restTemplate.exchange( SERVER_URL + "/user/" + userInput + "/name/",
167-
HttpMethod.GET,
168-
null,
169-
new ParameterizedTypeReference< List< User > >()
170-
{ //
171-
} );
172-
return responseWS.getBody();
173-
}
174-
175-
public List< User > findUsersLikeEmail(String userInput)
176-
{
177-
ResponseEntity< List< User > > responseWS
178-
= restTemplate.exchange( SERVER_URL + "/user/" + userInput + "/email/",
179-
HttpMethod.GET,
180-
null,
181-
new ParameterizedTypeReference< List< User > >()
182-
{ //
183-
} );
184-
return responseWS.getBody();
185-
}
186-
187-
public List< Game > findGamesLikeName(String userInput)
188-
{
189-
ResponseEntity< List< Game > > responseWS
190-
= restTemplate.exchange( SERVER_URL + "/game/" + userInput + "/name/",
191-
HttpMethod.GET,
192-
null,
193-
new ParameterizedTypeReference< List< Game > >()
194-
{ //
195-
} );
196-
return responseWS.getBody();
197-
}
198-
199-
public static void main( String[] args )
200-
{
201-
GameDAO dao = new GameDAO();
202-
203-
dao.storeGameToUser( new User( "jan@jan.com" ) , 6 );
204-
}
23+
// Stucky server
24+
public static final String SERVER_URL = "http://172.16.2.94:8082/";
25+
//public static final String SERVER_URL = "http://localhost:8082/";
26+
27+
RestTemplate restTemplate = new RestTemplate();
28+
29+
public GameDAO()
30+
{
31+
// Empty constructor
32+
}
33+
34+
public boolean finishMatch(Match... matches)
35+
{
36+
for (Match match : matches)
37+
{
38+
ResponseEntity<?> response = restTemplate.postForEntity(SERVER_URL + "/match", match, null);
39+
if (!response.getStatusCode().equals(HttpStatus.CREATED))
40+
{
41+
return false;
42+
}
43+
}
44+
return true;
45+
}
46+
47+
public boolean isUserLoggeable(String email, String tokenString)
48+
{
49+
UserWrapper wrapper = new UserWrapper(new User(email), new Token(tokenString));
50+
51+
ResponseEntity<?> response = restTemplate.postForEntity(SERVER_URL + "/auth", wrapper, null);
52+
return (response.getStatusCode().equals(HttpStatus.OK));
53+
}
54+
55+
public User validateUser(String email, String tokenString)
56+
{
57+
return isUserLoggeable(email, tokenString) ? getUserInformation(email) : null;
58+
}
59+
60+
public Token login(String email, String password)
61+
{
62+
return restTemplate.getForObject(SERVER_URL + "/login/" + email + "/" + password, Token.class);
63+
}
64+
65+
// saca todos los juegos de un usuario
66+
public List<Game> findAllGamesByUser(User user)
67+
{
68+
String userEmail = user.getId();
69+
ResponseEntity<List<Game>> responseWS = restTemplate.exchange(SERVER_URL + "/user/game/" + userEmail,
70+
HttpMethod.GET, null, new ParameterizedTypeReference<List<Game>>()
71+
{ //
72+
});
73+
return responseWS.getBody();
74+
}
75+
76+
// saca todos los juegos de un usuario
77+
public List<User> findAllUser()
78+
{
79+
ResponseEntity<List<User>> responseWS = restTemplate.exchange(SERVER_URL + "/user/",
80+
HttpMethod.GET, null, new ParameterizedTypeReference<List<User>>()
81+
{ //
82+
});
83+
return responseWS.getBody();
84+
}
85+
86+
public boolean createUser(User user, String password)
87+
{
88+
RegisterUser userToRegister = new RegisterUser(user, password);
89+
90+
ResponseEntity<?> response = restTemplate.postForEntity(SERVER_URL + "/user/", userToRegister, null);
91+
92+
return response.getStatusCode().equals(HttpStatus.CREATED);
93+
}
94+
95+
public boolean createGame(Game game)
96+
{
97+
ResponseEntity<?> response = restTemplate.postForEntity(SERVER_URL + "/game/", game, null);
98+
99+
return response.getStatusCode().equals(HttpStatus.CREATED);
100+
}
101+
102+
public boolean storeGameToUser(User user, int idGame)
103+
{
104+
Map<String, String> params = new HashMap<>();
105+
params.put("idGame", String.valueOf(idGame));
106+
107+
ResponseEntity<?> response = restTemplate.postForEntity(SERVER_URL + "/game/{idGame}", user, null, params);
108+
return response.getStatusCode().equals(HttpStatus.OK);
109+
}
110+
111+
public void updateUserBasicInformatio(User user)
112+
{
113+
Map<String, String> params = new HashMap<>();
114+
115+
restTemplate.put(SERVER_URL + "/user", user, params);
116+
}
117+
118+
public User getUserInformation(String email)
119+
{
120+
return restTemplate.getForObject(SERVER_URL + "/user/" + email, User.class);
121+
}
122+
123+
/**
124+
*
125+
* @param email
126+
* @param gameID
127+
* @return time in miliseconds if player never played to this game result
128+
* will be <b>0.0</b>
129+
*/
130+
public float gameTimePlayedByGame(String email, int gameID)
131+
{
132+
return restTemplate.getForObject(SERVER_URL + "/user/" + email + "/game/" + gameID + "/time/", Float.class);
133+
}
134+
135+
public List<RankingPointsTO> getRankingPointsByGameAndUser(int gameId)
136+
{
137+
ResponseEntity<List<RankingPointsTO>> responseWS = restTemplate.exchange(SERVER_URL + "/ranking/" + gameId,
138+
HttpMethod.GET, null, new ParameterizedTypeReference<List<RankingPointsTO>>()
139+
{ //
140+
});
141+
return responseWS.getBody();
142+
}
143+
144+
public List<Game> getAllGames()
145+
{
146+
ResponseEntity<List<Game>> responseWS = restTemplate.exchange(SERVER_URL + "/game/", HttpMethod.GET, null,
147+
new ParameterizedTypeReference<List<Game>>()
148+
{ //
149+
});
150+
return responseWS.getBody();
151+
}
152+
153+
public List<String> getAllGenre()
154+
{
155+
ResponseEntity<List<String>> responseWS = restTemplate.exchange(SERVER_URL + "/genre/", HttpMethod.GET, null,
156+
new ParameterizedTypeReference<List<String>>()
157+
{ //
158+
});
159+
return responseWS.getBody();
160+
}
161+
162+
public List<User> findUsersLikeName(String userInput)
163+
{
164+
ResponseEntity<List<User>> responseWS = restTemplate.exchange(SERVER_URL + "/user/" + userInput + "/name/",
165+
HttpMethod.GET, null, new ParameterizedTypeReference<List<User>>()
166+
{ //
167+
});
168+
return responseWS.getBody();
169+
}
170+
171+
public List<User> findUsersLikeEmail(String userInput)
172+
{
173+
ResponseEntity<List<User>> responseWS = restTemplate.exchange(SERVER_URL + "/user/" + userInput + "/email/",
174+
HttpMethod.GET, null, new ParameterizedTypeReference<List<User>>()
175+
{ //
176+
});
177+
return responseWS.getBody();
178+
}
179+
180+
public List<Game> findGamesLikeName(String userInput)
181+
{
182+
ResponseEntity<List<Game>> responseWS = restTemplate.exchange(SERVER_URL + "/game/" + userInput + "/name/",
183+
HttpMethod.GET, null, new ParameterizedTypeReference<List<Game>>()
184+
{ //
185+
});
186+
return responseWS.getBody();
187+
}
205188
}

0 commit comments

Comments
 (0)