Browse Source

update hse begems editor
- adjust WebController add post method for save
- enable spring data
- add model entity and repo

Alfi Ramdhani 1 year ago
parent
commit
1249553494

+ 4 - 4
pom.xml

@@ -15,10 +15,10 @@
15 15
 	<description>HSE Begems Editor</description>
16 16
 	
17 17
 	<dependencies>
18
-		<!--<dependency>
18
+		<dependency>
19 19
 			<groupId>org.springframework.boot</groupId>
20 20
 			<artifactId>spring-boot-starter-data-jpa</artifactId>
21
-		</dependency>-->
21
+		</dependency>
22 22
 		<dependency>
23 23
 			<groupId>org.springframework.boot</groupId>
24 24
 			<artifactId>spring-boot-starter-thymeleaf</artifactId>
@@ -28,11 +28,11 @@
28 28
 			<artifactId>spring-boot-starter-web</artifactId>
29 29
 		</dependency>
30 30
 		<!-- JDBC -->
31
-		<!--<dependency>
31
+		<dependency>
32 32
 			<groupId>mysql</groupId>
33 33
 			<artifactId>mysql-connector-java</artifactId>
34 34
 			<scope>runtime</scope>
35
-		</dependency>-->
35
+		</dependency>
36 36
 		<dependency>
37 37
 			<groupId>org.projectlombok</groupId>
38 38
 			<artifactId>lombok</artifactId>

+ 22 - 1
src/main/java/com/fusi24/hse/begems/editor/controller/WebController.java

@@ -1,17 +1,38 @@
1 1
 package com.fusi24.hse.begems.editor.controller;
2 2
 
3
+import org.springframework.beans.factory.annotation.Autowired;
3 4
 import org.springframework.stereotype.Controller;
5
+import org.springframework.ui.Model;
4 6
 import org.springframework.web.bind.annotation.GetMapping;
7
+import org.springframework.web.bind.annotation.ModelAttribute;
8
+import org.springframework.web.bind.annotation.PostMapping;
9
+import org.springframework.web.bind.annotation.RequestParam;
5 10
 import org.springframework.web.servlet.ModelAndView;
6 11
 
12
+import com.fusi24.hse.begems.editor.model.BegemsDocumentContent;
13
+import com.fusi24.hse.begems.editor.model.BegemsDocumentContentRepo;
14
+
7 15
 @Controller
8 16
 public class WebController {
9 17
 
18
+	@Autowired
19
+	private BegemsDocumentContentRepo begemsDocumentContentRepo;
20
+	
10 21
 	@GetMapping
11
-	public ModelAndView index() {
22
+	public ModelAndView index(@RequestParam String key) {
23
+		BegemsDocumentContent documentContent = begemsDocumentContentRepo.findFirstByGeneratedId(key).orElse(null);
24
+		if(documentContent == null) throw new RuntimeException("Key is required.");
12 25
 		ModelAndView modelAndView = new ModelAndView("index");
13 26
 		modelAndView.addObject("title", "HSE Editor");
14 27
 		modelAndView.addObject("heading", "HSE BeGeMS Document Editor");
28
+		modelAndView.addObject("documentContent", documentContent);
29
+		modelAndView.addObject("content", documentContent.getValue());
15 30
 		return modelAndView;
16 31
 	}
32
+	
33
+	@PostMapping
34
+	public String saveContent(Model model, @ModelAttribute BegemsDocumentContent data) {
35
+		begemsDocumentContentRepo.save(data);
36
+		return "redirect:/?key="+data.getGeneratedId();
37
+	}
17 38
 }

+ 29 - 0
src/main/java/com/fusi24/hse/begems/editor/model/BegemsDocumentContent.java

@@ -0,0 +1,29 @@
1
+package com.fusi24.hse.begems.editor.model;
2
+
3
+import java.math.BigInteger;
4
+
5
+import javax.persistence.Column;
6
+import javax.persistence.Entity;
7
+import javax.persistence.GeneratedValue;
8
+import javax.persistence.GenerationType;
9
+import javax.persistence.Id;
10
+import javax.persistence.Table;
11
+
12
+import lombok.Getter;
13
+import lombok.Setter;
14
+
15
+@Getter @Setter
16
+@Entity
17
+@Table(name = "begems_document_content", catalog = "bcbeats")
18
+public class BegemsDocumentContent {
19
+
20
+	@Id
21
+	@GeneratedValue(strategy = GenerationType.IDENTITY)
22
+	private BigInteger id;
23
+	
24
+	@Column(name = "gen_id")
25
+	private String generatedId;
26
+	
27
+	private String value;
28
+	
29
+}

+ 11 - 0
src/main/java/com/fusi24/hse/begems/editor/model/BegemsDocumentContentRepo.java

@@ -0,0 +1,11 @@
1
+package com.fusi24.hse.begems.editor.model;
2
+
3
+import java.math.BigInteger;
4
+import java.util.Optional;
5
+
6
+import org.springframework.data.repository.CrudRepository;
7
+
8
+public interface BegemsDocumentContentRepo extends CrudRepository<BegemsDocumentContent, BigInteger> {
9
+
10
+	public Optional<BegemsDocumentContent> findFirstByGeneratedId(String key);
11
+}

+ 31 - 10
src/main/resources/templates/index.html

@@ -3,7 +3,19 @@
3 3
 <head>
4 4
     <meta charset="utf-8">
5 5
     <title th:text="${title}"></title>
6
+    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
7
+    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
8
+    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
6 9
     <style>
10
+    
11
+    .btn-submit{
12
+    	margin-top: 15px;
13
+    	display:flex; 
14
+    	justify-content:flex-end; 
15
+    	width:100%; 
16
+    	padding-right:15px;
17
+    }
18
+    
7 19
     .document-editor {
8 20
         border: 1px solid var(--ck-color-base-border);
9 21
         border-radius: var(--ck-border-radius);
@@ -178,15 +190,24 @@
178 190
 <body>
179 191
 
180 192
 	<h1><span th:text="${heading}"></span></h1>
181
-
182
-   <div class="document-editor">
183
-    <div class="document-editor__toolbar"></div>
184
-    <div class="document-editor__editable-container">
185
-        <div class="document-editor__editable">
186
-            Editor content is inserted here.
187
-        </div>
188
-    </div>
189
-    </div>
193
+	<form method="post", th:action="@{/}" th:object="${documentContent}">
194
+		<input type="hidden" th:field="*{id}">
195
+		<input type="hidden" th:field="*{generatedId}">
196
+		<div class="document-editor">
197
+		    <div class="document-editor__toolbar"></div>
198
+		    <div class="document-editor__editable-container">
199
+		        <textarea class="document-editor__editable" th:field="*{value}">
200
+		            <!-- Editor content is inserted here. -->
201
+		            <span th:text="${content}"></span>
202
+		        </textarea>
203
+		    </div>
204
+	    </div>
205
+	    <div class="btn-submit">
206
+	    	<input type="submit" class="btn btn-default btn-lg" value="Submit">
207
+	    </div>
208
+	</form>
209
+   
210
+    
190 211
 
191 212
 <!--
192 213
 	https://ckeditor.com/docs/ckeditor5/latest/features/images/image-upload/ckbox.html
@@ -297,7 +318,7 @@
297 318
                         '@apple', '@bears', '@brownie', '@cake', '@cake', '@candy', '@canes', '@chocolate', '@cookie', '@cotton', '@cream',
298 319
                         '@cupcake', '@danish', '@donut', '@dragée', '@fruitcake', '@gingerbread', '@gummi', '@ice', '@jelly-o',
299 320
                         '@liquorice', '@macaroon', '@marzipan', '@oat', '@pie', '@plum', '@pudding', '@sesame', '@snaps', '@soufflé',
300
-                        '@sugar', '@sweet', '@topping', '@wafer'
321
+                        '@sugar', '@sweet', '@topping', '@wafer', '@alfi'
301 322
                     ],
302 323
                     minimumCharacters: 1
303 324
                 }