Added '/api/posts/raw' route, updated deps and fixed docs
All checks were successful
bit / docker (push) Successful in 1m13s
All checks were successful
bit / docker (push) Successful in 1m13s
This commit is contained in:
parent
8cf8c4c8db
commit
2ec05c4dfa
10
README.md
10
README.md
@ -120,6 +120,10 @@ _Parameters_: **email**(`string`), **password**(`string`).
|
|||||||
_Description_: Search a post by its ID.
|
_Description_: Search a post by its ID.
|
||||||
_Parameters_: none.
|
_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`):
|
### `GET` Post By Title(`/api/posts/bytitle`):
|
||||||
**(special endpoint)**
|
**(special endpoint)**
|
||||||
_Description_: Search a post by its title.
|
_Description_: Search a post by its title.
|
||||||
@ -142,7 +146,7 @@ _Parameters_: **user**(`User`).
|
|||||||
## Examples
|
## Examples
|
||||||
Below there are some practical examples on how to use the REST API:
|
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:
|
`POST` request to `/api/posts/new` with the following body:
|
||||||
```json
|
```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`.
|
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
|
## Unit tests
|
||||||
The **bit** platform provides some unit tests for the _post_ and the _user_ controllers. You can
|
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
|
find them in `src/test`. The unit tests are automatically executed during the container bootstrap
|
||||||
|
4
pom.xml
4
pom.xml
@ -5,12 +5,12 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>3.2.1</version>
|
<version>3.2.3</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>com.ceticamarco</groupId>
|
<groupId>com.ceticamarco</groupId>
|
||||||
<artifactId>bit</artifactId>
|
<artifactId>bit</artifactId>
|
||||||
<version>0.0.1</version>
|
<version>0.0.2</version>
|
||||||
<name>bit</name>
|
<name>bit</name>
|
||||||
<description>Simple Web-based text sharing platform</description>
|
<description>Simple Web-based text sharing platform</description>
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -60,6 +60,25 @@ public class PostController {
|
|||||||
return new ResponseEntity<>(jsonOutput, HttpStatus.OK);
|
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
|
* Get posts by title if user is PRIVILEGED
|
||||||
*
|
*
|
||||||
|
@ -62,7 +62,7 @@ public class PostControllerTests {
|
|||||||
post.setTitle("test");
|
post.setTitle("test");
|
||||||
post.setContent("This is a 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")
|
mockMvc.perform(MockMvcRequestBuilders.get("/api/posts/abc123")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
@ -72,6 +72,23 @@ public class PostControllerTests {
|
|||||||
Mockito.verify(postService, Mockito.times(1)).getPostById(anyString());
|
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
|
@Test
|
||||||
public void getPostByTitle() throws Exception {
|
public void getPostByTitle() throws Exception {
|
||||||
var post = new Post();
|
var post = new Post();
|
||||||
|
Loading…
Reference in New Issue
Block a user