Skip to content

Commit 71f9a7e

Browse files
Update UserRestControler
1 parent 292e2c1 commit 71f9a7e

File tree

1 file changed

+137
-1
lines changed

1 file changed

+137
-1
lines changed

backend/webandtech/src/main/java/webapp8/webandtech/controller/api/users/UserRestControler.java

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,142 @@ public List<Users> getAllUsers(){
3535
return userService.getAll();
3636
}
3737

38+
@Operation(summary = "Get a user by its id")
39+
@ApiResponses(value = {
40+
@ApiResponse(
41+
responseCode = "200",
42+
description = "Found the User",
43+
content = {@Content(
44+
mediaType = "application/json"
45+
)}
46+
),
47+
@ApiResponse(
48+
responseCode = "404",
49+
description = "User not found",
50+
content = @Content
51+
)
52+
})
53+
@JsonView(Users.Detailed.class)
54+
@GetMapping("/{id}")
55+
public ResponseEntity<Users> getUsersById(@Parameter(description="id of user to be searched") @PathVariable int id){
56+
Optional<Users> user = userService.getUserId(id);
57+
if(!user.isEmpty()){
58+
return ResponseEntity.ok(user.get());
59+
}else {
60+
return ResponseEntity.notFound().build();
61+
}
62+
}
3863

39-
}
64+
@Operation(summary = "Create a user")
65+
@ApiResponses(value = {
66+
@ApiResponse(
67+
responseCode = "201",
68+
description = "Successful user creation",
69+
content = {@Content(
70+
mediaType = "application/json"
71+
)}
72+
),
73+
@ApiResponse(
74+
responseCode = "406 ",
75+
description = "Not Acceptable user creation the username or email is token",
76+
content = {@Content(
77+
mediaType = "application/json"
78+
)}
79+
)
80+
})
81+
@JsonView(Users.Detailed.class)
82+
@PostMapping("/")
83+
public ResponseEntity<Users> registerUser(@Parameter(description="Object Json Type Users") @RequestBody Users user) throws IOException{
84+
if(user.getEmail() != null) {
85+
if(userService.existEmail(user.getEmail())) {
86+
return new ResponseEntity<>(user,HttpStatus.NOT_ACCEPTABLE);
87+
}
88+
}
89+
if(!userService.existsUser(user.getUsername())) {
90+
Resource imagedefault = new ClassPathResource("/static/images/avatar.png");
91+
user.setUserimg(BlobProxy.generateProxy(imagedefault.getInputStream(), imagedefault.contentLength()));
92+
userService.saveUser(user);
93+
user = userService.getUser(user.getUsername());
94+
URI location = fromCurrentRequest().path("/{id}").buildAndExpand(user.getIdusers()).toUri();
95+
return ResponseEntity.created(location).body(user);
96+
}else {
97+
return new ResponseEntity<>(user,HttpStatus.NOT_ACCEPTABLE);
98+
}
99+
}
40100

101+
@Operation(summary = "Modify a user")
102+
@ApiResponses(value = {
103+
@ApiResponse(
104+
responseCode = "201",
105+
description = "Successful user modification",
106+
content = {@Content(
107+
mediaType = "application/json"
108+
)}
109+
),
110+
@ApiResponse(
111+
responseCode = "404",
112+
description = "User not found",
113+
content = @Content
114+
),
115+
@ApiResponse(
116+
responseCode = "406 ",
117+
description = "Not Acceptable user creation the username or email is token",
118+
content = {@Content(
119+
mediaType = "application/json"
120+
)}
121+
)
122+
})
123+
@JsonView(Users.Detailed.class)
124+
@PutMapping("/{id}")
125+
public ResponseEntity<Users> replaceUser(@Parameter(description="id of user to be searched") @PathVariable int id, @Parameter(description="Object Json Type Users") @RequestBody Users user) throws IOException{
126+
Optional<Users> use = userService.getUserId(id);
127+
if(!use.isEmpty()) {
128+
if(user.getEmail() != null) {
129+
if(userService.existEmail(user.getEmail())) {
130+
return new ResponseEntity<>(user,HttpStatus.NOT_ACCEPTABLE);
131+
}
132+
}
133+
if(user.getUsername() == null) {
134+
user.setUsername(use.get().getUsername());
135+
user.setIdusers(id);
136+
userService.modifyDataUser(user, use.get().getUsername(),null,null);
137+
if(user.getPass() != null) {
138+
userService.modifyPass(user.getUsername(), user.getPass());
139+
}
140+
user = userService.getUserId(id).get();
141+
return ResponseEntity.ok(user);
142+
}else {
143+
if(!userService.existsUser(user.getUsername())) {
144+
user.setIdusers(id);
145+
userService.modifyDataUser(user, use.get().getUsername(),null,null);
146+
if(user.getPass() != null) {
147+
userService.modifyPass(user.getUsername(), user.getPass());
148+
}
149+
user = userService.getUserId(id).get();
150+
return ResponseEntity.ok(user);
151+
}else {
152+
return new ResponseEntity<>(user,HttpStatus.NOT_ACCEPTABLE);
153+
}
154+
}
155+
}else {
156+
return ResponseEntity.notFound().build();
157+
}
158+
}
159+
160+
@Operation(summary = "Delete a user")
161+
@ApiResponses(value = {
162+
@ApiResponse(
163+
responseCode = "200",
164+
description = "Successful user delete",
165+
content = {@Content(
166+
mediaType = "application/json"
167+
)}
168+
),
169+
@ApiResponse(
170+
responseCode = "404",
171+
description = "User not found",
172+
content = @Content
173+
)
174+
})
175+
176+
}

0 commit comments

Comments
 (0)