-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathPostController.java
More file actions
56 lines (46 loc) · 2.22 KB
/
Copy pathPostController.java
File metadata and controls
56 lines (46 loc) · 2.22 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
package com.booleanuk.api.controller;
import com.booleanuk.api.model.Post;
import com.booleanuk.api.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
@RestController
@RequestMapping("posts")
public class PostController {
@Autowired
private PostRepository postRepository;
@PostMapping
public ResponseEntity<Post> createPost(@RequestBody Post post) {
if (post == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(this.postRepository.save(post), HttpStatus.CREATED);
}
@GetMapping
public List<Post> getAllPosts() {
return this.postRepository.findAll();
}
@GetMapping("/{id}")
public ResponseEntity<Post> getPost(@PathVariable int id) {
return new ResponseEntity<>(this.postRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,"No post with that id were found")), HttpStatus.OK);
}
@PutMapping("/{id}")
public ResponseEntity<Post> updatePost(@PathVariable int id, @RequestBody Post post) {
if (post == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not update the post, please check all required fields are correct");
}
Post updatePost = this.postRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,"No post with that id were found "));
updatePost.setTitle(post.getTitle());
updatePost.setDescription(post.getDescription());
return new ResponseEntity<>(this.postRepository.save(updatePost), HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
public ResponseEntity<Post> deletePost(@PathVariable int id) {
Post deletedPost = this.postRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No post with that id were found"));
this.postRepository.delete(deletedPost);
return ResponseEntity.ok(deletedPost);
}
}