Description
It is difficult to imagine a web application without the database. Imagine that every time you quit the program, all the data just disappears into thin air: no one wants that to happen! In this stage, we will make sure no data gets lost and store all the uploaded code snippets in the database for further reference.
We recommend you use the H2 database in the disk-based storage mode, not in-memory. Here are the dependencies you need for this database:
runtimeOnly 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
Use the following configuration in your project:
spring.datasource.url=jdbc:h2:file:../snippets
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
Notice how every code snippet is just black. It can be quite hard to read without syntax highlighting. Luckily, there is a library called highlight.js that can help you fix this. See the usage section to understand how to use it. Note that in order for it to work, your code snippets should be inside the tags <pre><code> ... </code></pre>, so make sure your code snippets are generated just like that. Put the following lines inside the <head> tags of your HTML, and viola: every code snippet is now highlighted!
<link rel="stylesheet"
href="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css">
<script src="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>Objectives
In this stage, your program should support all the same endpoints as in the previous stage. There are two differences: first, all the saved code snippets should be available after reloading the server, and second, they should be highlighted.
To recap, the 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 code snippet that can help you access it via the endpointGET /code/N.
GET /code/newshould be the same as in the second and third stages.
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 following examples, 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: