Description
In this stage, you are going to use a specific tool to generate HTML: template engine. It will help you connect your Java content with the HTML content easily.
Use the following dependency in the build.gradle file to be able to use FreeMarker:
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
In this stage, you need to store all the code snippets that have been uploaded to the program. To get an individual snippet, you need to use GET /code/N and GET /api/code/N endpoints, where N is the code snippet number starting from 1. GET /code and GET /api/code endpoints should no longer be accessible. For now, store the code snippets in the memory: we will start storing them in a database later.
Your program should also be able to show the latest uploaded code snippets: use the endpoints GET /code/latest and GET /api/code/latest for this.
Objectives
In this stage, your program should support the following endpoints:
-
GET /api/code/Nshould return JSON with the N-th uploaded code snippet.
-
GET /code/Nshould return HTML that contains the N-th uploaded code snippet.
-
POST /api/code/newshould take a JSON object with a single fieldcode, use it as the current code snippet, and return JSON with a single fieldid. ID is the unique number of the snippet that helps you access it via the endpointGET /code/N.
-
GET /code/newshould be the same as in the previous stage.
-
GET /api/code/latestshould return a JSON array with 10 most recently uploaded code snippets sorted from the newest to the oldest.
-
GET /code/latestshould return HTML that contains 10 most recently uploaded code snippets. Use the titleLatestfor this page.
Examples
In the examples below, consider that no code snippets have been uploaded beforehand.
Example 1
Request POST /api/code/new with the following body:
{ "code": "class Code { ..." }
Response: { "id" : "1" }.
Another request POST /api/code/new with the following body:
{ "code": "public static void ..." }
Response: { "id" : "2" }.
Example 2
Request: GET /api/code/1
Response:
{
"code": "class Code { ...",
"date": "2020/05/05 11:59:12"
}
Example 3
Request: GET /api/code/2
Response:
{
"code": "public static void ...",
"date": "2020/05/05 12:00:43"
}
Example 4
Request: GET /api/code/latest
Response:
[
{
"code": "public static void ...",
"date": "2020/05/05 12:00:43"
},
{
"code": "class Code { ...",
"date": "2020/05/05 11:59:12"
}
]
Example 5
Request: GET /code/latest
Response: