Skip to content

Commit 874b18b

Browse files
feat: initial implementation of UserController class
1 parent bc85ac2 commit 874b18b

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package meet_at_mensa.user.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
import meet_at_mensa.user.exception.UserNotFoundException;
8+
import meet_at_mensa.user.service.UserService;
9+
10+
import org.openapitools.api.UserApi;
11+
import org.openapitools.model.User;
12+
import org.openapitools.model.UserNew;
13+
import org.openapitools.model.UserUpdate;
14+
15+
import java.util.UUID;
16+
17+
18+
19+
@RestController
20+
public class UserController implements UserApi {
21+
22+
// UserService handles database operations
23+
@Autowired
24+
private UserService userService;
25+
26+
27+
// DELETE @ api/v2/user/{userID}
28+
@Override
29+
public ResponseEntity<Void> deleteApiV2UserUserID(UUID userID) {
30+
31+
try {
32+
33+
// attempt to delete user with given ID
34+
userService.deleteUser(userID);
35+
36+
// if delete operation succeeded, return 200
37+
return ResponseEntity.ok().build();
38+
39+
} catch (UserNotFoundException e) {
40+
41+
// if user was not found, return 404
42+
return ResponseEntity.notFound().build();
43+
44+
}
45+
}
46+
47+
48+
// GET @ api/v2/user/{userID}
49+
@Override
50+
public ResponseEntity<User> getApiV2UserUserID(UUID userID) {
51+
52+
try {
53+
54+
// attempt to get user with the given ID
55+
User user = userService.getUser(userID);
56+
57+
// return 200 with User
58+
return ResponseEntity.ok(user);
59+
60+
61+
} catch (UserNotFoundException e) {
62+
63+
// if user was not found, return 404
64+
return ResponseEntity.notFound().build();
65+
66+
}
67+
}
68+
69+
70+
// POST @ api/v2/user/register
71+
@Override
72+
public ResponseEntity<User> postApiV2UserRegister(UserNew userNew) {
73+
74+
try {
75+
76+
// attempt to register a new user
77+
User newUser = userService.registerUser(userNew);
78+
79+
return ResponseEntity.status(201).body(newUser);
80+
81+
} catch (Exception e) {
82+
83+
// TODO fix signature to match API, this should not return 503
84+
return ResponseEntity.internalServerError().build();
85+
86+
}
87+
}
88+
89+
// PUT @ api/v2/user/{userID}
90+
@Override
91+
public ResponseEntity<User> putApiV2UserUserID(UUID userID, UserUpdate userUpdate) {
92+
93+
try {
94+
95+
// attempt to update User
96+
User updatedUser = userService.updateUser(userID, userUpdate);
97+
98+
// return the updated user
99+
return ResponseEntity.ok(updatedUser);
100+
101+
} catch (UserNotFoundException e) {
102+
103+
// if user was not found, return 404
104+
return ResponseEntity.notFound().build();
105+
}
106+
107+
}
108+
109+
}

0 commit comments

Comments
 (0)