-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathUserController.java
More file actions
51 lines (44 loc) · 1.98 KB
/
UserController.java
File metadata and controls
51 lines (44 loc) · 1.98 KB
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
package com.prgmrs.voucher.controller.console;
import com.prgmrs.voucher.controller.console.wrapper.ResponseDTO;
import com.prgmrs.voucher.dto.request.UserRequest;
import com.prgmrs.voucher.dto.request.VoucherIdRequest;
import com.prgmrs.voucher.enums.StatusCode;
import com.prgmrs.voucher.exception.WrongRangeFormatException;
import com.prgmrs.voucher.service.UserService;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
@Component
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
public ResponseDTO<?> createUser(UserRequest userRequest) {
try {
return new ResponseDTO<>(userService.createUser(userRequest), StatusCode.REQUEST_OK);
} catch (WrongRangeFormatException | DataAccessException e) {
return new ResponseDTO<>(e.getMessage(), StatusCode.BAD_REQUEST);
}
}
public ResponseDTO<?> getAllUsers() {
try {
return new ResponseDTO<>(userService.findAll(), StatusCode.REQUEST_OK);
} catch (WrongRangeFormatException | DataAccessException e) {
return new ResponseDTO<>(e.getMessage(), StatusCode.BAD_REQUEST);
}
}
public ResponseDTO<?> getUserListWithVoucherAssigned() {
try {
return new ResponseDTO<>(userService.getUserListWithVoucherAssigned(), StatusCode.REQUEST_OK);
} catch (WrongRangeFormatException | DataAccessException e) {
return new ResponseDTO<>(e.getMessage(), StatusCode.BAD_REQUEST);
}
}
public ResponseDTO<?> getUserByVoucherId(VoucherIdRequest voucherIdRequest) {
try {
return new ResponseDTO<>(userService.getUserByVoucherId(voucherIdRequest), StatusCode.REQUEST_OK);
} catch (WrongRangeFormatException | DataAccessException e) {
return new ResponseDTO<>(e.getMessage(), StatusCode.BAD_REQUEST);
}
}
}