Skip to content

Commit

Permalink
Merge pull request #472 from KEEPER31337/feature/ctf_카테고리_여러개로_설정되도록_변경
Browse files Browse the repository at this point in the history
Feature/ctf 카테고리 여러개로 설정되도록 변경
  • Loading branch information
shkisme authored Dec 23, 2022
2 parents 265355e + cab11ae commit 6c815ca
Show file tree
Hide file tree
Showing 22 changed files with 423 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package keeper.project.homepage.ctf.dto;

import static java.util.stream.Collectors.toList;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.time.LocalDateTime;
import java.util.ArrayList;
import keeper.project.homepage.ctf.entity.CtfChallengeCategoryEntity;
import keeper.project.homepage.ctf.entity.CtfChallengeEntity;
import keeper.project.homepage.ctf.entity.CtfChallengeTypeEntity;
import keeper.project.homepage.ctf.entity.CtfContestEntity;
Expand Down Expand Up @@ -38,12 +39,12 @@ public class CtfChallengeAdminDto extends CtfChallengeDto {
private CtfDynamicChallengeInfoDto dynamicInfo;

public CtfChallengeEntity toEntity(CtfContestEntity contest, CtfChallengeTypeEntity type,
CtfChallengeCategoryEntity category, FileEntity fileEntity, MemberEntity creator) {
FileEntity fileEntity, MemberEntity creator) {
return CtfChallengeEntity.builder()
.name(title)
.description(content)
.ctfContestEntity(contest)
.ctfChallengeCategoryEntity(category)
.ctfChallengeHasCtfChallengeCategoryList(new ArrayList<>())
.ctfChallengeTypeEntity(type)
.isSolvable(isSolvable)
.registerTime(LocalDateTime.now())
Expand All @@ -56,8 +57,6 @@ public CtfChallengeEntity toEntity(CtfContestEntity contest, CtfChallengeTypeEnt
}

public static CtfChallengeAdminDto toDto(CtfChallengeEntity challenge, Long solvedTeamCount) {
CtfChallengeCategoryDto category = CtfChallengeCategoryDto.toDto(
challenge.getCtfChallengeCategoryEntity());
CtfChallengeTypeDto type = CtfChallengeTypeDto.toDto(
challenge.getCtfChallengeTypeEntity());
FileDto file = FileDto.toDto(
Expand All @@ -70,7 +69,9 @@ public static CtfChallengeAdminDto toDto(CtfChallengeEntity challenge, Long solv
.title(challenge.getName())
.content(challenge.getDescription())
.contestId(challenge.getCtfContestEntity().getId())
.category(category)
.categories(challenge.getCtfChallengeHasCtfChallengeCategoryList().stream()
.map(CtfChallengeCategoryDto::toDto)
.collect(toList()))
.type(type)
.flag(getVirtualTeamFlag(challenge).getContent())
.isSolvable(challenge.getIsSolvable())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import keeper.project.homepage.ctf.entity.CtfChallengeCategoryEntity;
import keeper.project.homepage.ctf.entity.CtfChallengeHasCtfChallengeCategoryEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -24,10 +25,24 @@ public class CtfChallengeCategoryDto {
@JsonProperty(access = Access.READ_ONLY)
private String name;

public static CtfChallengeCategoryEntity toEntity(CtfChallengeCategoryDto ctfChallengeCategoryDto){
return CtfChallengeCategoryEntity.builder()
.id(ctfChallengeCategoryDto.getId())
.name(ctfChallengeCategoryDto.getName())
.build();
}

public static CtfChallengeCategoryDto toDto(CtfChallengeCategoryEntity ctfChallengeCategoryEntity) {
return CtfChallengeCategoryDto.builder()
.id(ctfChallengeCategoryEntity.getId())
.name(ctfChallengeCategoryEntity.getName())
.build();
}

public static CtfChallengeCategoryDto toDto(CtfChallengeHasCtfChallengeCategoryEntity ctfChallengeHasCtfChallengeCategoryEntity) {
return CtfChallengeCategoryDto.builder()
.id(ctfChallengeHasCtfChallengeCategoryEntity.getCategory().getId())
.name(ctfChallengeHasCtfChallengeCategoryEntity.getCategory().getName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package keeper.project.homepage.ctf.dto;

import static java.util.stream.Collectors.toList;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import keeper.project.homepage.ctf.entity.CtfChallengeEntity;
Expand Down Expand Up @@ -32,16 +33,17 @@ public class CtfChallengeDto extends CtfCommonChallengeDto {

public static CtfChallengeDto toDto(CtfChallengeEntity challenge, Long solvedTeamCount,
Boolean isSolved, CtfFlagEntity ctfFlagEntity) {
CtfChallengeCategoryDto category = CtfChallengeCategoryDto.toDto(
challenge.getCtfChallengeCategoryEntity());
FileDto file = FileDto.toDto(challenge.getFileEntity());

return CtfChallengeDto.builder()
.challengeId(challenge.getId())
.title(challenge.getName())
.content(challenge.getDescription())
.contestId(challenge.getCtfContestEntity().getId())
.category(category)
.categories(challenge.getCtfChallengeHasCtfChallengeCategoryList()
.stream()
.map(CtfChallengeCategoryDto::toDto)
.collect(toList()))
.creatorName(challenge.getCreator().getNickName())
.score(challenge.getScore())
.solvedTeamCount(solvedTeamCount)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package keeper.project.homepage.ctf.dto;

import static java.util.stream.Collectors.toList;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import java.time.LocalDateTime;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import keeper.project.homepage.ctf.entity.CtfChallengeEntity;
Expand All @@ -28,7 +31,7 @@ public class CtfCommonChallengeDto {

protected String title;
protected Long score;
protected CtfChallengeCategoryDto category;
protected List<CtfChallengeCategoryDto> categories;
protected Long contestId;
@Max(MAX_SUBMIT_COUNT)
@Min(MIN_SUBMIT_COUNT)
Expand All @@ -48,14 +51,15 @@ public class CtfCommonChallengeDto {

public static CtfCommonChallengeDto toDto(CtfChallengeEntity challenge, Boolean isSolved,
CtfFlagEntity ctfFlagEntity) {
CtfChallengeCategoryDto category = CtfChallengeCategoryDto.toDto(
challenge.getCtfChallengeCategoryEntity());

return CtfCommonChallengeDto.builder()
.challengeId(challenge.getId())
.title(challenge.getName())
.contestId(challenge.getCtfContestEntity().getId())
.category(category)
.categories(challenge.getCtfChallengeHasCtfChallengeCategoryList()
.stream()
.map(CtfChallengeCategoryDto::toDto)
.collect(toList()))
.score(challenge.getScore())
.isSolved(isSolved)
.remainedSubmitCount(ctfFlagEntity.getRemainedSubmitCount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public class CtfChallengeEntity {
@JoinColumn(name = "type_id", nullable = false)
CtfChallengeTypeEntity ctfChallengeTypeEntity;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
CtfChallengeCategoryEntity ctfChallengeCategoryEntity;
@Builder.Default
@OneToMany(mappedBy = "challenge", cascade = CascadeType.REMOVE)
List<CtfChallengeHasCtfChallengeCategoryEntity> ctfChallengeHasCtfChallengeCategoryList = new ArrayList<>();

@Column(nullable = false)
@Setter
Expand Down Expand Up @@ -90,4 +90,8 @@ public class CtfChallengeEntity {
@PrimaryKeyJoinColumn
@Setter
CtfDynamicChallengeInfoEntity dynamicChallengeInfoEntity;

public void addCtfChallengeHasCtfChallengeCategory(CtfChallengeHasCtfChallengeCategoryEntity ctfChallengeHasCtfChallengeCategoryEntity) {
this.getCtfChallengeHasCtfChallengeCategoryList().add(ctfChallengeHasCtfChallengeCategoryEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package keeper.project.homepage.ctf.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@IdClass(CtfChallengeHasCtfChallengeCategoryEntityPK.class) // 정의한 idclass 주입
@Table(name = "ctf_challenge_has_ctf_challenge_category")
public class CtfChallengeHasCtfChallengeCategoryEntity implements Serializable {

@Id
@ManyToOne(targetEntity = CtfChallengeEntity.class, fetch = FetchType.LAZY)
@JoinColumn(name = "ctf_challenge_id")
private CtfChallengeEntity challenge;

@Id
@ManyToOne(targetEntity = CtfChallengeCategoryEntity.class, fetch = FetchType.LAZY)
@JoinColumn(name = "ctf_challenge_category_id")
private CtfChallengeCategoryEntity category;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package keeper.project.homepage.ctf.entity;

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CtfChallengeHasCtfChallengeCategoryEntityPK implements Serializable {

private Long challenge;
private Long category;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package keeper.project.homepage.ctf.repository;

import keeper.project.homepage.ctf.entity.CtfChallengeHasCtfChallengeCategoryEntity;
import keeper.project.homepage.ctf.entity.CtfChallengeHasCtfChallengeCategoryEntityPK;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CtfChallengeHasCtfChallengeCategoryRepository extends
JpaRepository<CtfChallengeHasCtfChallengeCategoryEntity, CtfChallengeHasCtfChallengeCategoryEntityPK> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import keeper.project.homepage.ctf.dto.CtfChallengeAdminDto;
import keeper.project.homepage.ctf.dto.CtfChallengeCategoryDto;
import keeper.project.homepage.ctf.dto.CtfContestAdminDto;
import keeper.project.homepage.ctf.dto.CtfDynamicChallengeInfoDto;
import keeper.project.homepage.ctf.dto.CtfProbMakerDto;
import keeper.project.homepage.ctf.dto.CtfSubmitLogDto;
import keeper.project.homepage.ctf.entity.CtfChallengeCategoryEntity;
import keeper.project.homepage.ctf.entity.CtfChallengeEntity;
import keeper.project.homepage.ctf.entity.CtfChallengeHasCtfChallengeCategoryEntity;
import keeper.project.homepage.ctf.entity.CtfChallengeTypeEntity;
import keeper.project.homepage.ctf.entity.CtfContestEntity;
import keeper.project.homepage.ctf.entity.CtfDynamicChallengeInfoEntity;
Expand All @@ -24,6 +26,7 @@
import keeper.project.homepage.ctf.exception.CustomCtfChallengeNotFoundException;
import keeper.project.homepage.ctf.exception.CustomCtfTypeNotFoundException;
import keeper.project.homepage.ctf.repository.CtfChallengeCategoryRepository;
import keeper.project.homepage.ctf.repository.CtfChallengeHasCtfChallengeCategoryRepository;
import keeper.project.homepage.ctf.repository.CtfChallengeRepository;
import keeper.project.homepage.ctf.repository.CtfChallengeTypeRepository;
import keeper.project.homepage.ctf.repository.CtfContestRepository;
Expand Down Expand Up @@ -63,6 +66,8 @@ public class CtfAdminService {
private final CtfContestRepository ctfContestRepository;
private final CtfTeamRepository ctfTeamRepository;
private final CtfChallengeCategoryRepository ctfChallengeCategoryRepository;

private final CtfChallengeHasCtfChallengeCategoryRepository ctfChallengeHasCtfChallengeCategoryRepository;
private final CtfSubmitLogRepository ctfSubmitLogRepository;
private final CtfChallengeTypeRepository ctfChallengeTypeRepository;
private final CtfChallengeRepository challengeRepository;
Expand Down Expand Up @@ -111,6 +116,8 @@ public CtfProbMakerDto designateProbMaker(CtfProbMakerDto probMakerDto) {
@Transactional
public CtfChallengeAdminDto createChallenge(CtfChallengeAdminDto challengeAdminDto) {
CtfChallengeEntity newChallenge = createChallengeEntity(challengeAdminDto);

setChallengeCategory(newChallenge, challengeAdminDto);
if (ctfUtilService.isTypeDynamic(newChallenge)) {
trySetDynamicInfoInChallenge(newChallenge, challengeAdminDto);
}
Expand Down Expand Up @@ -317,14 +324,32 @@ private Long getCtfId(CtfChallengeEntity challenge) {
private CtfChallengeEntity createChallengeEntityWithFileEntity(
CtfChallengeAdminDto challengeAdminDto, FileEntity fileEntity) {
CtfContestEntity contest = getCtfContestEntity(challengeAdminDto.getContestId());
CtfChallengeCategoryEntity category = getCategoryEntity(challengeAdminDto);

CtfChallengeTypeEntity type = getTypeEntity(challengeAdminDto);
MemberEntity creator = authService.getMemberEntityWithJWT();

CtfChallengeEntity challenge = challengeAdminDto
.toEntity(contest, type, category, fileEntity, creator);
.toEntity(contest, type, fileEntity, creator);

return challengeRepository.save(challenge);
}

private void setChallengeCategory(CtfChallengeEntity challenge,
CtfChallengeAdminDto challengeAdminDto) {
List<CtfChallengeCategoryEntity> ctfChallengeCategoryEntityList = challengeAdminDto.getCategories()
.stream()
.map(CtfChallengeCategoryDto::toEntity).toList();

for (CtfChallengeCategoryEntity ctfChallengeCategory : ctfChallengeCategoryEntityList) {
challenge.addCtfChallengeHasCtfChallengeCategory(ctfChallengeHasCtfChallengeCategoryRepository
.save(CtfChallengeHasCtfChallengeCategoryEntity
.builder()
.challenge(challenge)
.category(ctfChallengeCategory)
.build()));
}
}

private CtfChallengeEntity createChallengeEntity(CtfChallengeAdminDto challengeAdminDto) {
return createChallengeEntityWithFileEntity(challengeAdminDto, null);
}
Expand All @@ -335,9 +360,10 @@ private CtfChallengeTypeEntity getTypeEntity(CtfChallengeAdminDto challengeAdmin
.orElseThrow(CustomCtfTypeNotFoundException::new);
}

private CtfChallengeCategoryEntity getCategoryEntity(CtfChallengeAdminDto challengeAdminDto) {
private CtfChallengeCategoryEntity getCategoryEntity(
CtfChallengeCategoryDto ctfChallengeCategoryDto) {
return ctfChallengeCategoryRepository
.findById(challengeAdminDto.getCategory().getId())
.findById(ctfChallengeCategoryDto.getId())
.orElseThrow(CustomCtfCategoryNotFoundException::new);
}

Expand Down
Loading

0 comments on commit 6c815ca

Please sign in to comment.