Skip to content

Commit

Permalink
添加导出题目
Browse files Browse the repository at this point in the history
  • Loading branch information
Smith-Cruise committed Feb 25, 2018
1 parent 94b48ad commit e2ed408
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 8 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# web static
eagle-oj-web/src/main/resources/public/
# web create temporary data
/data/

# idea
*.iml
.idea/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.eagleoj.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.eagleoj.web.controller.exception.WebErrorException;
import com.eagleoj.web.controller.format.index.ImportProblemsFormat;
import com.eagleoj.web.data.status.RoleStatus;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
Expand All @@ -16,11 +19,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -88,7 +89,6 @@ public ResponseEntity listContestProblemsForAdding(@RequestParam("page") int pag
}

@ApiOperation("获取所有题目")
@RequiresAuthentication
@RequiresRoles(value = {"8", "9"}, logical = Logical.OR)
@GetMapping
public ResponseEntity getAllProblems(@RequestParam("page") int page,
Expand All @@ -98,12 +98,33 @@ public ResponseEntity getAllProblems(@RequestParam("page") int page,
}

@ApiOperation("获取待审核的题目列表")
@RequiresAuthentication
@RequiresRoles(value = {"8", "9"}, logical = Logical.OR)
@GetMapping("/auditing")
public ResponseEntity getAuditingProblems(@RequestParam("page") int page,
@RequestParam("page_size") int pageSize) {
Page pager = PageHelper.startPage(page, pageSize);
return new ResponseEntity(WebUtil.generatePageData(pager, problemService.listAuditingProblems()));
}

@ApiOperation("导出题目")
@RequiresRoles("9")
@PostMapping("/export")
public ResponseEntity exportProblems(@RequestBody @Valid ImportProblemsFormat format) {
JSONArray pidList = format.getPidList();
if (pidList.size() == 0) {
throw new WebErrorException("列表不得为空");
}
if (! problemService.exportProblems(pidList)) {
throw new WebErrorException("题目导出失败");
}
return new ResponseEntity("题目导出成功");
}

@ApiOperation("导入题目")
@RequiresRoles("9")
@PostMapping("/import")
public ResponseEntity importProblems() {
// todo
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.eagleoj.web.controller.format.index;

import com.alibaba.fastjson.JSONArray;

import javax.validation.constraints.NotNull;

/**
* @author Smith
**/
public class ImportProblemsFormat {

@NotNull
private JSONArray pidList;

public JSONArray getPidList() {
return pidList;
}

public void setPidList(JSONArray pidList) {
this.pidList = pidList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ int save(JSONArray tags, int owner, String title, JSONObject description, JSONOb

List<ProblemEntity> listAllProblems();

boolean exportProblems(JSONArray list);

boolean importProblems(JSONArray list);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
import com.eagleoj.web.dao.TagsMapper;
import com.eagleoj.web.entity.ProblemEntity;
import com.eagleoj.web.entity.TagProblemEntity;
import com.eagleoj.web.entity.TestCaseEntity;
import com.eagleoj.web.service.*;
import com.eagleoj.web.service.async.AsyncTaskService;
import com.eagleoj.web.util.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -27,6 +35,8 @@
@Service
public class ProblemServiceImpl implements ProblemService {

private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

@Autowired
private ProblemMapper problemMapper;

Expand Down Expand Up @@ -227,4 +237,77 @@ public List<ProblemEntity> listProblemsForContest(int uid) {
public List<ProblemEntity> listAllProblems() {
return problemMapper.listAll();
}

@Override
public boolean exportProblems(JSONArray pidList) {
JSONArray data = new JSONArray();
for (int i=0; i<pidList.size(); i++) {
int pid = pidList.getInteger(i);
JSONObject obj = new JSONObject();
ProblemEntity problemEntity = problemMapper.getByPid(pid);
obj.put("title", problemEntity.getTime());
obj.put("description", problemEntity.getDescription());
obj.put("input_format", problemEntity.getInputFormat());
obj.put("output_format", problemEntity.getOutputFormat());
obj.put("difficult", problemEntity.getDifficult());
obj.put("samples", problemEntity.getSamples());
obj.put("time", problemEntity.getTime());
obj.put("memory", problemEntity.getMemory());
List<TestCaseEntity> testCases = testCasesService.listProblemTestCases(pid);
JSONArray testCaseArray = new JSONArray(testCases.size());
for (TestCaseEntity entity: testCases) {
JSONObject tempObj = new JSONObject();
tempObj.put("stdin", entity.getStdin());
tempObj.put("stdout", entity.getStdout());
tempObj.put("strength", entity.getStrength());
testCaseArray.add(tempObj);
}
obj.put("test_cases", testCaseArray);
data.add(obj);
}
FileOutputStream fileOutputStream = null;
try {
Calendar calendar = Calendar.getInstance();
String filename = calendar.get(Calendar.YEAR)+"-"
+calendar.get(Calendar.MONTH)+"-"
+calendar.get(Calendar.DAY_OF_MONTH)+"-"
+calendar.get(Calendar.HOUR_OF_DAY)+"-"
+calendar.get(Calendar.MINUTE)+"-"
+calendar.get(Calendar.SECOND)+".json";

File parent = new File("data");
if (! parent.exists()) {
if (! parent.mkdir()) {
throw new IOException("create dictionary failed");
}
}

File file = new File("data/"+filename);
if (! file.exists()) {
if (! file.createNewFile()) {
throw new IOException("create file failed");
}
}
fileOutputStream = new FileOutputStream(file, false);
fileOutputStream.write(data.toJSONString().getBytes("utf-8"));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
return false;
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
return true;
}

@Transactional
@Override
public boolean importProblems(JSONArray list) {
return false;
}
}
Empty file.

0 comments on commit e2ed408

Please sign in to comment.