Skip to content

Commit 11c79ea

Browse files
author
JEstebanC
committed
Small modifications in the bill filter
1 parent e8d3321 commit 11c79ea

22 files changed

+276
-82
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<artifactId>spring-boot-starter-web</artifactId>
3232
</dependency>
3333

34+
3435
<dependency>
3536
<groupId>org.postgresql</groupId>
3637
<artifactId>postgresql</artifactId>

src/main/java/JEstebanC/FastFoodApp/controller/BillController.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import JEstebanC.FastFoodApp.dto.UserBillOrdersDTO;
3131
import JEstebanC.FastFoodApp.enumeration.StatusBill;
32+
import JEstebanC.FastFoodApp.enumeration.StatusOrder;
3233
import JEstebanC.FastFoodApp.model.Bill;
3334
import JEstebanC.FastFoodApp.model.Response;
3435
import JEstebanC.FastFoodApp.model.User;
@@ -66,8 +67,8 @@ public ResponseEntity<Response> saveBill(@RequestBody @Valid Bill bill) {
6667
@GetMapping(value = "/list")
6768
public ResponseEntity<Response> listByParams(@Param(value = "idBill") Long idBill,
6869
@Param(value = "idUser") Long idUser, @Param(value = "statusBill") StatusBill statusBill,
69-
@Param(value = "startDate") String startDate, @Param(value = "endDate") String endDate,
70-
HttpServletRequest request) {
70+
@Param(value = "statusOrder") StatusOrder statusOrder, @Param(value = "startDate") String startDate,
71+
@Param(value = "endDate") String endDate, HttpServletRequest request) {
7172
if (request.isUserInRole("ROLE_CLIENT")) {
7273
log.info("ENTRA CLIENTE");
7374
String authorizationHeader = request.getHeader(AUTHORIZATION);
@@ -77,7 +78,6 @@ public ResponseEntity<Response> listByParams(@Param(value = "idBill") Long idBil
7778
Algorithm algorithm = Algorithm.HMAC256(OperationUtil.keyValue().getBytes());
7879
JWTVerifier verifier = JWT.require(algorithm).build();
7980
DecodedJWT decodeJWT = verifier.verify(token);
80-
8181

8282
if (idBill != null) {
8383
log.info("ENTRA CLIENTE BILL");
@@ -87,13 +87,13 @@ public ResponseEntity<Response> listByParams(@Param(value = "idBill") Long idBil
8787
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
8888
.data(Map.of("bill", serviceImp.findByIdBill(idBill))).message("bill")
8989
.status(HttpStatus.OK).statusCode(HttpStatus.OK.value()).build());
90-
}else {
90+
} else {
9191
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
9292
.message(("Error seeing the bill, you have not the permissions"))
9393
.status(HttpStatus.BAD_REQUEST).statusCode(HttpStatus.BAD_REQUEST.value()).build());
9494
}
9595
}
96-
if (idUser!=null) {
96+
if (idUser != null) {
9797
User userOld = serviceImpUser.findById(idUser);
9898
if (userOld.getUsername().equals(decodeJWT.getSubject().toString())) {
9999
log.info("ENTRA CLIENTE SIN BILL");
@@ -108,12 +108,11 @@ public ResponseEntity<Response> listByParams(@Param(value = "idBill") Long idBil
108108
.message(("Error seeing the bill, you have not the permissions"))
109109
.status(HttpStatus.BAD_REQUEST).statusCode(HttpStatus.BAD_REQUEST.value()).build());
110110
}
111-
}else {
111+
} else {
112112
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
113113
.message(("Error seeing the bill, you have not the permissions"))
114114
.status(HttpStatus.BAD_REQUEST).statusCode(HttpStatus.BAD_REQUEST.value()).build());
115115
}
116-
117116

118117
} catch (Exception e) {
119118
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
@@ -129,6 +128,11 @@ public ResponseEntity<Response> listByParams(@Param(value = "idBill") Long idBil
129128
.message("bill").status(HttpStatus.OK).statusCode(HttpStatus.OK.value()).build());
130129

131130
}
131+
if (statusBill == null && statusOrder != null && startDate != null && endDate != null) {
132+
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
133+
.data(Map.of("bill", serviceImp.findByOrder(statusOrder, startDate, endDate))).message("bill")
134+
.status(HttpStatus.OK).statusCode(HttpStatus.OK.value()).build());
135+
}
132136
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
133137
.data(Map.of("bill", serviceImp.findByNewIdUser(idUser, statusBill, startDate, endDate)))
134138
.message("bill").status(HttpStatus.OK).statusCode(HttpStatus.OK.value()).build());

src/main/java/JEstebanC/FastFoodApp/controller/RefreshTokenController.java

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
88

99
import java.io.IOException;
10+
import java.time.Instant;
1011
import java.util.Date;
1112
import java.util.HashMap;
1213
import java.util.Map;
1314

1415
import org.springframework.beans.factory.annotation.Autowired;
15-
import org.springframework.util.MimeTypeUtils;
16+
import org.springframework.http.HttpStatus;
17+
import org.springframework.http.ResponseEntity;
1618
import org.springframework.web.bind.annotation.GetMapping;
1719
import org.springframework.web.bind.annotation.RequestMapping;
1820
import org.springframework.web.bind.annotation.RestController;
1921

22+
import JEstebanC.FastFoodApp.model.Response;
2023
import JEstebanC.FastFoodApp.model.User;
2124
import JEstebanC.FastFoodApp.security.OperationUtil;
2225
import JEstebanC.FastFoodApp.service.UserServiceImp;
@@ -29,7 +32,6 @@
2932
import com.auth0.jwt.interfaces.DecodedJWT;
3033
import com.fasterxml.jackson.core.exc.StreamWriteException;
3134
import com.fasterxml.jackson.databind.DatabindException;
32-
import com.fasterxml.jackson.databind.ObjectMapper;
3335

3436
/**
3537
* @author Juan Esteban Castaño Holguin castanoesteban9@gmail.com 2022-02-05
@@ -42,49 +44,57 @@ public class RefreshTokenController {
4244

4345
@Autowired
4446
private final UserServiceImp serviceImp;
47+
4548
@GetMapping()
46-
public void refreshToken(HttpServletRequest request, HttpServletResponse response) throws StreamWriteException, DatabindException, IOException {
49+
public ResponseEntity<Response> refreshToken(HttpServletRequest request, HttpServletResponse response)
50+
throws StreamWriteException, DatabindException, IOException {
4751
String authorizationHeader = request.getHeader(AUTHORIZATION);
48-
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
49-
try {
50-
// Remove the word Bearer, because we just want the token
51-
String refresh_token = authorizationHeader.substring("Bearer ".length());
52-
// Reference to the keyValue
53-
Algorithm algorithm = Algorithm.HMAC256(OperationUtil.keyValue().getBytes());
54-
JWTVerifier verifier = JWT.require(algorithm).build();
55-
DecodedJWT decodeJWT = verifier.verify(refresh_token);
56-
String username = decodeJWT.getSubject();
57-
if (serviceImp.findByUsername(username) != null) {
58-
User user = serviceImp.findByUsername(username);
59-
60-
String access_token = JWT.create().withSubject(user.getUsername())
61-
.withExpiresAt(new Date(System.currentTimeMillis() + 30 * 60 * 1000))
62-
.withIssuer(request.getRequestURL().toString())
63-
.withClaim("roles",user.getUserRoles().getAuthority())
64-
.sign(algorithm);
65-
66-
Map<String, String> tokens = new HashMap<>();
67-
tokens.put("access_token", access_token);
68-
response.setContentType(MimeTypeUtils.APPLICATION_JSON_VALUE);
69-
new ObjectMapper().writeValue(response.getOutputStream(), tokens);
70-
} else {
71-
log.error("Error username not found");
72-
}
7352

74-
} catch (Exception e) {
75-
log.error("Error logging in AuthorizationFilter: " + e.getMessage());
76-
response.setHeader("Error", e.getMessage());
77-
response.setStatus(401);
78-
Map<String, String> error = new HashMap<>();
79-
error.put("error_message", e.getMessage());
80-
response.setContentType(MimeTypeUtils.APPLICATION_JSON_VALUE);
53+
try {
54+
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
55+
try {
56+
// Remove the word Bearer, because we just want the token
57+
String refresh_token = authorizationHeader.substring("Bearer ".length());
58+
// Reference to the keyValue
59+
Algorithm algorithm = Algorithm.HMAC256(OperationUtil.keyValue().getBytes());
60+
JWTVerifier verifier = JWT.require(algorithm).build();
61+
DecodedJWT decodeJWT = verifier.verify(refresh_token);
62+
String username = decodeJWT.getSubject();
63+
if (serviceImp.findByUsername(username) != null) {
64+
User user = serviceImp.findByUsername(username);
8165

82-
new ObjectMapper().writeValue(response.getOutputStream(), error);
83-
}
66+
String access_token = JWT.create().withSubject(user.getUsername())
67+
.withExpiresAt(new Date(System.currentTimeMillis() + 30 * 60 * 1000))
68+
.withIssuer(request.getRequestURL().toString())
69+
.withClaim("roles", user.getUserRoles().getAuthority()).sign(algorithm);
70+
71+
Map<String, Object> tokens = new HashMap<>();
72+
tokens.put("valid", true);
73+
tokens.put("access_token", access_token);
74+
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
75+
.data(Map.of("tokens", tokens)).message("tokens").status(HttpStatus.OK)
76+
.statusCode(HttpStatus.OK.value()).build());
77+
} else {
78+
log.error("Error username not found");
79+
return ResponseEntity
80+
.ok(Response.builder().timeStamp(Instant.now()).message("Error username not found")
81+
.status(HttpStatus.OK).statusCode(HttpStatus.OK.value()).build());
82+
}
8483

85-
} else {
86-
throw new RuntimeException("Refresh token is missing");
84+
} catch (Exception e) {
85+
log.error("Error logging in AuthorizationFilter: " + e.getMessage());
86+
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now()).message(e.getMessage())
87+
.status(HttpStatus.OK).statusCode(HttpStatus.OK.value()).build());
88+
}
89+
90+
}
91+
} catch (Exception e) {
92+
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now()).message("Refresh token is missing")
93+
.status(HttpStatus.OK).statusCode(HttpStatus.OK.value()).build());
8794
}
95+
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now()).message("Refresh token is missing")
96+
.status(HttpStatus.OK).statusCode(HttpStatus.OK.value()).build());
97+
8898
}
8999

90100
}

src/main/java/JEstebanC/FastFoodApp/controller/UserController.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public ResponseEntity<Response> getUserByName(@PathVariable("name") String name)
175175

176176
} else {
177177
return ResponseEntity.ok(
178-
Response.builder().timeStamp(Instant.now()).message("The user called" + name + " does not exist")
178+
Response.builder().timeStamp(Instant.now()).message("The user called " + name + " does not exist")
179179
.status(HttpStatus.BAD_REQUEST).statusCode(HttpStatus.BAD_REQUEST.value()).build());
180180
}
181181
}
@@ -195,6 +195,37 @@ public ResponseEntity<Response> getUserByEmail(@PathVariable("email") String ema
195195
}
196196
}
197197

198+
// SEARCH BY EMAIL VALIDATION
199+
@GetMapping(value = "/is-valid-email/{email}")
200+
public ResponseEntity<Response> getUserByEmailValidation(@PathVariable("email") String email) {
201+
if (serviceImp.findByEmailValid(email) != null) {
202+
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
203+
.data(Map.of("User", serviceImp.findByEmailValid(email))).message("Get user by email: " + email)
204+
.status(HttpStatus.OK).statusCode(HttpStatus.OK.value()).build());
205+
} else {
206+
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
207+
.message("The User with mail: " + email + " does not exist").status(HttpStatus.BAD_REQUEST)
208+
.statusCode(HttpStatus.BAD_REQUEST.value()).build());
209+
}
210+
}
211+
212+
// SEARCH BY USERNAME VALIDATION
213+
@GetMapping(value = "/is-valid-username/{username}")
214+
public ResponseEntity<Response> getUserByNameValidation(@PathVariable("username") String username) {
215+
216+
if (serviceImp.findByUsernameValidation(username) != null) {
217+
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
218+
.data(Map.of("User", serviceImp.findByUsernameValidation(username)))
219+
.message("Get user by name: " + username).status(HttpStatus.OK).statusCode(HttpStatus.OK.value())
220+
.build());
221+
222+
} else {
223+
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
224+
.message("The user called " + username + " does not exist").status(HttpStatus.BAD_REQUEST)
225+
.statusCode(HttpStatus.BAD_REQUEST.value()).build());
226+
}
227+
}
228+
198229
private ResponseEntity<Response> actionForRole(Long id, String strUser, HttpServletRequest request,
199230
MultipartFile file) throws JsonMappingException, JsonProcessingException {
200231
if (request.isUserInRole("ROLE_EMPLOYEE")) {

src/main/java/JEstebanC/FastFoodApp/dto/BillOrdersDTO.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
import org.springframework.format.annotation.DateTimeFormat;
1010

11+
import JEstebanC.FastFoodApp.dto.validation.UserForBillDTO;
1112
import JEstebanC.FastFoodApp.enumeration.StatusBill;
13+
import JEstebanC.FastFoodApp.enumeration.StatusOrder;
1214
import JEstebanC.FastFoodApp.model.PayMode;
1315
import JEstebanC.FastFoodApp.model.Additional;
1416
import JEstebanC.FastFoodApp.model.Product;
@@ -33,6 +35,7 @@ public class BillOrdersDTO {
3335

3436
// Order
3537
private Long idOrder;
38+
private StatusOrder statusOrder;
3639
private int amount;
3740
private int noTable;
3841
private int total;

src/main/java/JEstebanC/FastFoodApp/dto/BillUserDTO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.springframework.format.annotation.DateTimeFormat;
99

10+
import JEstebanC.FastFoodApp.dto.validation.UserForBillDTO;
1011
import JEstebanC.FastFoodApp.enumeration.StatusBill;
1112
import JEstebanC.FastFoodApp.model.PayMode;
1213
import lombok.Data;

src/main/java/JEstebanC/FastFoodApp/dto/OrdersDTO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.Collection;
77

8+
import JEstebanC.FastFoodApp.enumeration.StatusOrder;
89
import JEstebanC.FastFoodApp.model.Additional;
910
import JEstebanC.FastFoodApp.model.Product;
1011
import lombok.Data;
@@ -19,6 +20,7 @@ public class OrdersDTO {
1920

2021
// Order
2122
private Long idOrder;
23+
private StatusOrder statusOrder;
2224
private int amount;
2325
private int noTable;
2426
private int total;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
*
3+
*/
4+
package JEstebanC.FastFoodApp.dto.validation;
5+
6+
import lombok.Data;
7+
8+
/**
9+
* @author Juan Esteban Castaño Holguin
10+
* castanoesteban9@gmail.com
11+
* 2022-04-22
12+
*/
13+
@Data
14+
public class UserEmailDTO {
15+
private String email;
16+
}

src/main/java/JEstebanC/FastFoodApp/dto/UserForBillDTO.java renamed to src/main/java/JEstebanC/FastFoodApp/dto/validation/UserForBillDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
*/
4-
package JEstebanC.FastFoodApp.dto;
4+
package JEstebanC.FastFoodApp.dto.validation;
55

66
import lombok.Data;
77

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
*
3+
*/
4+
package JEstebanC.FastFoodApp.dto.validation;
5+
6+
import lombok.Data;
7+
8+
/**
9+
* @author Juan Esteban Castaño Holguin
10+
* castanoesteban9@gmail.com
11+
* 2022-04-22
12+
*/
13+
@Data
14+
public class UsernameDTO {
15+
private String username;
16+
}

src/main/java/JEstebanC/FastFoodApp/enumeration/Status.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@AllArgsConstructor
1313
public enum Status {
1414

15-
INACTIVO(0),
16-
ACTIVO(1);
15+
INACTIVE(0),
16+
ACTIVE(1);
1717
private final int status;
1818
}

src/main/java/JEstebanC/FastFoodApp/enumeration/StatusBill.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public enum StatusBill {
44

5-
PAGO,PENDIENTE,NOPAGO;
5+
PAID, PENDING, NONPAYMENT;
66
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*
3+
*/
4+
package JEstebanC.FastFoodApp.enumeration;
5+
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
9+
/**
10+
* @author Juan Esteban Castaño Holguin
11+
* castanoesteban9@gmail.com
12+
* 2022-04-22
13+
*/
14+
@Getter
15+
@AllArgsConstructor
16+
public enum StatusOrder {
17+
NEW,
18+
COOKING,
19+
DELIVERED,
20+
}

src/main/java/JEstebanC/FastFoodApp/model/Orders.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
import javax.persistence.ManyToOne;
1414
import javax.validation.constraints.NotNull;
1515

16-
import JEstebanC.FastFoodApp.enumeration.Status;
17-
16+
import JEstebanC.FastFoodApp.enumeration.StatusOrder;
1817
import lombok.AllArgsConstructor;
1918
import lombok.Data;
2019
import lombok.NoArgsConstructor;
@@ -53,6 +52,6 @@ public class Orders {
5352
private int noTable;
5453
@NotNull(message = "total cannot be empty or null")
5554
private int total;
56-
private Status status;
55+
private StatusOrder statusOrder;
5756

5857
}

src/main/java/JEstebanC/FastFoodApp/model/User.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class User {
2727

2828
@Id
29-
@GeneratedValue(strategy = GenerationType.AUTO)
29+
@GeneratedValue(strategy = GenerationType.IDENTITY)
3030
private Long idUser;
3131
@NotNull(message = "name cannot be empty or null")
3232
@Column(length = 50)

0 commit comments

Comments
 (0)