Skip to content

Commit

Permalink
✨完成代码运行功能
Browse files Browse the repository at this point in the history
  • Loading branch information
hz wang committed May 4, 2023
1 parent 158e3b8 commit 7b3a885
Show file tree
Hide file tree
Showing 39 changed files with 4,152 additions and 2,567 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mewhz.paste;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mewhz.paste.constant;

/**
* @author mewhz
*/
public interface RunConstant {

String RUN_DIRECTORY = "e:/runCode/code/";

String CLEAR_FILE_NAME = "e:/runCode/code/clear.bat";

String JAVA_TYPE = "java";

String CPP_TYPE = "cpp";

String PYTHON_TYPE = "python";

String JAVA_CODE_FILE = "Main.java";

String CPP_CODE_FILE = "main.cpp";

String PYTHON_CODE_FILE = "main.py";

String OUT_FILE_SUFFIX = "-output.txt";

String INT_FILE_SUFFIX = "-input.txt";

String ERROR_FILE_SUFFIX = "-error.txt";
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.mewhz.paste.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mewhz.paste.model.entity.Code;
import com.mewhz.paste.model.entity.Run;
import com.mewhz.paste.model.vo.*;
import com.mewhz.paste.service.CodeService;
import org.springframework.web.bind.annotation.*;
Expand All @@ -25,7 +26,7 @@ public class CodeController {

@GetMapping("/")
public ResultVO<List<Code>> findAll(){
return ResultVO.ok(codeService.list());
return ResultVO.ok();
}

@PostMapping("/")
Expand Down Expand Up @@ -58,9 +59,9 @@ public ResultVO<List<CodeInfoVO>> getUserShareCode(@PathVariable Integer userId)
return ResultVO.ok(codeService.userShareCode(userId));
}

@GetMapping("/list")
public ResultVO<ResultPageVO<CodeInfoVO>> getList(CodeSearchVO codeSearchVO) {
return ResultVO.ok(codeService.getList(codeSearchVO));
@GetMapping("/pageList")
public ResultVO<ResultPageVO<CodeInfoVO>> getPageList(CodeSearchVO codeSearchVO) {
return ResultVO.ok(codeService.getPageList(codeSearchVO));
}

@PostMapping("/delete")
Expand Down Expand Up @@ -88,4 +89,9 @@ public ResultVO<List<CodeStatusInfo>> getUserCollectList(@PathVariable Integer u
return ResultVO.ok(codeService.userCollectCode(userId));
}

@GetMapping("/getUserRunCodeList/{userId}")
public ResultVO<List<Run>> getUserRunCodeList(@PathVariable Integer userId) {
return ResultVO.ok(codeService.getUserRunCodeList(userId));
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
package com.mewhz.paste.controller;

import com.mewhz.paste.model.entity.Code;
import com.mewhz.paste.model.entity.User;
import com.mewhz.paste.model.vo.ResultVO;
import com.mewhz.paste.model.vo.UserRegisterVO;
import com.mewhz.paste.service.CodeService;
import com.mewhz.paste.service.UserService;
import lombok.SneakyThrows;
import net.datafaker.Faker;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author mewhz
Expand All @@ -28,6 +16,7 @@ public class HeartController {
@Resource
private CodeService codeService;


// @GetMapping("/login")
// public SaTokenInfo login() {
// StpUtil.login(1001);
Expand Down Expand Up @@ -72,21 +61,4 @@ public class HeartController {
//
// }

@SneakyThrows
@GetMapping("/download/{codeId}")
public void download(HttpServletResponse response, @PathVariable String codeId) {
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition",
"attachment;filename=file_" + System.currentTimeMillis() + ".txt");

Code code = codeService.getById(codeId);

byte[] bytes = code.getCodeText().getBytes();

System.out.println(bytes.length);

response.getOutputStream().write(bytes);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mewhz.paste.controller;

import com.mewhz.paste.model.entity.Code;
import com.mewhz.paste.model.entity.Run;
import com.mewhz.paste.model.vo.CodeRunVO;
import com.mewhz.paste.model.vo.ResultVO;
import com.mewhz.paste.model.vo.RunResultVO;
import com.mewhz.paste.service.RunService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
* @author mewhz
*/
@RestController
@RequestMapping("/run")
public class RunController {

@Resource
private RunService runService;

@PostMapping("/")
public ResultVO<RunResultVO> receivedCode(@RequestBody CodeRunVO codeRunVO) {
return ResultVO.ok(runService.receivedCode(codeRunVO));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ResultVO<ResultPageVO<UserInfoVO>> getList(UserSearchVO userSearchVO) {

@PostMapping("/update")
public ResultVO<Boolean> update(@RequestBody User user) {
return ResultVO.ok(userService.updateById(user));
return ResultVO.ok(userService.updateUser(user));
}

@PostMapping("/delete")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.mewhz.paste.exception;

import cn.dev33.satoken.exception.NotLoginException;
import com.mewhz.paste.common.BaseResponse;
import com.mewhz.paste.common.ResultUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
Expand All @@ -12,8 +8,4 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(NotLoginException.class)
public BaseResponse<?> handlerNotLoginException(NotLoginException e) {
return ResultUtils.error(e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface CodeMapper extends BaseMapper<Code> {

Integer codeTotal(CodeSearchVO codeSearchVO);

List<CodeInfoVO> codeList(@Param("codeSearchVO") CodeSearchVO codeSearchVO, @Param("size") Long size);
List<CodeInfoVO> codePageList(@Param("codeSearchVO") CodeSearchVO codeSearchVO, @Param("size") Long size);

List<CodeStatusInfo> codeStatusList(@Param("codeAuthorIds") List<Integer> codeAuthorIds, @Param("userId") Integer userId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mewhz.paste.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mewhz.paste.model.entity.Run;
import org.springframework.stereotype.Repository;

/**
* @author mewhz
*/
@Repository
public interface RunMapper extends BaseMapper<Run> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Code {
private String codeText;
private String codeType;
private String codeTitle;
private Integer codeStatus;
private String codeCreateDate;
private Integer codeAuthorId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mewhz.paste.model.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
* @author mewhz
*/
@Data
@ToString
@TableName("run")
@AllArgsConstructor
@NoArgsConstructor
public class Run {

@TableId(value = "run_id", type = IdType.AUTO)
private Integer runId;
private Integer codeId;
private String runInput;
private String runOutput;
private String runError;
private String runCreateDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mewhz.paste.model.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
* @author mewhz
*/
//@Data
//@ToString
//@NoArgsConstructor
//@AllArgsConstructor
//public class CodeRunInfoVO {
//
//
//
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mewhz.paste.model.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
* @author mewhz
*/
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class CodeRunVO {

private String codeText;
private String codeType;
private Integer codeAuthorId;

private String runInput;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mewhz.paste.model.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
* @author mewhz
*/
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class RunResultVO {

private String runOutput;
private String runError;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mewhz.paste.mapper.CodeMapper;
import com.mewhz.paste.mapper.RunMapper;
import com.mewhz.paste.mapper.UserMapper;
import com.mewhz.paste.model.entity.Code;
import com.mewhz.paste.model.entity.Run;
import com.mewhz.paste.model.entity.User;
import com.mewhz.paste.model.vo.*;
import com.mewhz.paste.utils.CodeUtils;
Expand Down Expand Up @@ -38,6 +40,9 @@ public class CodeService extends ServiceImpl<CodeMapper, Code> {
@Resource
private CodeMapper codeMapper;

@Resource
private RunMapper runMapper;

public CodeInfoVO findByCodeId(Integer codeId) {
return codeMapper.findByCodeId(codeId);
}
Expand Down Expand Up @@ -95,9 +100,9 @@ public List<CodeInfoVO> userShareCode(Integer userId) {

}

public ResultPageVO<CodeInfoVO> getList(CodeSearchVO codeSearchVO) {
public ResultPageVO<CodeInfoVO> getPageList(CodeSearchVO codeSearchVO) {
Integer count = codeMapper.codeTotal(codeSearchVO);
List<CodeInfoVO> codes = codeMapper.codeList(codeSearchVO, CODE_PAGE_NUM);
List<CodeInfoVO> codes = codeMapper.codePageList(codeSearchVO, CODE_PAGE_NUM);
return new ResultPageVO<>(codes, count);
}

Expand Down Expand Up @@ -137,12 +142,29 @@ public List<CodeStatusInfo> getStatusList(Integer userId) {
.collect(Collectors.toList());


codeAuthorIds.forEach(System.out::println);
if (codeAuthorIds.size() == 0) {
return null;
}

return codeMapper.codeStatusList(codeAuthorIds, userId);
}

public List<CodeStatusInfo> userCollectCode(Integer userId){
return codeMapper.userCollectCode(userId);
}

public List<Run> getUserRunCodeList(Integer userId) {
List<Integer> codeIds = this.list(new LambdaQueryWrapper<Code>()
.eq(Code::getCodeAuthorId, userId))
.stream()
.map(Code::getCodeId)
.collect(Collectors.toList());

if (codeIds.size() == 0) {
return null;
}

return runMapper.selectList(new LambdaQueryWrapper<Run>().in(Run::getCodeId, codeIds));

}
}
Loading

0 comments on commit 7b3a885

Please sign in to comment.