forked from RameshMF/Spring-Boot-Blog-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserController.java
More file actions
144 lines (114 loc) · 5.93 KB
/
UserController.java
File metadata and controls
144 lines (114 loc) · 5.93 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.sopromadze.blogapi.controller;
import com.sopromadze.blogapi.model.Album;
import com.sopromadze.blogapi.model.Post;
import com.sopromadze.blogapi.model.user.User;
import com.sopromadze.blogapi.payload.ApiResponse;
import com.sopromadze.blogapi.payload.InfoRequest;
import com.sopromadze.blogapi.payload.PagedResponse;
import com.sopromadze.blogapi.payload.UserIdentityAvailability;
import com.sopromadze.blogapi.payload.UserProfile;
import com.sopromadze.blogapi.payload.UserSummary;
import com.sopromadze.blogapi.security.CurrentUser;
import com.sopromadze.blogapi.security.UserPrincipal;
import com.sopromadze.blogapi.service.AlbumService;
import com.sopromadze.blogapi.service.PostService;
import com.sopromadze.blogapi.service.UserService;
import com.sopromadze.blogapi.utils.AppConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private PostService postService;
@Autowired
private AlbumService albumService;
@GetMapping("/me")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<UserSummary> getCurrentUser(@CurrentUser UserPrincipal currentUser) {
UserSummary userSummary = userService.getCurrentUser(currentUser);
return new ResponseEntity< >(userSummary, HttpStatus.OK);
}
@GetMapping("/checkUsernameAvailability")
public ResponseEntity<UserIdentityAvailability> checkUsernameAvailability(@RequestParam(value = "username") String username) {
UserIdentityAvailability userIdentityAvailability = userService.checkUsernameAvailability(username);
return new ResponseEntity< >(userIdentityAvailability, HttpStatus.OK);
}
@GetMapping("/checkEmailAvailability")
public ResponseEntity<UserIdentityAvailability> checkEmailAvailability(@RequestParam(value = "email") String email) {
UserIdentityAvailability userIdentityAvailability = userService.checkEmailAvailability(email);
return new ResponseEntity< >(userIdentityAvailability, HttpStatus.OK);
}
@GetMapping("/{username}/profile")
public ResponseEntity<UserProfile> getUSerProfile(@PathVariable(value = "username") String username) {
UserProfile userProfile = userService.getUserProfile(username);
return new ResponseEntity< >(userProfile, HttpStatus.OK);
}
@GetMapping("/{username}/posts")
public ResponseEntity<PagedResponse<Post>> getPostsCreatedBy(@PathVariable(value = "username") String username,
@RequestParam(value = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
@RequestParam(value = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {
PagedResponse<Post> response = postService.getPostsByCreatedBy(username, page, size);
return new ResponseEntity< >(response, HttpStatus.OK);
}
@GetMapping("/{username}/albums")
public ResponseEntity<PagedResponse<Album>> getUserAlbums(@PathVariable(name = "username") String username,
@RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
@RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {
PagedResponse<Album> response = albumService.getUserAlbums(username, page, size);
return new ResponseEntity< >(response, HttpStatus.OK);
}
@PostMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<User> addUser(@Valid @RequestBody User user) {
User newUser = userService.addUser(user);
return new ResponseEntity< >(newUser, HttpStatus.CREATED);
}
@PutMapping("/{username}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<User> updateUser(@Valid @RequestBody User newUser,
@PathVariable(value = "username") String username, @CurrentUser UserPrincipal currentUser) {
User updatedUSer = userService.updateUser(newUser, username, currentUser);
return new ResponseEntity< >(updatedUSer, HttpStatus.CREATED);
}
@DeleteMapping("/{username}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<ApiResponse> deleteUser(@PathVariable(value = "username") String username,
@CurrentUser UserPrincipal currentUser) {
ApiResponse apiResponse = userService.deleteUser(username, currentUser);
return new ResponseEntity< >(apiResponse, HttpStatus.OK);
}
@PutMapping("/{username}/giveAdmin")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<ApiResponse> giveAdmin(@PathVariable(name = "username") String username) {
ApiResponse apiResponse = userService.giveAdmin(username);
return new ResponseEntity< >(apiResponse, HttpStatus.OK);
}
@PutMapping("/{username}/takeAdmin")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<ApiResponse> takeAdmin(@PathVariable(name = "username") String username) {
ApiResponse apiResponse = userService.removeAdmin(username);
return new ResponseEntity< >(apiResponse, HttpStatus.OK);
}
@PutMapping("/setOrUpdateInfo")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<UserProfile> setAddress(@CurrentUser UserPrincipal currentUser,
@Valid @RequestBody InfoRequest infoRequest) {
UserProfile userProfile = userService.setOrUpdateInfo(currentUser, infoRequest);
return new ResponseEntity< >(userProfile, HttpStatus.OK);
}
}