From 2ec05c4dfa936365fb3a6e9183fd6bcbe53eccd1 Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Wed, 6 Mar 2024 10:16:14 +0100 Subject: [PATCH] Added '/api/posts/raw' route, updated deps and fixed docs --- README.md | 10 +++++++++- pom.xml | 4 ++-- .../ceticamarco/bits/post/PostController.java | 19 +++++++++++++++++++ .../ceticamarco/bits/PostControllerTests.java | 19 ++++++++++++++++++- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9ded315..71d4a30 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,10 @@ _Parameters_: **email**(`string`), **password**(`string`). _Description_: Search a post by its ID. _Parameters_: none. +### `GET` Post Content By ID(`/api/posts/raw/{postId}`): +_Description_: Retrieve post content by its ID. +_Parameters_: none. + ### `GET` Post By Title(`/api/posts/bytitle`): **(special endpoint)** _Description_: Search a post by its title. @@ -142,7 +146,7 @@ _Parameters_: **user**(`User`). ## Examples Below there are some practical examples on how to use the REST API: -1. **Add a non-anonymous, perpetual post**(_note: the user must exist_) +1. **Add a non-anonymous**(_note: the user must exist_) `POST` request to `/api/posts/new` with the following body: ```json @@ -193,6 +197,10 @@ Below there are some practical examples on how to use the REST API: In this case user `john@example.com` is a user of the class `PRIVILEGED`. +5. **Get content of post "`af4598c`"** + +`GET` request to `/api/posts/raw/af4598c` with empty body. + ## Unit tests The **bit** platform provides some unit tests for the _post_ and the _user_ controllers. You can find them in `src/test`. The unit tests are automatically executed during the container bootstrap diff --git a/pom.xml b/pom.xml index 410475c..8043fa1 100644 --- a/pom.xml +++ b/pom.xml @@ -5,12 +5,12 @@ org.springframework.boot spring-boot-starter-parent - 3.2.1 + 3.2.3 com.ceticamarco bit - 0.0.1 + 0.0.2 bit Simple Web-based text sharing platform diff --git a/src/main/java/com/ceticamarco/bits/post/PostController.java b/src/main/java/com/ceticamarco/bits/post/PostController.java index 29c7d89..971c8b4 100644 --- a/src/main/java/com/ceticamarco/bits/post/PostController.java +++ b/src/main/java/com/ceticamarco/bits/post/PostController.java @@ -60,6 +60,25 @@ public class PostController { return new ResponseEntity<>(jsonOutput, HttpStatus.OK); } + /** + * Get post content by ID + * + * @param postId the ID of the requested post + * @return the content of the post(raw) + */ + @GetMapping("/api/posts/raw/{postId}") + public ResponseEntity getPostContentById(@PathVariable("postId") String postId) { + var res = postService.getPostById(postId); + if(res.isLeft()) { + throw new GenericErrorException(res.getLeft().getMessage(), "error"); + } + + var content = res.get().getContent(); + var jsonOutput = new JsonEmitter<>(content).emitJsonKey(); + + return new ResponseEntity<>(jsonOutput, HttpStatus.OK); + } + /** * Get posts by title if user is PRIVILEGED * diff --git a/src/test/java/com/ceticamarco/bits/PostControllerTests.java b/src/test/java/com/ceticamarco/bits/PostControllerTests.java index b375ac3..1b7564a 100644 --- a/src/test/java/com/ceticamarco/bits/PostControllerTests.java +++ b/src/test/java/com/ceticamarco/bits/PostControllerTests.java @@ -62,7 +62,7 @@ public class PostControllerTests { post.setTitle("test"); post.setContent("This is a test"); - when(postService.getPostById(anyString())).thenReturn(Either.right(any(Post.class))); + when(postService.getPostById(anyString())).thenReturn(Either.right(post)); mockMvc.perform(MockMvcRequestBuilders.get("/api/posts/abc123") .contentType(MediaType.APPLICATION_JSON) @@ -72,6 +72,23 @@ public class PostControllerTests { Mockito.verify(postService, Mockito.times(1)).getPostById(anyString()); } + @Test + public void getPostContentById() throws Exception { + var post = new Post(); + post.setId("abc123"); + post.setTitle("test"); + post.setContent("This is a test"); + + when(postService.getPostById(anyString())).thenReturn(Either.right(post)); + + mockMvc.perform(MockMvcRequestBuilders.get("/api/posts/raw/abc123") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(post))) + .andExpect(MockMvcResultMatchers.status().isOk()); + + Mockito.verify(postService, Mockito.times(1)).getPostById(anyString()); + } + @Test public void getPostByTitle() throws Exception { var post = new Post();