Skip to content

Commit

Permalink
add analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
jinmingyi1998 committed Mar 1, 2020
1 parent 319df09 commit 0db300d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/main/java/cn/edu/zjnu/acm/controller/ProblemController.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public String showProblem(@PathVariable Long id) {
public String problemArticle() {
return "problem/article";
}

@GetMapping("/article/edit/{id:[0-9]+}")
public String editArticle() {
return "problem/edit_analysis";
}
}

@Slf4j
Expand Down Expand Up @@ -205,6 +210,37 @@ public RestfulResult postAnalysis(@PathVariable Long pid,
return new RestfulResult(200, "success", null);
}

@GetMapping("/analysis/edit/{aid:[0-9]+}")
public RestfulResult getOneAnalysis(@PathVariable Long aid, @SessionAttribute User currentUser) {
Analysis analysis = problemService.getAnalysisById(aid);
if (analysis == null) {
throw new NotFoundException("Analysis not found");
}
if (analysis.getUser().getId() != currentUser.getId()) {
throw new ForbiddenException("Permission denied");
}
analysis.getUser().hideInfo();
analysis.setProblem(null);
analysis.setComment(null);
return new RestfulResult(200, "success", analysis);
}

@PostMapping("/analysis/edit/{aid:[0-9]+}")
public RestfulResult editAnalysis(@PathVariable Long aid,
@SessionAttribute User currentUser,
@RequestBody @Validated Analysis analysis) {
Analysis ana = problemService.getAnalysisById(aid);
if (analysis == null) {
throw new NotFoundException("Analysis not found");
}
if (ana.getUser().getId() != currentUser.getId()) {
throw new ForbiddenException("Permission denied");
}
ana.setText(analysis.getText());
problemService.postAnalysis(ana);
return new RestfulResult(200, "success", null);
}

@PostMapping("/analysis/post/comment/{aid:[0-9]+}")
public RestfulResult postAnalysisComment(@PathVariable Long aid, @SessionAttribute User currentUser,
@RequestBody ContestController.CommentPost commentPost) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public Result forbiddenExceptionHandle(ForbiddenException e) {
@ResponseBody
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public Result serverExceptionHandle(Exception e) {
e.printStackTrace();
return new Result(500, "Internal Server Error");
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/problem/article.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h2 class="ui left floated header">
<div class="sub header">{{normalPostTime(article.postTime)}}</div>
</div>
</h2>
<a href="" class="ui right floated button basic" v-if="isOwner(article.user.id)">Edit</a>
<a :href="'/problems/article/edit/'+article.id" class="ui right floated button basic" v-if="isOwner(article.user.id)">Edit</a>
<div class="md-text" style="padding: 0;">
<textarea style="display: none;" v-html="article.text">TEXT</textarea>
</div>
Expand Down
102 changes: 102 additions & 0 deletions src/main/resources/templates/problem/edit_analysis.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<!DOCTYPE html>
<html lang="en">
<head>
<th:block th:insert="~{fragment/header :: header}"></th:block>
<title>Update</title>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
<link rel="stylesheet" href="/editor/css/editormd.min.css">
<script src="/editor/editormd.min.js"></script>
</head>
<body>
<main class="ui container segment">
<form onsubmit="return false" class="ui form" id="article">
<div class="field">
<div id="editor-article">
<textarea style="display: none"></textarea>
</div>
</div>
<div class="button ui inverted green" v-on:click="send_article()"><i class="ui paper plane icon"></i>Post</div>
</form>
</main>

<script>
aid = location.pathname.split('/');
aid = aid[aid.length - 1];
var editor;

var article = new Vue({
el: "#article",
data: {
id: aid,
text: "",
},
methods: {
send_article() {
if (this.title == "") {
alert("Title cannot be empty");
return;
}
this.text = editor.getMarkdown();
if (this.text.length < 15) {
alert("content too short! At least 15 letters");
return;
}
axios.post("/api/problems/analysis/edit/" + aid, {
text: this.text
})
.then(function (res) {
if (res.data.code == 200) {
alert(res.data.message);
location.reload();
} else {
alert(res.data.message);
}
})
.catch(function (e) {
console.log(e);
}).finally(function () {
;
});
}
},
created() {
let that = this;
axios.get("/api/problems/analysis/edit/" + aid)
.then(function (res) {
if (res.data.code == 200) {
that.text = res.data.data.text;
$(function () {
editor = editormd({
id: "editor-article",
path: "/editor/lib/",
pluginPath: "/editor/plugins/",
height: 800,
autoFocus: false,
markdown: that.text,
tocTitle: "目录",
pageBreak: true,
atLink: true, // for @link
emailLink: true, // for mail address auto link
tex: true,
taskList: true, // Github Flavored Markdown task lists
flowChart: true,
sequenceDiagram: true,
previewCodeHighlight: true,
imageUploadURL: "/api/media/upload",
imageUpload: true,
imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "ico", "JPG", "JPEG", "PNG"],
});
});
}
})
.catch(function (e) {
console.log(e)
if (e.response.status == 404) {
location.href = "4O4";
}
});
}
});
</script>
</body>
</html>

0 comments on commit 0db300d

Please sign in to comment.