Skip to content

Commit 8afa9e0

Browse files
author
Omari Sopromadze
committed
removed ResponseEntity wildcards
1 parent d783224 commit 8afa9e0

28 files changed

Lines changed: 676 additions & 347 deletions

src/main/java/com/sopromadze/blogapi/controller/AlbumController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.validation.Valid;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
67
import org.springframework.http.ResponseEntity;
78
import org.springframework.security.access.prepost.PreAuthorize;
89
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -78,10 +79,13 @@ public ResponseEntity<ApiResponse> deleteAlbum(@PathVariable(name = "id") Long i
7879
}
7980

8081
@GetMapping("/{id}/photos")
81-
public PagedResponse<PhotoResponse> getAllPhotosByAlbum(@PathVariable(name = "id") Long id,
82+
public ResponseEntity<PagedResponse<PhotoResponse>> getAllPhotosByAlbum(@PathVariable(name = "id") Long id,
8283
@RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
8384
@RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {
84-
return photoService.getAllPhotosByAlbum(id, page, size);
85+
86+
PagedResponse<PhotoResponse> response = photoService.getAllPhotosByAlbum(id, page, size);
87+
88+
return new ResponseEntity<PagedResponse<PhotoResponse>>(response, HttpStatus.OK);
8589
}
8690

8791
}

src/main/java/com/sopromadze/blogapi/controller/CategoryController.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.springframework.web.bind.annotation.RequestParam;
1616
import org.springframework.web.bind.annotation.RestController;
1717

18-
import com.sopromadze.blogapi.exception.UnathorizedException;
18+
import com.sopromadze.blogapi.exception.UnauthorizedException;
1919
import com.sopromadze.blogapi.model.category.Category;
2020
import com.sopromadze.blogapi.payload.ApiResponse;
2121
import com.sopromadze.blogapi.payload.PagedResponse;
@@ -28,36 +28,40 @@
2828
@RequestMapping("/api/categories")
2929
public class CategoryController {
3030
@Autowired
31-
private CategoryService categoryService;
32-
33-
@GetMapping
34-
public PagedResponse<Category> getAllCategories(
35-
@RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
36-
@RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size){
37-
return categoryService.getAllCategories(page, size);
38-
}
39-
40-
@PostMapping
41-
@PreAuthorize("hasRole('USER')")
42-
public ResponseEntity<Category> addCategory(@Valid @RequestBody Category category, @CurrentUser UserPrincipal currentUser){
43-
return categoryService.addCategory(category, currentUser);
44-
}
45-
46-
@GetMapping("/{id}")
47-
public ResponseEntity<Category> getCategory(@PathVariable(name = "id") Long id){
48-
return categoryService.getCategory(id);
49-
}
50-
51-
@PutMapping("/{id}")
52-
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
53-
public ResponseEntity<Category> updateCategory(@PathVariable(name = "id") Long id, @Valid @RequestBody Category category, @CurrentUser UserPrincipal currentUser) throws UnathorizedException{
54-
return categoryService.updateCategory(id, category, currentUser);
55-
}
56-
57-
@DeleteMapping("/{id}")
58-
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
59-
public ResponseEntity<ApiResponse> deleteCategory(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) throws UnathorizedException{
60-
return categoryService.deleteCategory(id, currentUser);
61-
}
31+
private CategoryService categoryService;
32+
33+
@GetMapping
34+
public PagedResponse<Category> getAllCategories(
35+
@RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
36+
@RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {
37+
return categoryService.getAllCategories(page, size);
38+
}
39+
40+
@PostMapping
41+
@PreAuthorize("hasRole('USER')")
42+
public ResponseEntity<Category> addCategory(@Valid @RequestBody Category category,
43+
@CurrentUser UserPrincipal currentUser) {
44+
45+
return categoryService.addCategory(category, currentUser);
46+
}
47+
48+
@GetMapping("/{id}")
49+
public ResponseEntity<Category> getCategory(@PathVariable(name = "id") Long id) {
50+
return categoryService.getCategory(id);
51+
}
52+
53+
@PutMapping("/{id}")
54+
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
55+
public ResponseEntity<Category> updateCategory(@PathVariable(name = "id") Long id,
56+
@Valid @RequestBody Category category, @CurrentUser UserPrincipal currentUser) throws UnauthorizedException {
57+
return categoryService.updateCategory(id, category, currentUser);
58+
}
59+
60+
@DeleteMapping("/{id}")
61+
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
62+
public ResponseEntity<ApiResponse> deleteCategory(@PathVariable(name = "id") Long id,
63+
@CurrentUser UserPrincipal currentUser) throws UnauthorizedException {
64+
return categoryService.deleteCategory(id, currentUser);
65+
}
6266

6367
}

src/main/java/com/sopromadze/blogapi/controller/PhotoController.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.validation.Valid;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
67
import org.springframework.http.ResponseEntity;
78
import org.springframework.security.access.prepost.PreAuthorize;
89
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -15,6 +16,7 @@
1516
import org.springframework.web.bind.annotation.RequestParam;
1617
import org.springframework.web.bind.annotation.RestController;
1718

19+
import com.sopromadze.blogapi.payload.ApiResponse;
1820
import com.sopromadze.blogapi.payload.PagedResponse;
1921
import com.sopromadze.blogapi.payload.PhotoRequest;
2022
import com.sopromadze.blogapi.payload.PhotoResponse;
@@ -38,26 +40,35 @@ public PagedResponse<PhotoResponse> getAllPhotos(
3840

3941
@PostMapping
4042
@PreAuthorize("hasRole('USER')")
41-
public ResponseEntity<?> addPhoto(@Valid @RequestBody PhotoRequest photoRequest,
43+
public ResponseEntity<PhotoResponse> addPhoto(@Valid @RequestBody PhotoRequest photoRequest,
4244
@CurrentUser UserPrincipal currentUser) {
43-
return photoService.addPhoto(photoRequest, currentUser);
45+
PhotoResponse photoResponse = photoService.addPhoto(photoRequest, currentUser);
46+
47+
return new ResponseEntity<PhotoResponse>(photoResponse, HttpStatus.OK);
4448
}
4549

4650
@GetMapping("/{id}")
47-
public ResponseEntity<?> getPhoto(@PathVariable(name = "id") Long id) {
48-
return photoService.getPhoto(id);
51+
public ResponseEntity<PhotoResponse> getPhoto(@PathVariable(name = "id") Long id) {
52+
PhotoResponse photoResponse = photoService.getPhoto(id);
53+
54+
return new ResponseEntity<PhotoResponse>(photoResponse, HttpStatus.OK);
4955
}
5056

5157
@PutMapping("/{id}")
5258
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
53-
public ResponseEntity<?> updatePhoto(@PathVariable(name = "id") Long id,
59+
public ResponseEntity<PhotoResponse> updatePhoto(@PathVariable(name = "id") Long id,
5460
@Valid @RequestBody PhotoRequest photoRequest, @CurrentUser UserPrincipal currentUser) {
55-
return photoService.updatePhoto(id, photoRequest, currentUser);
61+
62+
PhotoResponse photoResponse = photoService.updatePhoto(id, photoRequest, currentUser);
63+
64+
return new ResponseEntity<PhotoResponse>(photoResponse, HttpStatus.OK);
5665
}
5766

5867
@DeleteMapping("/{id}")
5968
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
60-
public ResponseEntity<?> deletePhoto(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) {
61-
return photoService.deletePhoto(id, currentUser);
69+
public ResponseEntity<ApiResponse> deletePhoto(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) {
70+
ApiResponse apiResponse = photoService.deletePhoto(id, currentUser);
71+
72+
return new ResponseEntity<ApiResponse>(apiResponse, HttpStatus.OK);
6273
}
6374
}

src/main/java/com/sopromadze/blogapi/controller/PostController.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.validation.Valid;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
67
import org.springframework.http.ResponseEntity;
78
import org.springframework.security.access.prepost.PreAuthorize;
89
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -16,8 +17,10 @@
1617
import org.springframework.web.bind.annotation.RestController;
1718

1819
import com.sopromadze.blogapi.model.post.Post;
20+
import com.sopromadze.blogapi.payload.ApiResponse;
1921
import com.sopromadze.blogapi.payload.PagedResponse;
2022
import com.sopromadze.blogapi.payload.PostRequest;
23+
import com.sopromadze.blogapi.payload.PostResponse;
2124
import com.sopromadze.blogapi.security.CurrentUser;
2225
import com.sopromadze.blogapi.security.UserPrincipal;
2326
import com.sopromadze.blogapi.service.PostService;
@@ -30,50 +33,64 @@ public class PostController {
3033
private PostService postService;
3134

3235
@GetMapping
33-
public PagedResponse<Post> getAllPosts(
36+
public ResponseEntity<PagedResponse<Post>> getAllPosts(
3437
@RequestParam(value = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
3538
@RequestParam(value = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {
36-
return postService.getAllPosts(page, size);
39+
PagedResponse<Post> response = postService.getAllPosts(page, size);
40+
41+
return new ResponseEntity<PagedResponse<Post>>(response, HttpStatus.OK);
3742
}
3843

3944
@GetMapping("/category/{id}")
40-
public PagedResponse<Post> getPostsByCategory(
45+
public ResponseEntity<PagedResponse<Post>> getPostsByCategory(
4146
@RequestParam(value = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
4247
@RequestParam(value = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size,
4348
@PathVariable(name = "id") Long id) {
44-
return postService.getPostsByCategory(id, page, size);
49+
PagedResponse<Post> response = postService.getPostsByCategory(id, page, size);
50+
51+
return new ResponseEntity<PagedResponse<Post>>(response, HttpStatus.OK);
4552
}
4653

4754
@GetMapping("/tag/{id}")
48-
public PagedResponse<Post> getPostsByTag(
55+
public ResponseEntity<PagedResponse<Post>> getPostsByTag(
4956
@RequestParam(value = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
5057
@RequestParam(value = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size,
5158
@PathVariable(name = "id") Long id) {
52-
return postService.getPostsByTag(id, page, size);
59+
PagedResponse<Post> response = postService.getPostsByTag(id, page, size);
60+
61+
return new ResponseEntity<PagedResponse<Post>>(response, HttpStatus.OK);
5362
}
5463

5564
@PostMapping
5665
@PreAuthorize("hasRole('USER')")
57-
public ResponseEntity<?> addPost(@Valid @RequestBody PostRequest postRequest,
66+
public ResponseEntity<PostResponse> addPost(@Valid @RequestBody PostRequest postRequest,
5867
@CurrentUser UserPrincipal currentUser) {
59-
return postService.addPost(postRequest, currentUser);
68+
PostResponse postResponse = postService.addPost(postRequest, currentUser);
69+
70+
return new ResponseEntity<PostResponse>(postResponse, HttpStatus.CREATED);
6071
}
6172

6273
@GetMapping("/{id}")
63-
public ResponseEntity<?> getPost(@PathVariable(name = "id") Long id) {
64-
return postService.getPost(id);
74+
public ResponseEntity<Post> getPost(@PathVariable(name = "id") Long id) {
75+
Post post = postService.getPost(id);
76+
77+
return new ResponseEntity<Post>(post, HttpStatus.OK);
6578
}
6679

6780
@PutMapping("/{id}")
6881
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
69-
public ResponseEntity<?> updatePost(@PathVariable(name = "id") Long id,
82+
public ResponseEntity<Post> updatePost(@PathVariable(name = "id") Long id,
7083
@Valid @RequestBody PostRequest newPostRequest, @CurrentUser UserPrincipal currentUser) {
71-
return postService.updatePost(id, newPostRequest, currentUser);
84+
Post post = postService.updatePost(id, newPostRequest, currentUser);
85+
86+
return new ResponseEntity<Post>(post, HttpStatus.OK);
7287
}
7388

7489
@DeleteMapping("/{id}")
7590
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
76-
public ResponseEntity<?> deletePost(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) {
77-
return postService.deletePost(id, currentUser);
91+
public ResponseEntity<ApiResponse> deletePost(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) {
92+
ApiResponse apiResponse = postService.deletePost(id, currentUser);
93+
94+
return new ResponseEntity<ApiResponse>(apiResponse, HttpStatus.OK);
7895
}
7996
}

src/main/java/com/sopromadze/blogapi/controller/TagController.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.validation.Valid;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
67
import org.springframework.http.ResponseEntity;
78
import org.springframework.security.access.prepost.PreAuthorize;
89
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -16,6 +17,7 @@
1617
import org.springframework.web.bind.annotation.RestController;
1718

1819
import com.sopromadze.blogapi.model.tag.Tag;
20+
import com.sopromadze.blogapi.payload.ApiResponse;
1921
import com.sopromadze.blogapi.payload.PagedResponse;
2022
import com.sopromadze.blogapi.security.CurrentUser;
2123
import com.sopromadze.blogapi.security.UserPrincipal;
@@ -29,33 +31,45 @@ public class TagController {
2931
private TagService tagService;
3032

3133
@GetMapping
32-
public PagedResponse<?> getAllTags(
34+
public ResponseEntity<PagedResponse<Tag>> getAllTags(
3335
@RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
34-
@RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size){
35-
return tagService.getAllTags(page, size);
36+
@RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {
37+
38+
PagedResponse<Tag> response = tagService.getAllTags(page, size);
39+
40+
return new ResponseEntity<PagedResponse<Tag>>(response, HttpStatus.OK);
3641
}
3742

3843
@PostMapping
3944
@PreAuthorize("hasRole('USER')")
40-
public ResponseEntity<?> addTag(@Valid @RequestBody Tag tag, @CurrentUser UserPrincipal currentUser){
41-
return tagService.addTag(tag, currentUser);
45+
public ResponseEntity<Tag> addTag(@Valid @RequestBody Tag tag, @CurrentUser UserPrincipal currentUser){
46+
Tag newTag = tagService.addTag(tag, currentUser);
47+
48+
return new ResponseEntity<Tag>(newTag, HttpStatus.CREATED);
4249
}
4350

4451
@GetMapping("/{id}")
45-
public ResponseEntity<?> getTag(@PathVariable(name = "id") Long id){
46-
return tagService.getTag(id);
52+
public ResponseEntity<Tag> getTag(@PathVariable(name = "id") Long id){
53+
Tag tag = tagService.getTag(id);
54+
55+
return new ResponseEntity<Tag>(tag, HttpStatus.OK);
4756
}
4857

4958
@PutMapping("/{id}")
5059
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
51-
public ResponseEntity<?> updateTag(@PathVariable(name = "id") Long id, @Valid @RequestBody Tag tag, @CurrentUser UserPrincipal currentUser){
52-
return tagService.updateTag(id, tag, currentUser);
60+
public ResponseEntity<Tag> updateTag(@PathVariable(name = "id") Long id, @Valid @RequestBody Tag tag, @CurrentUser UserPrincipal currentUser){
61+
62+
Tag updatedTag = tagService.updateTag(id, tag, currentUser);
63+
64+
return new ResponseEntity<Tag>(updatedTag, HttpStatus.OK);
5365
}
5466

5567
@DeleteMapping("/{id}")
5668
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
57-
public ResponseEntity<?> deleteTag(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser){
58-
return tagService.deleteTag(id, currentUser);
69+
public ResponseEntity<ApiResponse> deleteTag(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser){
70+
ApiResponse apiResponse = tagService.deleteTag(id, currentUser);
71+
72+
return new ResponseEntity<ApiResponse>(apiResponse, HttpStatus.OK);
5973
}
6074

6175
}

0 commit comments

Comments
 (0)