Skip to content

Commit

Permalink
Merge pull request #19 from shuoj/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kastnerorz authored Nov 21, 2019
2 parents 9194bf6 + 9a9fda8 commit a5e11a2
Show file tree
Hide file tree
Showing 68 changed files with 1,028 additions and 833 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@
<artifactId>sentry-logback</artifactId>
<version>1.7.27</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/cn/kastner/oj/OjApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class OjApplication {

public static void main(String[] args) {
SpringApplication.run(OjApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(OjApplication.class, args);
}
}
1 change: 1 addition & 0 deletions src/main/java/cn/kastner/oj/aspect/SubmissionAspect.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.kastner.oj.aspect;

import cn.kastner.oj.domain.*;
import cn.kastner.oj.domain.enums.Result;
import cn.kastner.oj.domain.security.UserContext;
import cn.kastner.oj.dto.SubmissionDTO;
import cn.kastner.oj.exception.AppException;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/kastner/oj/constant/LanguageConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.kastner.oj.constant;

import cn.kastner.oj.domain.Language;
import cn.kastner.oj.domain.enums.Language;

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.kastner.oj.domain.User;
import cn.kastner.oj.dto.SecurityQuestionDTO;
import cn.kastner.oj.dto.UserDTO;
import cn.kastner.oj.exception.AppException;
import cn.kastner.oj.exception.AuthenticationException;
import cn.kastner.oj.exception.NoSuchItemException;
Expand Down Expand Up @@ -59,13 +60,11 @@ public AuthenticationRestController(
}

/**
* @param username 用户名
* @param password 密码
* @return { "token": "..." }
* @throws AuthenticationException 认证失败
*/
@PostMapping(value = "/api/v1/auth")
public ResponseEntity<?> createAuthenticationToken(
public ResponseEntity createAuthenticationToken(
@RequestBody JwtAuthenticationRequest authenticationRequest) throws AuthenticationException {
final String token =
authenticationService.login(
Expand All @@ -75,7 +74,7 @@ public ResponseEntity<?> createAuthenticationToken(
}

@GetMapping(value = "/api/v1/refresh")
public ResponseEntity<?> refreshAndGetAuthenticationToken(HttpServletRequest request) {
public ResponseEntity refreshAndGetAuthenticationToken(HttpServletRequest request) {
String authToken = request.getHeader(tokenHeader);
final String token = authToken.substring(7);
String username = jwtTokenUtil.getUsernameFromToken(token);
Expand All @@ -90,12 +89,12 @@ public ResponseEntity<?> refreshAndGetAuthenticationToken(HttpServletRequest req
}

@PostMapping(value = "/api/v1/register")
public JwtUser register(@Validated @RequestBody User addedUser, BindingResult result)
public JwtUser register(@Validated @RequestBody UserDTO userDTO, BindingResult result)
throws AppException {
if (result.hasFieldErrors()) {
throw new ValidateException(result.getFieldError().getDefaultMessage());
}
User user = authenticationService.register(addedUser);
User user = authenticationService.register(userDTO);
return JwtUserFactory.create(user);
}

Expand Down
78 changes: 44 additions & 34 deletions src/main/java/cn/kastner/oj/controller/ContestRestController.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package cn.kastner.oj.controller;

import cn.kastner.oj.domain.ContestOption;
import cn.kastner.oj.dto.ContestDTO;
import cn.kastner.oj.dto.PageDTO;
import cn.kastner.oj.dto.ProblemDTO;
import cn.kastner.oj.dto.RankingDTO;
import cn.kastner.oj.domain.enums.ContestOption;
import cn.kastner.oj.dto.*;
import cn.kastner.oj.exception.AppException;
import cn.kastner.oj.exception.ContestException;
import cn.kastner.oj.exception.ValidateException;
import cn.kastner.oj.query.ContestQuery;
import cn.kastner.oj.security.JwtUser;
import cn.kastner.oj.query.RankingQuery;
import cn.kastner.oj.service.ContestService;
import cn.kastner.oj.util.NetResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@RestController
Expand All @@ -26,12 +27,9 @@ public class ContestRestController {

private final ContestService contestService;

private final NetResult netResult;

@Autowired
public ContestRestController(ContestService contestService, NetResult netResult) {
public ContestRestController(ContestService contestService) {
this.contestService = contestService;
this.netResult = netResult;
}

/**
Expand All @@ -55,7 +53,8 @@ public ContestDTO getContest(@PathVariable String id) throws AppException {
public PageDTO<ContestDTO> getContests(
ContestQuery contestQuery,
@RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "size", defaultValue = "10") Integer size) {
@RequestParam(value = "size", defaultValue = "10") Integer size)
throws ContestException {
return contestService.findCriteria(page, size, contestQuery);
}

Expand Down Expand Up @@ -107,7 +106,8 @@ public List<ProblemDTO> getProblems(@PathVariable String id) throws AppException
}

@GetMapping("/{contestId}/problems/{problemId}")
public ProblemDTO getOneProblem(@PathVariable String contestId, @PathVariable String problemId) throws AppException {
public ProblemDTO getOneProblem(@PathVariable String contestId, @PathVariable String problemId)
throws AppException {
return contestService.findOneProblem(contestId, problemId);
}

Expand All @@ -121,61 +121,71 @@ public List<ProblemDTO> setProblems(
@PostMapping("/{id}/problems/add")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public ResponseEntity addProblem(
@RequestParam String problemId, @PathVariable String id, @RequestParam Integer score) throws AppException {
@RequestParam String problemId, @PathVariable String id, @RequestParam Integer score)
throws AppException {
contestService.addProblem(problemId, id, score);
return ResponseEntity.ok().build();
}

@DeleteMapping("/{id}/problems")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public ResponseEntity deleteProblems(@RequestBody List<String> problemIdList, @PathVariable String id)
throws AppException {
public ResponseEntity deleteProblems(
@RequestBody List<String> problemIdList, @PathVariable String id) throws AppException {
contestService.deleteProblems(problemIdList, id);
return ResponseEntity.ok().build();
}

@PostMapping(value = "/{id}/groups")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public List<JwtUser> addUsersByGroups(
public List<RankingUserDTO> addUsersByGroups(
@RequestBody List<String> groupIdList, @PathVariable String id) throws AppException {
return contestService.addUsersByGroups(groupIdList, id);
}

@PostMapping("/{id}/join")
public NetResult joinContest(@PathVariable String id, String password) throws AppException {
Boolean result = contestService.joinContest(id, password);
if (result) {
netResult.message = "加入成功";
} else {
netResult.message = "密码错误";
}
netResult.code = 200;
netResult.data = result;
return netResult;
public void joinContest(@PathVariable String id, String password) throws AppException {
contestService.joinContest(id, password);
}

@GetMapping("/{id}/users")
public List<JwtUser> getUsers(@PathVariable String id) throws AppException {
public List<RankingUserDTO> getUsers(@PathVariable String id) throws AppException {
return contestService.getUsers(id);
}

@PostMapping("/{id}/users")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public List<JwtUser> addUsers(@RequestBody List<String> userIdList, @PathVariable String id)
throws AppException {
public List<RankingUserDTO> addUsers(
@RequestBody List<String> userIdList, @PathVariable String id) throws AppException {
return contestService.addUsers(userIdList, id);
}

@DeleteMapping("/{id}/users")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public List<JwtUser> deleteUsers(@RequestBody List<String> userIdList, @PathVariable String id)
public void deleteUsers(@RequestBody List<String> userIdList, @PathVariable String id)
throws AppException {
return contestService.deleteUsers(userIdList, id);
contestService.deleteUsers(userIdList, id);
}

@GetMapping("/{id}/ranking")
public RankingDTO getRanking(@PathVariable String id) throws AppException {
return contestService.getRanking(id);
public RankingDTO getRanking(@PathVariable String id, RankingQuery query) throws AppException {
return contestService.getRanking(id, query);
}

@GetMapping("/{id}/ranking/export")
public void exportRanking(
@PathVariable String id, RankingQuery query, HttpServletResponse response)
throws ContestException {
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
String fileName = contestService.findById(id).getName() + "排名";
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

Workbook workbook = contestService.exportRanking(id, query);
try (OutputStream os = response.getOutputStream()) {
workbook.write(os);
} catch (IOException e) {
throw new ContestException(ContestException.EXPORT_ERROR);
}
}

@PatchMapping("/{id}/status")
Expand Down
70 changes: 14 additions & 56 deletions src/main/java/cn/kastner/oj/controller/GroupRestController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cn.kastner.oj.controller;

import cn.kastner.oj.dto.GroupDTO;
import cn.kastner.oj.exception.*;
import cn.kastner.oj.repository.UserRepository;
import cn.kastner.oj.dto.PageDTO;
import cn.kastner.oj.exception.AppException;
import cn.kastner.oj.exception.GroupException;
import cn.kastner.oj.exception.ValidateException;
import cn.kastner.oj.query.GroupQuery;
import cn.kastner.oj.security.JwtUser;
import cn.kastner.oj.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -19,61 +22,38 @@ public class GroupRestController {

private final GroupService groupService;


@Autowired
public GroupRestController(GroupService groupService) {
this.groupService = groupService;
}

/**
* 获取指定群组
*
* @param id
* @throws NoSuchItemException return groupDTO
*/
@GetMapping(value = "/{id}")
public GroupDTO getGroup(@PathVariable String id) throws GroupException {
return groupService.findById(id);
}

/**
* 获取所有群组
*
* @return
* @throws NoSuchItemException
*/
/** 条件查询群组 */
@GetMapping
public List<GroupDTO> getGroups() throws GroupException {
return groupService.findAllGroups();
public PageDTO<GroupDTO> findCriteria(GroupQuery query, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
return groupService.findCriteria(query, page, size);
}

/**
* 创建群组
*
* @param groupDTO
* @param bindingResult
* @return groupDTO
*/
/** 创建群组 */
@PostMapping
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public GroupDTO createGroup(
@Validated @RequestBody GroupDTO groupDTO, BindingResult bindingResult)
throws AppException {
@Validated @RequestBody GroupDTO groupDTO, BindingResult bindingResult) throws AppException {
if (bindingResult.hasErrors()) {
throw new ValidateException(bindingResult.getFieldError().getDefaultMessage());
} else {
return groupService.create(groupDTO);
}
}

/**
* 更新群组信息
*
* @param groupDTO
* @param bindingResult
* @param id
* @return
*/
/** 更新群组信息 */
@PutMapping(value = "/{id}")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public GroupDTO updateGroup(
Expand All @@ -90,27 +70,14 @@ public GroupDTO updateGroup(
}
}

/**
* 删除指定群组
*
* @param id
* @return
* @throws NoSuchItemException
*/
/** 删除指定群组 */
@DeleteMapping(value = "/{id}")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public GroupDTO deleteGroup(@PathVariable String id) throws AppException {
return groupService.delete(id);
}

/**
* 添加指定群组成员(由管理员批量添加)
*
* @param usersId
* @param bindingResult
* @param id
* @return
*/
/** 添加指定群组成员(由管理员批量添加) */
@PostMapping(value = "/{id}/members")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public List<JwtUser> addMembers(
Expand All @@ -125,16 +92,7 @@ public List<JwtUser> addMembers(
return groupService.addMembers(id, usersId);
}

/**
* 删除指定群组内的成员(由管理员批量删除)
*
* @param usersId
* @param bindingResult
* @param id
* @return
* @throws ValidateException
* @throws NoSuchItemException
*/
/** 删除指定群组内的成员(由管理员批量删除) */
@DeleteMapping(value = "/{id}/members")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public List<JwtUser> deleteMembers(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.kastner.oj.controller;

import cn.kastner.oj.domain.JudgeServerStatus;
import cn.kastner.oj.domain.pojos.JudgeServerStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
Expand Down
Loading

0 comments on commit a5e11a2

Please sign in to comment.