|
1 | 1 | package com.sopromadze.blogapi.controller; |
2 | 2 |
|
| 3 | +import javax.validation.Valid; |
| 4 | + |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.http.ResponseEntity; |
| 7 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 8 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 9 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 10 | +import org.springframework.web.bind.annotation.GetMapping; |
| 11 | +import org.springframework.web.bind.annotation.PathVariable; |
| 12 | +import org.springframework.web.bind.annotation.PostMapping; |
| 13 | +import org.springframework.web.bind.annotation.PutMapping; |
| 14 | +import org.springframework.web.bind.annotation.RequestBody; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestParam; |
| 17 | +import org.springframework.web.bind.annotation.RestController; |
| 18 | + |
| 19 | +import com.sopromadze.blogapi.exception.ResponseEntityErrorException; |
3 | 20 | import com.sopromadze.blogapi.model.album.Album; |
| 21 | +import com.sopromadze.blogapi.payload.AlbumResponse; |
| 22 | +import com.sopromadze.blogapi.payload.ApiResponse; |
4 | 23 | import com.sopromadze.blogapi.payload.PagedResponse; |
| 24 | +import com.sopromadze.blogapi.payload.request.AlbumRequest; |
5 | 25 | import com.sopromadze.blogapi.security.CurrentUser; |
6 | 26 | import com.sopromadze.blogapi.security.UserPrincipal; |
7 | 27 | import com.sopromadze.blogapi.service.AlbumService; |
8 | 28 | import com.sopromadze.blogapi.service.PhotoService; |
9 | | -import com.sopromadze.blogapi.util.AppConstants; |
10 | | -import org.springframework.beans.factory.annotation.Autowired; |
11 | | -import org.springframework.http.ResponseEntity; |
12 | | -import org.springframework.security.access.prepost.PreAuthorize; |
13 | | -import org.springframework.web.bind.annotation.*; |
14 | | - |
15 | | -import javax.validation.Valid; |
| 29 | +import com.sopromadze.blogapi.utils.AppConstants; |
16 | 30 |
|
17 | 31 | @RestController |
18 | 32 | @RequestMapping("/api/albums") |
19 | 33 | public class AlbumController { |
20 | 34 | @Autowired |
21 | | - private AlbumService albumService; |
22 | | - |
| 35 | + private AlbumService albumService; |
| 36 | + |
23 | 37 | @Autowired |
24 | | - private PhotoService photoService; |
| 38 | + private PhotoService photoService; |
| 39 | + |
| 40 | + @ExceptionHandler(ResponseEntityErrorException.class) |
| 41 | + public ResponseEntity<ApiResponse> handleExceptions(ResponseEntityErrorException exception) { |
| 42 | + return exception.getApiResponse(); |
| 43 | + } |
25 | 44 |
|
26 | | - @GetMapping |
27 | | - public PagedResponse<Album> getAllAlbums( |
28 | | - @RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page, |
29 | | - @RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size){ |
30 | | - return albumService.getAllAlbums(page, size); |
31 | | - } |
| 45 | + @GetMapping |
| 46 | + public PagedResponse<AlbumResponse> getAllAlbums( |
| 47 | + @RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page, |
| 48 | + @RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) { |
| 49 | + |
| 50 | + return albumService.getAllAlbums(page, size); |
| 51 | + } |
32 | 52 |
|
33 | | - @PostMapping |
34 | | - @PreAuthorize("hasRole('USER')") |
35 | | - public ResponseEntity<?> addAlbum(@Valid @RequestBody Album album, @CurrentUser UserPrincipal currentUser){ |
36 | | - return albumService.addAlbum(album, currentUser); |
37 | | - } |
| 53 | + @PostMapping |
| 54 | + @PreAuthorize("hasRole('USER')") |
| 55 | + public ResponseEntity<Album> addAlbum(@Valid @RequestBody AlbumRequest albumRequest, @CurrentUser UserPrincipal currentUser) { |
| 56 | + return albumService.addAlbum(albumRequest, currentUser); |
| 57 | + } |
38 | 58 |
|
39 | | - @GetMapping("/{id}") |
40 | | - public ResponseEntity<?> getAlbum(@PathVariable(name = "id") Long id){ |
41 | | - return albumService.getAlbum(id); |
42 | | - } |
| 59 | + @GetMapping("/{id}") |
| 60 | + public ResponseEntity<?> getAlbum(@PathVariable(name = "id") Long id) { |
| 61 | + return albumService.getAlbum(id); |
| 62 | + } |
43 | 63 |
|
44 | | - @PutMapping("/{id}") |
45 | | - @PreAuthorize("hasRole('USER') or hasRole('ADMIN')") |
46 | | - public ResponseEntity<?> updateAlbum(@PathVariable(name = "id") Long id, @Valid @RequestBody Album newAlbum, @CurrentUser UserPrincipal currentUser){ |
47 | | - return albumService.updateAlbum(id, newAlbum, currentUser); |
48 | | - } |
| 64 | + @PutMapping("/{id}") |
| 65 | + @PreAuthorize("hasRole('USER') or hasRole('ADMIN')") |
| 66 | + public ResponseEntity<AlbumResponse> updateAlbum(@PathVariable(name = "id") Long id, @Valid @RequestBody AlbumRequest newAlbum, |
| 67 | + @CurrentUser UserPrincipal currentUser) { |
| 68 | + return albumService.updateAlbum(id, newAlbum, currentUser); |
| 69 | + } |
49 | 70 |
|
50 | | - @DeleteMapping("/{id}") |
51 | | - @PreAuthorize("hasRole('USER') or hasRole('ADMIN')") |
52 | | - public ResponseEntity<?> deleteAlbum(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser){ |
53 | | - return albumService.deleteAlbum(id, currentUser); |
54 | | - } |
| 71 | + @DeleteMapping("/{id}") |
| 72 | + @PreAuthorize("hasRole('USER') or hasRole('ADMIN')") |
| 73 | + public ResponseEntity<?> deleteAlbum(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) { |
| 74 | + return albumService.deleteAlbum(id, currentUser); |
| 75 | + } |
55 | 76 |
|
56 | | - @GetMapping("/{id}/photos") |
57 | | - public PagedResponse<?> getAllPhotosByAlbum( |
58 | | - @PathVariable(name = "id") Long id, |
59 | | - @RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page, |
60 | | - @RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size){ |
61 | | - return photoService.getAllPhotosByAlbum(id, page, size); |
62 | | - } |
| 77 | + @GetMapping("/{id}/photos") |
| 78 | + public PagedResponse<?> getAllPhotosByAlbum(@PathVariable(name = "id") Long id, |
| 79 | + @RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page, |
| 80 | + @RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) { |
| 81 | + return photoService.getAllPhotosByAlbum(id, page, size); |
| 82 | + } |
63 | 83 |
|
64 | 84 | } |
0 commit comments