Skip to content

Commit 7883ef2

Browse files
Added the ability to post, store and get multiple snippets
1 parent 3c4ece1 commit 7883ef2

File tree

7 files changed

+86
-24
lines changed

7 files changed

+86
-24
lines changed

src/platform/controller/APIController.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,44 @@
44
import org.springframework.web.bind.annotation.*;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.http.ResponseEntity;
7+
import platform.model.entity.Code;
78
import platform.model.entity.CodeDTO;
8-
import platform.model.entity.NewCode;
9+
import platform.model.entity.Response;
910
import platform.service.CodeService;
1011

1112
import javax.validation.Valid;
13+
import javax.validation.constraints.Min;
14+
import java.util.List;
1215

1316
@RestController
1417
@RequestMapping(path = "/api")
1518
@Validated
1619
public class APIController {
1720
@Autowired
18-
CodeService codeService;
21+
private CodeService codeService;
1922

20-
@GetMapping(path = "/code")
23+
@GetMapping(path = "/code/{id}")
2124
@ResponseBody
22-
public ResponseEntity<CodeDTO> getCodeSnippet() {
25+
public ResponseEntity<Code> getCodeSnippetById(@PathVariable("id") @Min(1) long id) {
2326
return ResponseEntity.ok()
2427
.header("Content-Type", "application/json")
25-
.body(codeService.getSnippet());
28+
.body(codeService.getCodeById(id));
29+
}
30+
31+
@GetMapping(path = "/code/latest")
32+
@ResponseBody
33+
public ResponseEntity<List<Code>> getLatestCodeSnippets() {
34+
return ResponseEntity.ok()
35+
.header("Content-Type", "application/json")
36+
.body(codeService.getLatestCode());
2637
}
2738

2839
@PostMapping(path = "/code/new")
2940
@ResponseBody
30-
public ResponseEntity<String> updateCodeSnippet(@RequestBody @Valid NewCode newCode) {
31-
codeService.updateSnippet(newCode);
41+
public ResponseEntity<Response> putCodeSnippet(@RequestBody @Valid CodeDTO newCode) {
42+
long id = codeService.putCode(newCode);
3243
return ResponseEntity.ok()
33-
.body("{}");
44+
.header("Content-Type", "application/json")
45+
.body(new Response(id));
3446
}
3547
}

src/platform/controller/WebInterfaceController.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,31 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.ui.Model;
6+
import org.springframework.validation.annotation.Validated;
67
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
79
import platform.service.CodeService;
810

11+
import javax.validation.constraints.Min;
12+
913
@Controller
14+
@Validated
1015
public class WebInterfaceController {
1116
@Autowired
12-
CodeService codeService;
17+
private CodeService codeService;
1318

14-
@GetMapping(path = "/code")
15-
public String getCodeSnippet(Model model) {
16-
model.addAttribute("snippetCode", codeService.getSnippet().getCode())
17-
.addAttribute("snippetDate", codeService.getSnippet().getDate());
19+
@GetMapping(path = "/code/{id}")
20+
public String getCodeSnippetById(Model model, @PathVariable("id") @Min(1) long id) {
21+
model.addAttribute("code", codeService.getCodeById(id));
1822
return "codeSnippet";
1923
}
2024

25+
@GetMapping(path = "/code/latest")
26+
public String getCodeSnippetById(Model model) {
27+
model.addAttribute("latestCode", codeService.getLatestCode());
28+
return "codeLatest";
29+
}
30+
2131
@GetMapping(path = "/code/new")
2232
public String getNewCode(Model model) {
2333
return "codeNew";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package platform.model.entity;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class Response {
11+
private String id;
12+
13+
public Response(long id) {
14+
this.id = String.valueOf(id);
15+
}
16+
}

src/resources/static/styles/codeSnippetStyle.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
border: solid 1px black;
88
background-color: whitesmoke;
99
padding: 5px;
10-
margin: 0;
10+
margin: 0 0 10px;
1111
width: 500px;
1212
white-space: pre-wrap;
1313
white-space: -moz-pre-wrap;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
5+
<title>Latest</title>
6+
<link rel="stylesheet" th:href="@{/styles/codeSnippetStyle.css}">
7+
</head>
8+
<body>
9+
<div>
10+
<table>
11+
<!--/*@thymesVar id="latestCode" type="java.util.List<platform.model.entity.Code>"*/-->
12+
<tr th:if="${latestCode.isEmpty()}">
13+
<td colspan="2">No code snippets uploaded</td>
14+
</tr>
15+
<tr th:each="code : ${latestCode}">
16+
<!--/*@thymesVar id="code" type="platform.model.entity.Code"*/-->
17+
<span id="load_date" th:text="${code.date}">
18+
</span>
19+
<pre id="code_snippet" th:text="${code.code}">
20+
</pre>
21+
</tr>
22+
</table>
23+
</div>
24+
</body>
25+
</html>

src/resources/templates/codeNew.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<!DOCTYPE HTML>
22
<html xmlns:th="http://www.thymeleaf.org">
33
<head>
4-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
4+
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
55
<title>Create</title>
6-
<link type="text/css" th:href="@{/styles/codeNewStyle.css}" rel="stylesheet">
7-
<script type="text/javascript" th:src="@{/js/codeNewSend.js}"></script>
6+
<link rel="stylesheet" th:href="@{/styles/codeNewStyle.css}" type="text/css">
7+
<script th:src="@{/js/codeNewSend.js}" type="text/javascript"></script>
88
</head>
99
<body>
1010
<div>
1111
<textarea id="code_snippet" placeholder="// write your code here"></textarea>
12-
<button id="send_snippet" type="submit" th:onclick="send()">Submit</button>
12+
<button id="send_snippet" th:onclick="send()" type="submit">Submit</button>
1313
</div>
1414
</body>
1515
</html>

src/resources/templates/codeSnippet.html

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<!DOCTYPE HTML>
22
<html xmlns:th="http://www.thymeleaf.org">
33
<head>
4-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
4+
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
55
<title>Code</title>
6-
<link th:href="@{/styles/codeSnippetStyle.css}" rel="stylesheet">
6+
<link rel="stylesheet" th:href="@{/styles/codeSnippetStyle.css}">
77
</head>
88
<body>
99
<div>
10-
<!--/*@thymesVar id="snippetDate" type="platform"*/-->
11-
<span id="load_date" th:text="${snippetDate}">
10+
<!--/*@thymesVar id="code" type="platform.model.entity.Code"*/-->
11+
<span id="load_date" th:text="${code.date}">
1212
</span>
13-
<!--/*@thymesVar id="snippetCode" type="platform"*/-->
14-
<pre id="code_snippet" th:text="${snippetCode}">
13+
<pre id="code_snippet" th:text="${code.code}">
1514
</pre>
1615
</div>
1716
</body>

0 commit comments

Comments
 (0)