Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Commit

Permalink
完善random
Browse files Browse the repository at this point in the history
  • Loading branch information
Smith-Cruise committed Feb 6, 2018
1 parent 8d87f23 commit 276a208
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.eagleoj.web.data.status.RoleStatus;
import com.eagleoj.web.service.ProblemModeratorService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import com.eagleoj.web.entity.*;
Expand Down Expand Up @@ -47,6 +48,16 @@ public class ProblemController {
@Autowired
private TestCasesService testCasesService;

@ApiOperation("随机获取一个题目ID")
@GetMapping("/random")
public ResponseEntity getRandomProblem() {
if (SecurityUtils.getSubject().isAuthenticated()) {
return new ResponseEntity(problemService.getRandomPid(SessionHelper.get().getUid()));
} else {
return new ResponseEntity(problemService.getRandomPid(null));
}
}


@ApiOperation("获取指定题目的信息")
@GetMapping("/{pid}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface ProblemMapper {

int count();

int getRandomPid(Integer uid);

ProblemEntity getByPid(int pid);

List<Map<String, Object>> listProblemTagsByPid(int pid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ int save(JSONArray tags, int owner, String title, JSONObject description, JSONOb

int countProblems();

int getRandomPid(Integer uid);

void deleteProblem(int pid);

void updateProblem(int pid, JSONArray tags, ProblemEntity problemEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public int countProblems() {
return problemMapper.count();
}

@Override
public int getRandomPid(Integer uid) {
return problemMapper.getRandomPid(uid);
}

@Transactional
@Override
public void deleteProblem(int pid) {
Expand Down
12 changes: 12 additions & 0 deletions eagle-oj-web/src/main/resources/mapping/ProblemMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
SELECT count(*) FROM problem
</select>

<select id="getRandomPid" resultType="Integer">
SELECT problem.pid FROM problem
<if test="uid != null">
LEFT JOIN problem_user ON problem.pid=problem_user.pid
</if>
WHERE problem.status=2
<if test="uid != null">
AND problem_user.status!='AC'
</if>
ORDER BY rand() LIMIT 1
</select>

<select id="getByPid" resultMap="problemResultMap">
SELECT * from `problem` WHERE `pid` = #{pid}
</select>
Expand Down

0 comments on commit 276a208

Please sign in to comment.