Skip to content

Commit

Permalink
Merge pull request #12 from shuoj/fix/update-announcement
Browse files Browse the repository at this point in the history
Fix/update announcement
  • Loading branch information
kastnerorz authored Nov 3, 2019
2 parents c869ba4 + 66714cf commit 8bdd8ad
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 104 deletions.
7 changes: 4 additions & 3 deletions src/main/java/cn/kastner/oj/domain/Problem.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Entity
Expand Down Expand Up @@ -51,7 +52,7 @@ public class Problem {
name = "problem_tag",
joinColumns = {@JoinColumn(name = "problem_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "tag_id", referencedColumnName = "id")})
private List<Tag> tagList;
private Set<Tag> tagList;

@Column(columnDefinition = "TEXT")
private String inputDesc;
Expand Down Expand Up @@ -91,7 +92,7 @@ public class Problem {
public Problem() {
this.id = UUID.randomUUID().toString();
this.visible = true;
this.tagList = new ArrayList<>();
this.tagList = new HashSet<>();
this.specialJudged = false;
this.hint = "";
this.source = "";
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/cn/kastner/oj/domain/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,25 @@ public Tag() {
this.problemCount = 0L;
this.problemList = new ArrayList<>();
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}

if (!(o instanceof Tag)) {
return false;
}

Tag tag = (Tag) o;
return tag.name.equals(name);
}

@Override
public int hashCode() {
int code = 20;
code = code * 30 + name.hashCode();
return code;
}
}
6 changes: 4 additions & 2 deletions src/main/java/cn/kastner/oj/dto/ProblemDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Data
public class ProblemDTO {
Expand Down Expand Up @@ -39,15 +41,15 @@ public class ProblemDTO {

private Boolean visible;

private List<TagDTO> tagList;
private Set<TagDTO> tagList;

@NotBlank(message = "输入描述不能为空")
private String inputDesc;

@NotBlank(message = "输出描述不能为空")
private String outputDesc;

@NotNull(message = "样例不能为空")
@NotEmpty(message = "样例不能为空")
private List<SampleIO> sampleIOList;

private String sampleIO;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/cn/kastner/oj/dto/TagDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,25 @@ public class TagDTO {
public TagDTO() {
this.problemCount = 0L;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}

if (!(o instanceof TagDTO)) {
return false;
}

TagDTO tagDTO = (TagDTO) o;
return tagDTO.name.equals(name);
}

@Override
public int hashCode() {
int code = 20;
code = code * 30 + name.hashCode();
return code;
}
}
18 changes: 18 additions & 0 deletions src/main/java/cn/kastner/oj/exception/ContestException.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public class ContestException extends AppException {

public static final String CANNOT_SHARING = "比赛设置为不可分享";

public static final String CONTEST_IS_ENDED = "比赛结束后不可修改";

public static final String START_TIME_IS_EARLY_THAN_NOW = "开始时间早于当前时间";

public static final String START_TIME_IS_AFTER_THAN_END_TIME = "开始时间晚于结束时间";

public ContestException(String message) {
super(message);
switch (message) {
Expand Down Expand Up @@ -55,6 +61,18 @@ public ContestException(String message) {
this.code = -9;
this.status = HttpStatus.BAD_REQUEST;
break;
case CONTEST_IS_ENDED:
this.code = -10;
this.status = HttpStatus.BAD_REQUEST;
break;
case START_TIME_IS_EARLY_THAN_NOW:
this.code = -11;
this.status = HttpStatus.BAD_REQUEST;
break;
case START_TIME_IS_AFTER_THAN_END_TIME:
this.code = -12;
this.status = HttpStatus.BAD_REQUEST;
break;
default:
this.code = -1;
this.status = HttpStatus.INTERNAL_SERVER_ERROR;
Expand Down
145 changes: 116 additions & 29 deletions src/main/java/cn/kastner/oj/service/impl/ContestServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service
@Transactional
Expand Down Expand Up @@ -61,7 +64,8 @@ public ContestServiceImpl(
ContestProblemRepository contestProblemRepository,
RankingUserRepository rankingUserRepository,
GroupRepository groupRepository,
IndexSequenceRepository indexSequenceRepository, DTOMapper mapper) {
IndexSequenceRepository indexSequenceRepository,
DTOMapper mapper) {
this.contestRepository = contestRepository;
this.timeCostRepository = timeCostRepository;
this.problemRepository = problemRepository;
Expand All @@ -84,6 +88,16 @@ public ContestDTO create(ContestDTO contestDTO) throws ContestException {

Contest contest = mapper.dtoToEntity(contestDTO);

if (contest.getStartDate().isEqual(LocalDateTime.now())
|| contest.getStartDate().isBefore(LocalDateTime.now())) {
throw new ContestException(ContestException.START_TIME_IS_EARLY_THAN_NOW);
}

if (contest.getStartDate().isEqual(contest.getEndDate())
|| contest.getStartDate().isAfter(contest.getEndDate())) {
throw new ContestException(ContestException.START_TIME_IS_AFTER_THAN_END_TIME);
}

requirePassword(contest);

contest.setCreateDate(LocalDateTime.now());
Expand Down Expand Up @@ -113,14 +127,58 @@ public ContestDTO delete(String id) throws ContestException {
@Override
public ContestDTO update(ContestDTO contestDTO) throws ContestException {
User user = UserContext.getCurrentUser();
Optional<Contest> contestOptional = contestRepository.findByName(contestDTO.getName());
if (contestOptional.isPresent() && !contestOptional.get().getId().equals(contestDTO.getId())) {
throw new ContestException(ContestException.HAVE_SAME_NAME_CONTEST);
Contest contest =
contestRepository
.findById(contestDTO.getId())
.orElseThrow(() -> new ContestException(ContestException.NO_SUCH_CONTEST));

if (ContestStatus.ENDED.equals(contest.getStatus())) {
throw new ContestException(ContestException.CONTEST_IS_ENDED);
}
Contest contest = mapper.dtoToEntity(contestDTO);
contest.setId(contestDTO.getId());

requirePassword(contest);
if (!ContestStatus.PROCESSING.equals(contest.getStatus())) {
if (null != contestDTO.getName()) {
Optional<Contest> contestOptional = contestRepository.findByName(contestDTO.getName());
if (contestOptional.isPresent() && !contestOptional.get().getId().equals(contestDTO.getId())) {
throw new ContestException(ContestException.HAVE_SAME_NAME_CONTEST);
}
contest.setName(contestDTO.getName());
}

if (null != contestDTO.getCouldShare()) {
contest.setCouldShare(contestDTO.getCouldShare());
}

if (null != contestDTO.getContestType()) {
contest.setContestType(ContestType.valueOf(contestDTO.getContestType()));
requirePassword(contest);
}

if (null != contestDTO.getJudgeType()) {
contest.setJudgeType(JudgeType.valueOf(contestDTO.getJudgeType()));
}

if (null != contestDTO.getStatus()) {
// set status, enable, ranking
setContestStatus(contest, ContestStatus.valueOf(contestDTO.getStatus()));
}

if (null != contestDTO.getDescription()) {
contest.setDescription(contestDTO.getDescription());
}

if (null != contestDTO.getStartDate()) {
contest.setStartDate(contestDTO.getStartDate());
}

if (null != contestDTO.getPassword()) {
contest.setPassword(contestDTO.getPassword());
}
}

if (null != contestDTO.getEndDate()) {
contest.setEndDate(contestDTO.getEndDate());
}

contest.setAuthor(user);
return mapper.entityToDTO(contestRepository.save(contest));
Expand Down Expand Up @@ -254,6 +312,14 @@ public PageDTO<ContestDTO> findCriteria(Integer page, Integer size, ContestQuery
return criteriaBuilder.and(predicateList.toArray(p));
};
List<Contest> contestList = contestRepository.findAll(cs, pageable).getContent();
for (Contest contest : contestList) {
if ((contest.getStartDate().isBefore(LocalDateTime.now())
|| contest.getStartDate().isEqual(LocalDateTime.now()))
&& contest.getStatus().equals(ContestStatus.NOT_STARTED)) {
setContestStatus(contest, ContestStatus.PROCESSING);
}
}
contestList = contestRepository.saveAll(contestList);
Long count = contestRepository.count(cs);
List<ContestDTO> contestDTOList = mapper.toContestDTOs(contestList);
return new PageDTO<>(page, size, count, contestDTOList);
Expand All @@ -270,8 +336,10 @@ public List<ProblemDTO> addProblems(List<String> problemIdList, String contestId
List<ContestProblem> contestProblemList = contestProblemRepository.findByContest(contest);
List<Problem> problemList = getProblemList(contest);
for (String problemId : problemIdList) {
Problem problem = problemRepository.findById(problemId)
.orElseThrow(() -> new ProblemException(ProblemException.NO_SUCH_PROBLEM));
Problem problem =
problemRepository
.findById(problemId)
.orElseThrow(() -> new ProblemException(ProblemException.NO_SUCH_PROBLEM));
if (!problemList.contains(problem)) {
ContestProblem contestProblem = new ContestProblem();
contestProblem.setProblem(problem);
Expand Down Expand Up @@ -324,37 +392,50 @@ public ContestDTO setContestStatus(String id, ContestOption option) throws Conte
.findById(id)
.orElseThrow(() -> new ContestException(ContestException.NO_SUCH_CONTEST));

Ranking ranking = contest.getRanking();
switch (option) {
case ENABLE:
setContestStatus(contest, ContestStatus.PROCESSING);
break;
case DISABLE:
setContestStatus(contest, ContestStatus.ENDED);
break;
case RESET:
setContestStatus(contest, ContestStatus.NOT_STARTED);
break;
default:
}
return mapper.entityToDTO(contestRepository.save(contest));
}

private void setContestStatus(Contest contest, ContestStatus status) {
Ranking ranking = contest.getRanking();
switch (status) {
case NOT_STARTED:
contest.setStatus(ContestStatus.NOT_STARTED);
contest.setEnable(false);
rankingUserRepository.deleteAll(rankingUserRepository.findByRanking(ranking));
contest.setRanking(ranking);
break;
case PROCESSING:
contest.setStatus(ContestStatus.PROCESSING);
contest.setEnable(true);
contest.setStartDate(LocalDateTime.now());
rankingUserRepository.deleteAll(rankingUserRepository.findByRanking(ranking));
// initialize rankingUserList
List<RankingUser> rankingUserList = new ArrayList<>();
Iterator<User> userIterator = contest.getUserSet().iterator();
while (userIterator.hasNext()) {
User user = userIterator.next();
for (User user : contest.getUserSet()) {
RankingUser rankingUser = RankingUserFactory.create(user, contest);
rankingUserList.add(rankingUser);
}
ranking.setRankingUserList(rankingUserList);
break;
case DISABLE:
contest.setStatus(ContestStatus.ENDED);
case ENDED:
contest.setEnable(false);
contest.setStatus(ContestStatus.ENDED);
setProblemsVisible(contest);
break;
case RESET:
contest.setStatus(ContestStatus.NOT_STARTED);
contest.setEnable(false);
rankingUserRepository.deleteAll(rankingUserRepository.findByRanking(ranking));
contest.setRanking(ranking);
break;
default:
break;
}
return mapper.entityToDTO(contestRepository.save(contest));
}

@Override
Expand All @@ -365,6 +446,7 @@ public Boolean joinContest(String id, String password) throws ContestException {
.findById(id)
.orElseThrow(() -> new ContestException(ContestException.NO_SUCH_CONTEST));

boolean result = false;
switch (contest.getContestType()) {
case PUBLIC:
if (contest.getEnable()) {
Expand All @@ -379,9 +461,10 @@ public Boolean joinContest(String id, String password) throws ContestException {
ul.add(user);
contest.setUserSet(ul);
contestRepository.save(contest);
return true;
result = true;
break;
case SECRET_WITHOUT_PASSWORD:
return false;
break;
case SECRET_WITH_PASSWORD:
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String encryptPassword = encoder.encode(password);
Expand All @@ -398,12 +481,16 @@ public Boolean joinContest(String id, String password) throws ContestException {
userSet.add(user);
contest.setUserSet(userSet);
contestRepository.save(contest);
return true;
result = true;
} else {
result = false;
}
return false;
break;
default:
return false;

}

return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ public GroupDTO update(GroupDTO groupDTO) throws GroupException, AuthorizationEx
if (existGroupOptional.isPresent()
&& !existGroupOptional.get().getId().equals(groupDTO.getId())) {
throw new GroupException(GroupException.HAVE_SUCH_GROUP);
} else {
Set<User> userSet = group.getUserSet();
Group tempGroup = mapper.dtoToEntity(groupDTO);
tempGroup.setUserSet(userSet);
return mapper.entityToDTO(groupRepository.save(tempGroup));
}
if (null != groupDTO.getName()) {
group.setName(groupDTO.getName());
}
return mapper.entityToDTO(groupRepository.save(group));
}

@Override
Expand Down
Loading

0 comments on commit 8bdd8ad

Please sign in to comment.