Added '/api/posts/raw' route, updated deps and fixed docs
bit / docker (push) Successful in 1m13s Details

This commit is contained in:
Marco Cetica 2024-03-06 10:16:14 +01:00
parent 8cf8c4c8db
commit 2ec05c4dfa
Signed by: marco
GPG Key ID: 45060A949E90D0FD
4 changed files with 48 additions and 4 deletions

View File

@ -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

View File

@ -5,12 +5,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ceticamarco</groupId>
<artifactId>bit</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<name>bit</name>
<description>Simple Web-based text sharing platform</description>
<properties>

View File

@ -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<String> 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
*

View File

@ -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();