forked from RameshMF/Spring-Boot-Blog-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoController.java
More file actions
73 lines (60 loc) · 2.98 KB
/
PhotoController.java
File metadata and controls
73 lines (60 loc) · 2.98 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
package com.sopromadze.blogapi.controller;
import com.sopromadze.blogapi.payload.ApiResponse;
import com.sopromadze.blogapi.payload.PagedResponse;
import com.sopromadze.blogapi.payload.PhotoRequest;
import com.sopromadze.blogapi.payload.PhotoResponse;
import com.sopromadze.blogapi.security.CurrentUser;
import com.sopromadze.blogapi.security.UserPrincipal;
import com.sopromadze.blogapi.service.PhotoService;
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/photos")
public class PhotoController {
@Autowired
private PhotoService photoService;
@GetMapping
public PagedResponse<PhotoResponse> getAllPhotos(
@RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
@RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {
return photoService.getAllPhotos(page, size);
}
@PostMapping
@PreAuthorize("hasRole('USER')")
public ResponseEntity<PhotoResponse> addPhoto(@Valid @RequestBody PhotoRequest photoRequest,
@CurrentUser UserPrincipal currentUser) {
PhotoResponse photoResponse = photoService.addPhoto(photoRequest, currentUser);
return new ResponseEntity< >(photoResponse, HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<PhotoResponse> getPhoto(@PathVariable(name = "id") Long id) {
PhotoResponse photoResponse = photoService.getPhoto(id);
return new ResponseEntity< >(photoResponse, HttpStatus.OK);
}
@PutMapping("/{id}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<PhotoResponse> updatePhoto(@PathVariable(name = "id") Long id,
@Valid @RequestBody PhotoRequest photoRequest, @CurrentUser UserPrincipal currentUser) {
PhotoResponse photoResponse = photoService.updatePhoto(id, photoRequest, currentUser);
return new ResponseEntity< >(photoResponse, HttpStatus.OK);
}
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public ResponseEntity<ApiResponse> deletePhoto(@PathVariable(name = "id") Long id, @CurrentUser UserPrincipal currentUser) {
ApiResponse apiResponse = photoService.deletePhoto(id, currentUser);
return new ResponseEntity< >(apiResponse, HttpStatus.OK);
}
}