Search in sources :

Example 1 with ApiException

use of cn.edu.sdu.qd.oj.common.exception.ApiException in project sduoj-server by SDUOJ.

the class ProblemManageService method createProblem.

@Transactional
public String createProblem(ProblemManageDTO problem) {
    problem.setProblemId(null);
    problem.setProblemCode(null);
    ProblemDO problemDO = problemManageConverter.from(problem);
    AssertUtils.isTrue(problemDao.save(problemDO), ApiExceptionEnum.UNKNOWN_ERROR);
    // TODO: 魔法值解决
    problemDO.setProblemCode("SDUOJ-" + problemDO.getProblemId());
    if (!problemDao.lambdaUpdate().eq(ProblemDO::getProblemId, problemDO.getProblemId()).set(ProblemDO::getProblemCode, problemDO.getProblemCode()).update()) {
        throw new ApiException(ApiExceptionEnum.UNKNOWN_ERROR);
    }
    return problemDO.getProblemCode();
}
Also used : ProblemDO(cn.edu.sdu.qd.oj.problem.entity.ProblemDO) ApiException(cn.edu.sdu.qd.oj.common.exception.ApiException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ApiException

use of cn.edu.sdu.qd.oj.common.exception.ApiException in project sduoj-server by SDUOJ.

the class UserController method thirdPartyLogin.

@GetMapping("/thirdPartyLogin")
@ResponseBody
public ResponseResult<UserThirdPartyLoginRespDTO> thirdPartyLogin(HttpServletRequest request, HttpServletResponse response, @RealIp String ipv4, @RequestHeader("user-agent") String userAgent) {
    ThirdPartyEnum thirdParty = ThirdPartyEnum.of(request.getParameter("thirdParty"));
    AssertUtils.notNull(thirdParty, ApiExceptionEnum.THIRD_PARTY_NOT_EXIST);
    UserThirdPartyLoginRespDTO respDTO = null;
    switch(thirdParty) {
        case SDUCAS:
            respDTO = userService.thirdPartyLoginBySduCas(request.getParameter("ticket"), ipv4, userAgent);
            break;
        case QQ:
        case WECHAT:
            throw new ApiException(ApiExceptionEnum.THIRD_PARTY_ERROR, "暂不支持这种第三方认证");
    }
    Optional.ofNullable(respDTO).map(UserThirdPartyLoginRespDTO::getUser).ifPresent(userSessionDTO -> {
        writeSessionToHeader(response, userSessionDTO);
    });
    return respDTO != null ? ResponseResult.ok(respDTO) : ResponseResult.error();
}
Also used : UserThirdPartyLoginRespDTO(cn.edu.sdu.qd.oj.user.dto.UserThirdPartyLoginRespDTO) ThirdPartyEnum(cn.edu.sdu.qd.oj.user.enums.ThirdPartyEnum) ApiException(cn.edu.sdu.qd.oj.common.exception.ApiException) ApiResponseBody(cn.edu.sdu.qd.oj.common.entity.ApiResponseBody)

Example 3 with ApiException

use of cn.edu.sdu.qd.oj.common.exception.ApiException in project sduoj-server by SDUOJ.

the class UserService method updateEmail.

public void updateEmail(Long userId, String password, @Email(message = "parameter is not an email") @NotBlank String email, String emailCode) {
    validateEmailCode(email, emailCode);
    validatePassword(userId, password);
    // 特判用户标 UserFeatureEnum.BAN_EMAIL_UPDATE
    UserDO userDO = userDao.lambdaQuery().select(UserDO::getFeatures).eq(UserDO::getUserId, userId).one();
    if (userConverter.featuresTo(userDO.getFeatures()).isBanEmailUpdate()) {
        throw new ApiException(ApiExceptionEnum.FEATURE_ERROR, "该用户被禁止更改邮箱");
    }
    AssertUtils.isTrue(userDao.lambdaUpdate().set(UserDO::getEmail, email).eq(UserDO::getUserId, userId).update(), ApiExceptionEnum.UNKNOWN_ERROR);
    consumeEmailCode(email, emailCode);
}
Also used : UserDO(cn.edu.sdu.qd.oj.user.entity.UserDO) ApiException(cn.edu.sdu.qd.oj.common.exception.ApiException)

Example 4 with ApiException

use of cn.edu.sdu.qd.oj.common.exception.ApiException in project sduoj-server by SDUOJ.

the class UserService method thirdPartyBinding.

/**
 * 第三方登录绑定已有账号
 */
@Transactional
public UserSessionDTO thirdPartyBinding(UserThirdPartyBindingReqDTO reqDTO, String ipv4, String userAgent) {
    // 取 redis 中的 token
    UserThirdPartyLoginRespDTO respDTO = JSON.parseObject((String) redisUtils.get(RedisConstants.getThirdPartyToken(reqDTO.getToken())), UserThirdPartyLoginRespDTO.class);
    AssertUtils.notNull(respDTO, ApiExceptionEnum.THIRD_PARTY_NOT_EXIST);
    ThirdPartyEnum thirdParty = respDTO.getThirdParty();
    // 调用具体的第三方绑定方式
    UserDO userDO = null;
    switch(thirdParty) {
        case SDUCAS:
            userDO = thirdPartyBindingBySduCas(reqDTO, respDTO);
            break;
        case QQ:
        case WECHAT:
            throw new ApiException(ApiExceptionEnum.THIRD_PARTY_ERROR, "暂不支持这种第三方认证");
    }
    // 特判用户标 UserFeatureEnum.BAN_THIRD_PARTY
    if (userConverter.featuresTo(userDO.getFeatures()).isBanThirdParty()) {
        throw new ApiException(ApiExceptionEnum.FEATURE_ERROR, "该用户被禁止使用第三方认证");
    }
    // 删除 redis 中对应的 token
    redisUtils.del(RedisConstants.getThirdPartyToken(reqDTO.getToken()));
    // 登录
    return loginWithWritingSession(userDO, ipv4, userAgent);
}
Also used : UserThirdPartyLoginRespDTO(cn.edu.sdu.qd.oj.user.dto.UserThirdPartyLoginRespDTO) UserDO(cn.edu.sdu.qd.oj.user.entity.UserDO) ThirdPartyEnum(cn.edu.sdu.qd.oj.user.enums.ThirdPartyEnum) ApiException(cn.edu.sdu.qd.oj.common.exception.ApiException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ApiException

use of cn.edu.sdu.qd.oj.common.exception.ApiException in project sduoj-server by SDUOJ.

the class ContestService method queryContestAndValidate.

private ContestDO queryContestAndValidate(long contestId, long userId) {
    ContestDO contestDO = contestDao.lambdaQuery().select(ContestDO::getContestId, ContestDO::getFeatures, ContestDO::getGmtStart, ContestDO::getGmtEnd, ContestDO::getUserId, ContestDO::getProblems, ContestDO::getParticipants, ContestDO::getUnofficialParticipants).eq(ContestDO::getContestId, contestId).one();
    AssertUtils.notNull(contestDO, ApiExceptionEnum.CONTEST_NOT_FOUND);
    // 判断比赛可见性
    ContestFeatureDTO featureDTO = ContestConvertUtils.featuresTo(contestDO.getFeatures());
    ContestOpennessEnum openness = ContestOpennessEnum.of(featureDTO.getOpenness());
    AssertUtils.isTrue(contestDO.containsUserIdInParticipants(userId) || !ContestOpennessEnum.PRIVATE.equals(openness), ApiExceptionEnum.CONTEST_NOT_PARTICIPATE);
    if (contestDO.getGmtStart().after(new Date())) {
        throw new ApiException(ApiExceptionEnum.CONTEST_NOT_BEGIN);
    }
    return contestDO;
}
Also used : ContestOpennessEnum(cn.edu.sdu.qd.oj.contest.enums.ContestOpennessEnum) ContestDO(cn.edu.sdu.qd.oj.contest.entity.ContestDO) InternalApiException(cn.edu.sdu.qd.oj.common.exception.InternalApiException) ApiException(cn.edu.sdu.qd.oj.common.exception.ApiException)

Aggregations

ApiException (cn.edu.sdu.qd.oj.common.exception.ApiException)22 Transactional (org.springframework.transaction.annotation.Transactional)12 IOException (java.io.IOException)9 UserDO (cn.edu.sdu.qd.oj.user.entity.UserDO)6 MultipartFile (org.springframework.web.multipart.MultipartFile)5 ApiExceptionEnum (cn.edu.sdu.qd.oj.common.enums.ApiExceptionEnum)4 AssertUtils (cn.edu.sdu.qd.oj.common.util.AssertUtils)4 ContestDO (cn.edu.sdu.qd.oj.contest.entity.ContestDO)4 PlainFileDownloadDTO (cn.edu.sdu.qd.oj.dto.PlainFileDownloadDTO)4 UserThirdPartyLoginRespDTO (cn.edu.sdu.qd.oj.user.dto.UserThirdPartyLoginRespDTO)4 Function (java.util.function.Function)4 Collectors (java.util.stream.Collectors)4 Slf4j (lombok.extern.slf4j.Slf4j)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Service (org.springframework.stereotype.Service)4 ApiResponseBody (cn.edu.sdu.qd.oj.common.entity.ApiResponseBody)3 InternalApiException (cn.edu.sdu.qd.oj.common.exception.InternalApiException)3 SnowflakeIdWorker (cn.edu.sdu.qd.oj.common.util.SnowflakeIdWorker)3 BinaryFileUploadReqDTO (cn.edu.sdu.qd.oj.dto.BinaryFileUploadReqDTO)3 ZipEntry (java.util.zip.ZipEntry)3