Skip to content

Commit

Permalink
✨添加识别码功能与展示页面
Browse files Browse the repository at this point in the history
  • Loading branch information
mewhz committed Nov 5, 2021
1 parent 6eab1c1 commit 52f6f1e
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 103 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
## 更新日志
>2021/10/28
>
>项目上线 v1.0 (whz)
>项目上线 v1.0 (whz) code.mewhz.com
>
>主页样式优化 v1.1 (zzp)
>
>2021/10/30
>
>修改数据库 v1.2 (whz)
>修改数据库 v1.2 (whz)
>
>2021/11/5
>
>添加识别码功能与展示页面 v1.3 (whz)
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- <plugin>&lt;!&ndash;编译跳过测试文件检查的生命周期&ndash;&gt;-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-surefire-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <skip>true</skip>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mewhz.paste.controller;

import cn.hutool.core.net.NetUtil;
import com.mewhz.paste.utils.IPUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/com/mewhz/paste/controller/ListController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.mewhz.paste.controller;

import cn.hutool.db.Entity;
import com.mewhz.paste.utils.CodeSQL;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
* @author mewhz
*/
@Controller
public class ListController {

@ResponseBody
@RequestMapping("/list")
public String list(@RequestParam String id){
String htmlBegin = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
" <link href=\"css/prism.css\" rel=\"stylesheet\">\n" +
"</head>\n" +
"<body>";
String summaryBegin = "<details>\n" +
" <summary>";
String summaryEnd = "</summary>\n" +
" <pre>\n" +
" <code class=\"language-";
String detailsEnd = " </code>\n" +
" </pre>\n" +
" </details>\n";
String htmlEnd = " <script src=\"js/prism.js\"></script>\n" +
" <script src=\"js/prism-c.min.js\"></script>\n" +
" <script src=\"js/prism-cpp.min.js\"></script>\n" +
" <script src=\"js/prism-java.min.js\"></script>\n" +
"</body>\n" +
"</html>";
StringBuilder html = new StringBuilder(htmlBegin);

CodeSQL codeSQL = new CodeSQL();

List<Entity> list = codeSQL.findIdentifyingCode(id);

for (Entity e : list){
html.append(summaryBegin);
html.append(e.get("date"));
html.append(" ");
html.append(e.get("time_id"));
html.append(summaryEnd);
html.append(e.get("type"));
html.append("\">");

String text = (String) e.get("text");

text = text.replaceAll("<", "&lt;");
text = text.replaceAll(">", "&gt;");
text = text.replaceAll("\n", "<br>");
text = text.replaceAll(" ", "&nbsp;");

html.append(text);
html.append(detailsEnd);
}
html.append(htmlEnd);

return html.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mewhz.paste.controller;

import com.mewhz.paste.model.Code;
import com.mewhz.paste.model.IdentifyingCode;
import com.mewhz.paste.utils.CodeSQL;
import com.mewhz.paste.utils.IPUtils;
import org.springframework.stereotype.Controller;
Expand All @@ -17,14 +18,16 @@ public class SubmitController {
@RequestMapping("/submit")
// @ResponseBody
@CrossOrigin
public String code(@RequestParam String text, @RequestParam String type, HttpServletRequest request){
public String code(@RequestParam String text, @RequestParam String type, @RequestParam String identifying ,HttpServletRequest request){
Date date = new Date();
String ip = IPUtils.getIpAddr(request);
String userAgent = request.getHeader("User-Agent");
Code code = new Code(text, type, date, ip, userAgent);
IdentifyingCode identifyingCode = new IdentifyingCode(identifying, date.getTime()+"");
System.out.println(code);
CodeSQL codeSql = new CodeSQL();
codeSql.insertCode(code);
codeSql.insertIdentifyingCode(identifyingCode);
return "redirect:/code?id=" + code.getDate().getTime();
}
}
89 changes: 0 additions & 89 deletions src/main/java/com/mewhz/paste/model/CodeHtml.java

This file was deleted.

62 changes: 62 additions & 0 deletions src/main/java/com/mewhz/paste/model/IdentifyingCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.mewhz.paste.model;

/**
* @author mewhz
*/
public class IdentifyingCode {
private int icId;
private String identifying;
private String timeId;

public IdentifyingCode(){

}

public IdentifyingCode(String identifying){
this.identifying = identifying;
}

public IdentifyingCode(String identifying, String timeId) {
this.identifying = identifying;
this.timeId = timeId;
}

public IdentifyingCode(int icId, String identifying, String timeId) {
this.icId = icId;
this.identifying = identifying;
this.timeId = timeId;
}

public int getIcId() {
return icId;
}

public void setIcId(int icId) {
this.icId = icId;
}

public String getIdentifying() {
return identifying;
}

public void setIdentifying(String identifying) {
this.identifying = identifying;
}

public String getTimeId() {
return timeId;
}

public void setTimeId(String timeId) {
this.timeId = timeId;
}

@Override
public String toString() {
return "IdentifyingCode{" +
"icId=" + icId +
", identifying='" + identifying + '\'' +
", timeId='" + timeId + '\'' +
'}';
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/mewhz/paste/utils/CodeSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.mewhz.paste.model.Code;
import com.mewhz.paste.model.IdentifyingCode;

import java.sql.SQLException;
import java.util.List;
Expand All @@ -11,6 +12,10 @@ public class CodeSQL {
public CodeSQL (){

}

/**
插入代码等信息到数据库
*/
public void insertCode(Code code){
try {
Db.use().insert(
Expand All @@ -27,6 +32,22 @@ public void insertCode(Code code){
}
}

/**
* 插入代码识别码等信息到数据库
*/
public void insertIdentifyingCode(IdentifyingCode identifyingCode){
try{
Db.use().insert(
Entity.create("identifying_code")
.set("identifying", identifyingCode.getIdentifying())
.set("time_id", identifyingCode.getTimeId())
);

} catch (SQLException e) {
e.printStackTrace();
}
}

public List<Entity> findCode(String timeId){
List<Entity> codes = null;
try {
Expand All @@ -38,4 +59,17 @@ public List<Entity> findCode(String timeId){
}
return codes;
}

public List<Entity> findIdentifyingCode(String identifying){
List<Entity> identifyingCodes = null;
try{
identifyingCodes = Db.use().query("select text, type, date, code.time_id\n" +
"from code inner join identifying_code\n" +
" on code.time_id = identifying_code.time_id\n" +
" where identifying = ?", identifying);
} catch (SQLException e) {
e.printStackTrace();
}
return identifyingCodes;
}
}
6 changes: 6 additions & 0 deletions src/main/resources/static/css/bootstrap.min.css

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<meta charset="UTF-8">
<title>贴代码</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../static/css/index.css">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.4.1/css/bootstrap.min.css"/>
<link rel="stylesheet" href="css/bootstrap.min.css"/>
</head>
<body class="container">
<div class="container-fluid">
Expand All @@ -20,6 +19,9 @@ <h1>
<div class="controls">
<textarea class="form-control" name="text" rows="6" cols="80"></textarea>
</div>
<div class="controls">
<input class="form-control" name="identifying" type="text" placeholder="识别码"/>
</div>
<div class="control-group">
<select class="form-control" name="type">
<option value="cpp">cpp</option>
Expand Down
Loading

0 comments on commit 52f6f1e

Please sign in to comment.