Skip to content

Commit

Permalink
Refactoring requests module
Browse files Browse the repository at this point in the history
  • Loading branch information
gcaraciolo committed Apr 8, 2019
1 parent 6ffe0df commit a6d8bc4
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 58 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

[*.java]
indent_style = space
indent_size = 4
continuation_indent_size = 8
9 changes: 5 additions & 4 deletions src/main/java/br/unicap/cardgame/app/ResponseAllowCORS.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
public class ResponseAllowCORS implements ContainerResponseFilter {

@Override
public void filter(final ContainerRequestContext requestContext,
final ContainerResponseContext cres) throws IOException {

allowCORS(cres);
public void filter(
final ContainerRequestContext requestContext,
final ContainerResponseContext cres
) throws IOException {
allowCORS(cres);
}

private void allowCORS(ContainerResponseContext cres) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import br.unicap.cardgame.model.Cards;

public class CardsController {
public int getCorretAnswer(Cards card) {

public int getCorretAnswer(Cards card) {
return card.getQuestionId().getCorrectAnswerId().getId();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
import javax.ejb.EJB;
import javax.ejb.Stateless;


@Stateless
public class CharsController {

@EJB
private CharsDAO charDAO;

public Chars getPlayerChar(String username) {
return charDAO.getPlayerChar(username);
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/br/unicap/cardgame/controller/DecksController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@

@Stateless
public class DecksController {

@EJB
private DecksDAO decksDAO;

public List<Cards> randonCards(Users u) {
List<Cards> cards = decksDAO.getUserCards(u);
if(cards.size() < 1) {
if (cards.size() < 1) {
return new ArrayList<Cards>();
}
List<Cards> randon = new ArrayList<Cards>();
for(int i = 0; i < 3; i++) {
for (int i = 0; i < 3; i++) {
randon.add(cards.get(Utils.getRandomNumber(0, cards.size())));
}
return randon;
}

public Cards randonCard(Users u) {
List<Cards> cards = decksDAO.getUserCards(u);
if(cards.size() < 1) {
if (cards.size() < 1) {
return null;
}
return cards.get(Utils.getRandomNumber(0, cards.size()));
return cards.get(Utils.getRandomNumber(0, cards.size()));
}

}
64 changes: 31 additions & 33 deletions src/main/java/br/unicap/cardgame/controller/UsersController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,37 @@

@Stateless
public class UsersController {

@EJB
private UsersDAO usersDAO;

public CardGameResponseToken authenticate(String username, String password) throws Exception {

Users u = usersDAO.getUserByUsername(username);

if(u == null || !verifyPassword(u, password)) {
throw new Exception("password invalido");
}
Player p = new Player(u.getId(), u.getUsername());
String token = Jwts.builder()
.setSubject(p.toString())
.signWith(SignatureAlgorithm.HS512, Utils.APP_CLIENT_SECRET)
.compact();

return new CardGameResponseToken(true, token);
}

private boolean verifyPassword(Users user, String password) {
return user.getPassword().equals(password);
}

public Users getUserByUsername(String username) {
return usersDAO.getUserByUsername(username);

@EJB
private UsersDAO usersDAO;

public CardGameResponseToken authenticate(String username, String password) throws Exception {

Users u = usersDAO.getUserByUsername(username);

if (u == null || !verifyPassword(u, password)) {
throw new Exception("password invalido");
}

public CardGameResponse changeUserChar(int user_id, int char_id) {
boolean update = usersDAO.changeUserChar(user_id, char_id);
if(update) {
return new CardGameResponse(true, 2000, "Char alterado com sucesso");
}
return new CardGameResponse(true, 2000, "Falha ao alterar char!");
Player p = new Player(u.getId(), u.getUsername());
String token = Jwts.builder().setSubject(p.toString())
.signWith(SignatureAlgorithm.HS512, Utils.APP_CLIENT_SECRET).compact();

return new CardGameResponseToken(true, token);
}

private boolean verifyPassword(Users user, String password) {
return user.getPassword().equals(password);
}

public Users getUserByUsername(String username) {
return usersDAO.getUserByUsername(username);
}

public CardGameResponse changeUserChar(int user_id, int char_id) {
boolean update = usersDAO.changeUserChar(user_id, char_id);
if (update) {
return new CardGameResponse(true, 2000, "Char alterado com sucesso");
}
return new CardGameResponse(true, 2000, "Falha ao alterar char!");
}
}
3 changes: 1 addition & 2 deletions src/main/java/br/unicap/cardgame/ws/ChangeCharGameWS.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import br.unicap.cardgame.app.Secured;
import br.unicap.cardgame.controller.UsersController;
import br.unicap.cardgame.jax.bean.ChangeCharJAXBean;
import br.unicap.cardgame.model.Player;
import br.unicap.cardgame.ws.request.ChangeCharJAXBean;
import br.unicap.cardgame.util.Utils;
import br.unicap.cardgame.ws.response.CardGameResponse;
import javax.ejb.EJB;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/br/unicap/cardgame/ws/LoginGameWS.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.unicap.cardgame.ws;

import br.unicap.cardgame.controller.UsersController;
import br.unicap.cardgame.jax.bean.LoginGameJAXBean;
import br.unicap.cardgame.ws.request.LoginGameJAXBean;
import br.unicap.cardgame.ws.response.CardGameResponseToken;
import javax.ejb.EJB;
import javax.ws.rs.Consumes;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/br/unicap/cardgame/ws/MoveGameWS.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.unicap.cardgame.ws;

import br.unicap.cardgame.app.Secured;
import br.unicap.cardgame.jax.bean.MoveGameJAXBean;
import br.unicap.cardgame.ws.request.MoveGameJAXBean;
import br.unicap.cardgame.engine.BattleFieldController;
import br.unicap.cardgame.model.Player;
import br.unicap.cardgame.util.Utils;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/br/unicap/cardgame/ws/PlayGameWS.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import br.unicap.cardgame.app.Secured;
import br.unicap.cardgame.engine.BattleFieldController;
import br.unicap.cardgame.jax.bean.PlayGameJAXBean;
import br.unicap.cardgame.ws.request.PlayGameJAXBean;
import br.unicap.cardgame.model.Player;
import br.unicap.cardgame.util.Utils;
import br.unicap.cardgame.ws.response.CardGameResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.unicap.cardgame.jax.bean;
package br.unicap.cardgame.ws.request;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.unicap.cardgame.jax.bean;
package br.unicap.cardgame.ws.request;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.unicap.cardgame.jax.bean;
package br.unicap.cardgame.ws.request;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.unicap.cardgame.jax.bean;
package br.unicap.cardgame.ws.request;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
Expand Down

0 comments on commit a6d8bc4

Please sign in to comment.