Skip to content

Commit 4f1e119

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 5fb44ee + 4d6fb2d commit 4f1e119

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

src/main/java/com/example/firstAPI/controller/UserController.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import com.example.firstAPI.DEnum.ECity;
55
import com.example.firstAPI.dto.request.UserRequestDTO;
6+
import com.example.firstAPI.dto.response.ResponData;
7+
import com.example.firstAPI.dto.response.ResponseError;
68
import com.example.firstAPI.dto.response.ResponseFailure;
79
import com.example.firstAPI.dto.response.ResponseSuccess;
810
import jakarta.validation.Valid;
@@ -16,37 +18,37 @@
1618
@RequestMapping("/user")
1719
public class UserController {
1820
@PostMapping("/")
19-
public ResponseSuccess createUser(@Valid @RequestBody UserRequestDTO user){
21+
public ResponData<?> createUser(@Valid @RequestBody UserRequestDTO user){
2022
System.out.println("Request add user: " + user.getFirstName());
2123
try{
22-
return new ResponseSuccess(HttpStatus.CREATED, "User added successfully");
24+
return new ResponData<>(HttpStatus.CREATED.value(), "User added successfully");
2325
} catch (Exception e) {
24-
return new ResponseFailure(HttpStatus.BAD_REQUEST, e.getMessage());
26+
return new ResponseError<>(HttpStatus.BAD_REQUEST.value(), e.getMessage());
2527
}
2628
}
2729
@GetMapping("/{id}")
28-
public ResponseSuccess getUser(@PathVariable("id") String id){
30+
public ResponData<?> getUser(@PathVariable("id") String id){
2931
System.out.println("get id: " + id);
3032
try{
31-
return new ResponseSuccess(HttpStatus.OK, "user", new UserRequestDTO("Nguyen", "Son", "[email protected]", 18, ECity.HANOI));
33+
return new ResponData<>(HttpStatus.OK.value(), "user", new UserRequestDTO("Nguyen", "Son", "[email protected]", 18, ECity.HANOI));
3234
} catch (Exception e) {
33-
return new ResponseFailure(HttpStatus.BAD_REQUEST, e.getMessage());
35+
return new ResponseError<>(HttpStatus.BAD_REQUEST.value(), e.getMessage());
3436
}
3537
}
3638
@PutMapping("/")
37-
public ResponseSuccess updateUser(@RequestBody UserRequestDTO user){
39+
public ResponData<?> updateUser(@RequestBody UserRequestDTO user){
3840
try{
39-
return new ResponseSuccess(HttpStatus.ACCEPTED, "User updated successfully");
41+
return new ResponData<>(HttpStatus.ACCEPTED.value(), "User updated successfully");
4042
} catch (Exception e) {
41-
return new ResponseFailure(HttpStatus.BAD_REQUEST, e.getMessage());
43+
return new ResponseError<>(HttpStatus.BAD_REQUEST.value(), e.getMessage());
4244
}
4345
}
4446
@DeleteMapping("/{id}")
45-
public ResponseSuccess deleteUser(@PathVariable String id){
47+
public ResponData<?> deleteUser(@PathVariable String id){
4648
try{
47-
return new ResponseSuccess(HttpStatus.NO_CONTENT, "delete " + id + " successfully");
49+
return new ResponData<>(HttpStatus.NO_CONTENT.value(), "delete " + id + " successfully");
4850
} catch (Exception e) {
49-
return new ResponseFailure(HttpStatus.BAD_REQUEST, e.getMessage());
51+
return new ResponseError<>(HttpStatus.BAD_REQUEST.value(), e.getMessage());
5052
}
5153
}
5254
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.firstAPI.dto.response;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
5+
public class ResponData<T> {
6+
private final int status;
7+
private final String message;
8+
@JsonInclude(JsonInclude.Include.NON_NULL)
9+
private T data;
10+
11+
public ResponData(int status, String message, T data) {
12+
this.status = status;
13+
this.message = message;
14+
this.data = data;
15+
}
16+
17+
public ResponData(int status, String message) {
18+
this.status = status;
19+
this.message = message;
20+
}
21+
22+
public int getStatus() {
23+
return status;
24+
}
25+
26+
public String getMessage() {
27+
return message;
28+
}
29+
30+
public T getData() {
31+
return data;
32+
}
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.firstAPI.dto.response;
2+
3+
public class ResponseError<T> extends ResponData{
4+
public ResponseError(int status, String message) {
5+
super(status, message);
6+
}
7+
8+
public ResponseError(int status, String message, T data) {
9+
super(status, message, data);
10+
}
11+
}

0 commit comments

Comments
 (0)