Skip to content

Commit 775d6d4

Browse files
Merge pull request #2 from apanashchenko/master
Add Post service create/update/delete
2 parents bc069db + 95e3b72 commit 775d6d4

File tree

6 files changed

+221
-0
lines changed

6 files changed

+221
-0
lines changed

src/main/java/com/example/timecoder/gateway/config/SecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ protected void configure(HttpSecurity http) throws Exception {
9090
.antMatchers("/gateway/auth/**", "/stream")
9191
.permitAll()
9292
.antMatchers("/free-theme").permitAll()
93+
.antMatchers(HttpMethod.GET, "/posts/**").permitAll()
9394
.antMatchers("/api/user/checkUsernameAvailability", "/api/user/checkEmailAvailability")
9495
.permitAll()
9596
.antMatchers(HttpMethod.GET, "/api/users/**")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.example.timecoder.gateway.controller;
2+
3+
import com.example.timecoder.gateway.model.Post;
4+
import com.example.timecoder.gateway.model.PostDto;
5+
import com.example.timecoder.gateway.proxy.TimecoderServiceProxy;
6+
import com.example.timecoder.gateway.service.PostService;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
import java.util.List;
11+
12+
13+
@RestController
14+
public class PostController {
15+
16+
@Autowired
17+
private TimecoderServiceProxy timecoderServiceProxy;
18+
19+
@Autowired
20+
private PostService postService;
21+
22+
@RequestMapping(value = "/posts", method = RequestMethod.GET)
23+
public List<Post> getAllPosts() {
24+
return timecoderServiceProxy.getAllPosts();
25+
}
26+
27+
@RequestMapping(value = "/posts/{id}", method = RequestMethod.GET)
28+
public PostDto getPostById(@PathVariable("id") Long id) {
29+
return postService.getPostById(id);
30+
}
31+
32+
@RequestMapping(value = "/posts", method = RequestMethod.POST)
33+
public Object createPost(@RequestBody Post post) {
34+
return timecoderServiceProxy.createPost(post);
35+
}
36+
37+
@RequestMapping(value = "/posts/{id}", method = RequestMethod.PUT)
38+
public Object updatePost(@PathVariable("id") Long id, @RequestBody Post post) {
39+
return timecoderServiceProxy.updatePost(id, post);
40+
}
41+
42+
@RequestMapping(value = "/posts/{id}", method = RequestMethod.DELETE)
43+
public Object deletePost(@PathVariable("id") Long id) {
44+
return timecoderServiceProxy.deletePost(id);
45+
}
46+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.example.timecoder.gateway.model;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
public class Post {
7+
8+
private long id;
9+
private long episodeId;
10+
private Date createdAt = new Date();
11+
private String name;
12+
private String shortDescription;
13+
private String description;
14+
private String link;
15+
private Object guests;
16+
17+
public long getId() {
18+
return id;
19+
}
20+
21+
public void setId(long id) {
22+
this.id = id;
23+
}
24+
25+
public long getEpisodeId() {
26+
return episodeId;
27+
}
28+
29+
public void setEpisodeId(long episodeId) {
30+
this.episodeId = episodeId;
31+
}
32+
33+
public Date getCreatedAt() {
34+
return createdAt;
35+
}
36+
37+
public void setCreatedAt(Date createdAt) {
38+
this.createdAt = createdAt;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
public String getShortDescription() {
50+
return shortDescription;
51+
}
52+
53+
public void setShortDescription(String shortDescription) {
54+
this.shortDescription = shortDescription;
55+
}
56+
57+
public String getDescription() {
58+
return description;
59+
}
60+
61+
public void setDescription(String description) {
62+
this.description = description;
63+
}
64+
65+
public String getLink() {
66+
return link;
67+
}
68+
69+
public void setLink(String link) {
70+
this.link = link;
71+
}
72+
73+
public Object getGuests() {
74+
return guests;
75+
}
76+
77+
public void setGuests(List<String> guests) {
78+
this.guests = guests;
79+
}
80+
81+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.timecoder.gateway.model;
2+
3+
4+
public class PostDto {
5+
6+
private Object post;
7+
private Object episode;
8+
private Object patrons;
9+
10+
public PostDto() {
11+
}
12+
13+
public PostDto(Object post, Object episode, Object patrons) {
14+
this.post = post;
15+
this.episode = episode;
16+
this.patrons = patrons;
17+
}
18+
19+
public Object getPost() {
20+
return post;
21+
}
22+
23+
public void setPost(Object post) {
24+
this.post = post;
25+
}
26+
27+
public Object getEpisode() {
28+
return episode;
29+
}
30+
31+
public void setEpisode(Object episode) {
32+
this.episode = episode;
33+
}
34+
35+
public Object getPatrons() {
36+
return patrons;
37+
}
38+
39+
public void setPatrons(Object patrons) {
40+
this.patrons = patrons;
41+
}
42+
}

src/main/java/com/example/timecoder/gateway/proxy/TimecoderServiceProxy.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.timecoder.gateway.proxy;
22

3+
import com.example.timecoder.gateway.model.Post;
34
import com.example.timecoder.gateway.payload.timecoder.EpisodePayload;
45
import com.example.timecoder.gateway.payload.timecoder.ThemePayload;
56
import org.springframework.cloud.openfeign.FeignClient;
@@ -47,4 +48,19 @@ public interface TimecoderServiceProxy {
4748

4849
@RequestMapping(value = "/theme/{id}/delete", method = RequestMethod.DELETE)
4950
Object deleteTheme(@RequestParam Long id);
51+
52+
@RequestMapping(value = "/posts", method = RequestMethod.GET)
53+
List<Post> getAllPosts();
54+
55+
@RequestMapping(value = "/posts/{id}", method = RequestMethod.GET)
56+
Post getPostById(@RequestParam Long id);
57+
58+
@RequestMapping(value = "/posts", method = RequestMethod.POST)
59+
Object createPost(Post post);
60+
61+
@RequestMapping(value = "/posts/{id}", method = RequestMethod.DELETE)
62+
Object deletePost(@RequestParam Long id);
63+
64+
@RequestMapping(value = "/posts/{id}", method = RequestMethod.PUT)
65+
Object updatePost(@RequestParam Long id, Post post);
5066
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.timecoder.gateway.service;
2+
3+
import com.example.timecoder.gateway.model.Post;
4+
import com.example.timecoder.gateway.model.PostDto;
5+
import com.example.timecoder.gateway.proxy.PatronsServiceProxy;
6+
import com.example.timecoder.gateway.proxy.TimecoderServiceProxy;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
10+
11+
@Service
12+
public class PostService {
13+
14+
@Autowired
15+
private TimecoderServiceProxy timecoderServiceProxy;
16+
17+
@Autowired
18+
private PatronsServiceProxy patronsServiceProxy;
19+
20+
21+
public PostDto getPostById(Long id) {
22+
Post post = null;
23+
Object episode = null;
24+
Object patronsList = null;
25+
try {
26+
post = timecoderServiceProxy.getPostById(id);
27+
episode = timecoderServiceProxy.getEpisodeById(post.getEpisodeId());
28+
patronsList = patronsServiceProxy.getAllActivePatrons();
29+
} catch (Exception e) {
30+
e.printStackTrace();
31+
}
32+
33+
return new PostDto(post, episode, patronsList);
34+
}
35+
}

0 commit comments

Comments
 (0)