diff --git a/src/main/java/com/example/timecoder/gateway/config/SecurityConfig.java b/src/main/java/com/example/timecoder/gateway/config/SecurityConfig.java index c59b85c..c2384a0 100644 --- a/src/main/java/com/example/timecoder/gateway/config/SecurityConfig.java +++ b/src/main/java/com/example/timecoder/gateway/config/SecurityConfig.java @@ -90,6 +90,7 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers("/gateway/auth/**", "/stream") .permitAll() .antMatchers("/free-theme").permitAll() + .antMatchers(HttpMethod.GET, "/posts/**").permitAll() .antMatchers("/api/user/checkUsernameAvailability", "/api/user/checkEmailAvailability") .permitAll() .antMatchers(HttpMethod.GET, "/api/users/**") diff --git a/src/main/java/com/example/timecoder/gateway/controller/PostController.java b/src/main/java/com/example/timecoder/gateway/controller/PostController.java new file mode 100644 index 0000000..83f7f57 --- /dev/null +++ b/src/main/java/com/example/timecoder/gateway/controller/PostController.java @@ -0,0 +1,46 @@ +package com.example.timecoder.gateway.controller; + +import com.example.timecoder.gateway.model.Post; +import com.example.timecoder.gateway.model.PostDto; +import com.example.timecoder.gateway.proxy.TimecoderServiceProxy; +import com.example.timecoder.gateway.service.PostService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + + +@RestController +public class PostController { + + @Autowired + private TimecoderServiceProxy timecoderServiceProxy; + + @Autowired + private PostService postService; + + @RequestMapping(value = "/posts", method = RequestMethod.GET) + public List getAllPosts() { + return timecoderServiceProxy.getAllPosts(); + } + + @RequestMapping(value = "/posts/{id}", method = RequestMethod.GET) + public PostDto getPostById(@PathVariable("id") Long id) { + return postService.getPostById(id); + } + + @RequestMapping(value = "/posts", method = RequestMethod.POST) + public Object createPost(@RequestBody Post post) { + return timecoderServiceProxy.createPost(post); + } + + @RequestMapping(value = "/posts/{id}", method = RequestMethod.PUT) + public Object updatePost(@PathVariable("id") Long id, @RequestBody Post post) { + return timecoderServiceProxy.updatePost(id, post); + } + + @RequestMapping(value = "/posts/{id}", method = RequestMethod.DELETE) + public Object deletePost(@PathVariable("id") Long id) { + return timecoderServiceProxy.deletePost(id); + } +} diff --git a/src/main/java/com/example/timecoder/gateway/model/Post.java b/src/main/java/com/example/timecoder/gateway/model/Post.java new file mode 100644 index 0000000..8db9dde --- /dev/null +++ b/src/main/java/com/example/timecoder/gateway/model/Post.java @@ -0,0 +1,81 @@ +package com.example.timecoder.gateway.model; + +import java.util.Date; +import java.util.List; + +public class Post { + + private long id; + private long episodeId; + private Date createdAt = new Date(); + private String name; + private String shortDescription; + private String description; + private String link; + private Object guests; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public long getEpisodeId() { + return episodeId; + } + + public void setEpisodeId(long episodeId) { + this.episodeId = episodeId; + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getShortDescription() { + return shortDescription; + } + + public void setShortDescription(String shortDescription) { + this.shortDescription = shortDescription; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public Object getGuests() { + return guests; + } + + public void setGuests(List guests) { + this.guests = guests; + } + +} diff --git a/src/main/java/com/example/timecoder/gateway/model/PostDto.java b/src/main/java/com/example/timecoder/gateway/model/PostDto.java new file mode 100644 index 0000000..bcb4237 --- /dev/null +++ b/src/main/java/com/example/timecoder/gateway/model/PostDto.java @@ -0,0 +1,42 @@ +package com.example.timecoder.gateway.model; + + +public class PostDto { + + private Object post; + private Object episode; + private Object patrons; + + public PostDto() { + } + + public PostDto(Object post, Object episode, Object patrons) { + this.post = post; + this.episode = episode; + this.patrons = patrons; + } + + public Object getPost() { + return post; + } + + public void setPost(Object post) { + this.post = post; + } + + public Object getEpisode() { + return episode; + } + + public void setEpisode(Object episode) { + this.episode = episode; + } + + public Object getPatrons() { + return patrons; + } + + public void setPatrons(Object patrons) { + this.patrons = patrons; + } +} diff --git a/src/main/java/com/example/timecoder/gateway/proxy/TimecoderServiceProxy.java b/src/main/java/com/example/timecoder/gateway/proxy/TimecoderServiceProxy.java index 32316e6..5febf37 100644 --- a/src/main/java/com/example/timecoder/gateway/proxy/TimecoderServiceProxy.java +++ b/src/main/java/com/example/timecoder/gateway/proxy/TimecoderServiceProxy.java @@ -1,5 +1,6 @@ package com.example.timecoder.gateway.proxy; +import com.example.timecoder.gateway.model.Post; import com.example.timecoder.gateway.payload.timecoder.EpisodePayload; import com.example.timecoder.gateway.payload.timecoder.ThemePayload; import org.springframework.cloud.openfeign.FeignClient; @@ -47,4 +48,19 @@ public interface TimecoderServiceProxy { @RequestMapping(value = "/theme/{id}/delete", method = RequestMethod.DELETE) Object deleteTheme(@RequestParam Long id); + + @RequestMapping(value = "/posts", method = RequestMethod.GET) + List getAllPosts(); + + @RequestMapping(value = "/posts/{id}", method = RequestMethod.GET) + Post getPostById(@RequestParam Long id); + + @RequestMapping(value = "/posts", method = RequestMethod.POST) + Object createPost(Post post); + + @RequestMapping(value = "/posts/{id}", method = RequestMethod.DELETE) + Object deletePost(@RequestParam Long id); + + @RequestMapping(value = "/posts/{id}", method = RequestMethod.PUT) + Object updatePost(@RequestParam Long id, Post post); } diff --git a/src/main/java/com/example/timecoder/gateway/service/PostService.java b/src/main/java/com/example/timecoder/gateway/service/PostService.java new file mode 100644 index 0000000..2a69f65 --- /dev/null +++ b/src/main/java/com/example/timecoder/gateway/service/PostService.java @@ -0,0 +1,35 @@ +package com.example.timecoder.gateway.service; + +import com.example.timecoder.gateway.model.Post; +import com.example.timecoder.gateway.model.PostDto; +import com.example.timecoder.gateway.proxy.PatronsServiceProxy; +import com.example.timecoder.gateway.proxy.TimecoderServiceProxy; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + + +@Service +public class PostService { + + @Autowired + private TimecoderServiceProxy timecoderServiceProxy; + + @Autowired + private PatronsServiceProxy patronsServiceProxy; + + + public PostDto getPostById(Long id) { + Post post = null; + Object episode = null; + Object patronsList = null; + try { + post = timecoderServiceProxy.getPostById(id); + episode = timecoderServiceProxy.getEpisodeById(post.getEpisodeId()); + patronsList = patronsServiceProxy.getAllActivePatrons(); + } catch (Exception e) { + e.printStackTrace(); + } + + return new PostDto(post, episode, patronsList); + } +}