|
1
|
|
|
package com.dawn.jat.illuminati.post.controller; |
|
2
|
|
|
|
|
3
|
|
|
import com.dawn.jat.illuminati.core.response.SuccessResponse; |
|
4
|
|
|
import com.dawn.jat.illuminati.post.dto.PostDto; |
|
5
|
|
|
import com.dawn.jat.illuminati.post.entity.PostEntity; |
|
6
|
|
|
import com.dawn.jat.illuminati.post.entity.PostSummaryEntity; |
|
7
|
|
|
import com.dawn.jat.illuminati.post.exception.PostCannotBeSavedException; |
|
8
|
|
|
import com.dawn.jat.illuminati.post.exception.PostNotFoundException; |
|
9
|
|
|
import com.dawn.jat.illuminati.post.exception.PostSummaryNotFoundException; |
|
10
|
|
|
import com.dawn.jat.illuminati.post.service.PostService; |
|
11
|
|
|
import java.util.List; |
|
12
|
|
|
import java.util.Optional; |
|
13
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
14
|
|
|
import org.springframework.http.HttpStatus; |
|
15
|
|
|
import org.springframework.http.ResponseEntity; |
|
16
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
17
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
|
18
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
19
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
20
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
21
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
@RestController |
|
25
|
|
|
@RequestMapping({"/api/post"}) |
|
26
|
|
|
public class PostController { |
|
27
|
|
|
@Autowired |
|
28
|
|
|
private PostService postService; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Gets a Post by the slug. |
|
32
|
|
|
*/ |
|
33
|
|
|
@GetMapping(value = "/{slug}") |
|
34
|
|
|
public ResponseEntity<Object> getPostBySlug(@PathVariable("slug") String slug) { |
|
35
|
|
|
Optional<PostEntity> postEntity = postService.findBySlug(slug); |
|
36
|
|
|
|
|
37
|
|
|
if (!postEntity.isPresent()) { |
|
38
|
|
|
throw new PostNotFoundException("Cannot find post"); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
SuccessResponse resp = new SuccessResponse(postEntity); |
|
42
|
|
|
return new ResponseEntity<>(resp, HttpStatus.OK); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Gets list of Post Summary. |
|
47
|
|
|
* @throws PostSummaryNotFoundException if Post Summary has No Content |
|
48
|
|
|
*/ |
|
49
|
|
|
@GetMapping(value = "/all-post/summary") |
|
50
|
|
|
public ResponseEntity<Object> getAllPostSummary() { |
|
51
|
|
|
List<PostSummaryEntity> allPost = postService.findPostSummary(); |
|
52
|
|
|
|
|
53
|
|
|
if (allPost.isEmpty()) { |
|
54
|
|
|
throw new PostSummaryNotFoundException("Cannot find post summary"); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
SuccessResponse resp = new SuccessResponse(allPost); |
|
58
|
|
|
return new ResponseEntity<>(resp, HttpStatus.OK); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create a Post in to database. |
|
63
|
|
|
*/ |
|
64
|
|
|
@PostMapping(value = "/create") |
|
65
|
|
|
public ResponseEntity<Object> createPost(@RequestBody PostDto post) { |
|
66
|
|
|
PostEntity savedPost = postService.create(post); |
|
67
|
|
|
SuccessResponse resp = new SuccessResponse(savedPost); |
|
68
|
|
|
return new ResponseEntity<>(resp, HttpStatus.OK); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Save an edited Post in to database. |
|
73
|
|
|
* @throws PostCannotBeSavedException if post cannot be saved |
|
74
|
|
|
*/ |
|
75
|
|
|
@PostMapping(value = "/save") |
|
76
|
|
|
public ResponseEntity<Object> savePost(@RequestBody PostDto post) { |
|
77
|
|
|
PostEntity savedPost = postService.save(post); |
|
78
|
|
|
if (savedPost == null) { |
|
79
|
|
|
throw new PostCannotBeSavedException("Failed to save Post"); |
|
80
|
|
|
} |
|
81
|
|
|
SuccessResponse resp = new SuccessResponse(savedPost); |
|
82
|
|
|
return new ResponseEntity<>(resp, HttpStatus.OK); |
|
83
|
|
|
} |
|
84
|
|
|
} |