Skip to content

Commit

Permalink
Merge pull request #264 from KEEPER31337/Election_관련_Entity_Repositor…
Browse files Browse the repository at this point in the history
…y_생성

Feature/Election 관련 entity repository 생성
  • Loading branch information
JJimini authored Aug 6, 2023
2 parents db18d85 + eeb2a51 commit a2ffdfa
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.keeper.homepage.domain.election.dao;

import com.keeper.homepage.domain.election.entity.ElectionCandidate;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ElectionCandidateRepository extends JpaRepository<ElectionCandidate, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.keeper.homepage.domain.election.dao;

import com.keeper.homepage.domain.election.entity.ElectionChartLog;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ElectionChartLogRepository extends JpaRepository<ElectionChartLog, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.keeper.homepage.domain.election.dao;

import com.keeper.homepage.domain.election.entity.Election;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ElectionRepository extends JpaRepository<Election, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.keeper.homepage.domain.election.dao;

import com.keeper.homepage.domain.election.entity.Election;
import com.keeper.homepage.domain.election.entity.ElectionVoter;
import com.keeper.homepage.domain.election.entity.ElectionVoterPK;
import com.keeper.homepage.domain.member.entity.Member;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ElectionVoterRepository extends JpaRepository<ElectionVoter, ElectionVoterPK> {

Optional <ElectionVoter> findByMemberAndElectionAndIsVoted(Member member, Election election, Boolean isVoted);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.keeper.homepage.domain.election.entity;

import static jakarta.persistence.CascadeType.REMOVE;
import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import com.keeper.homepage.domain.member.entity.Member;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

@DynamicInsert
@DynamicUpdate
@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
@Table(name = "election")
public class Election {

private static final int MAX_NAME_LENGTH = 45;
private static final int MAX_DESCRIPTION_LENGTH = 200;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Long id;

@Column(name = "name", nullable = false, length = MAX_NAME_LENGTH)
private String name;

@Column(name = "description", length = MAX_DESCRIPTION_LENGTH)
private String description;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "creator", nullable = false)
private Member member;

@Column(name = "register_time", nullable = false)
private LocalDateTime registerTime;

@Column(name = "is_available", nullable = false)
private Boolean isAvailable;

@OneToMany(mappedBy = "election", cascade = REMOVE)
private final List<ElectionCandidate> electionCandidates = new ArrayList<>();

@OneToMany(mappedBy = "election")
private final List<ElectionVoter> electionVoters = new ArrayList<>();

@Builder
private Election(String name, String description, Member member,
Boolean isAvailable) {
this.name = name;
this.description = description;
this.member = member;
this.registerTime = LocalDateTime.now();
this.isAvailable = isAvailable;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.keeper.homepage.domain.election.entity;

import static jakarta.persistence.CascadeType.REMOVE;
import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import com.keeper.homepage.domain.member.entity.Member;
import com.keeper.homepage.domain.member.entity.job.MemberJob;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

@DynamicInsert
@DynamicUpdate
@Entity
@NoArgsConstructor(access = PROTECTED)
@Table(name = "election_candidate")
public class ElectionCandidate {

private static final int MAX_DESCRIPTION_LENGTH = 200;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Long id;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "election_id", nullable = false)
private Election election;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "candidate_id", nullable = false)
private Member member;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "member_job_id", nullable = false)
private MemberJob memberJob;

@Column(name = "description", length = MAX_DESCRIPTION_LENGTH)
private String description;

@Column(name = "register_time", nullable = false)
private LocalDateTime registerTime;

@Column(name = "vote_count", nullable = false)
private Long voteCount;

@OneToMany(mappedBy = "electionCandidate", cascade = REMOVE)
private final List<ElectionChartLog> electionChartLogs = new ArrayList<>();

@Builder
private ElectionCandidate(Election election, Member member, MemberJob memberJob,
String description, Long voteCount) {
this.election = election;
this.member = member;
this.memberJob = memberJob;
this.registerTime = LocalDateTime.now();
this.description = description;
this.voteCount = voteCount;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.keeper.homepage.domain.election.entity;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

@DynamicInsert
@DynamicUpdate
@Entity
@NoArgsConstructor(access = PROTECTED)
@Table(name = "election_chart_log")
public class ElectionChartLog {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Long id;

@Column(name = "vote_time", nullable = false)
private LocalDateTime voteTime;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "election_candidate_id", nullable = false)
private ElectionCandidate electionCandidate;

@Builder
private ElectionChartLog(LocalDateTime voteTime, ElectionCandidate electionCandidate) {
this.voteTime = voteTime;
this.electionCandidate = electionCandidate;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.keeper.homepage.domain.election.entity;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import com.keeper.homepage.domain.member.entity.Member;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

@DynamicInsert
@DynamicUpdate
@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
@Table(name = "election_voter")
@IdClass(ElectionVoterPK.class)
public class ElectionVoter {

@Id
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "voter_id", nullable = false)
private Member member;

@Id
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "election_id", nullable = false)
private Election election;

@Column(name = "is_voted", nullable = false)
private Boolean isVoted;

@Builder
private ElectionVoter(Member member, Election election, Boolean isVoted) {
this.member = member;
this.election = election;
this.isVoted = isVoted;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.keeper.homepage.domain.election.entity;

import java.io.Serializable;
import lombok.Getter;

@Getter
public class ElectionVoterPK implements Serializable {

private Long member;
private Long election;

}
24 changes: 24 additions & 0 deletions src/test/java/com/keeper/homepage/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
import com.keeper.homepage.domain.comment.CommentTestHelper;
import com.keeper.homepage.domain.comment.application.CommentService;
import com.keeper.homepage.domain.comment.dao.CommentRepository;
import com.keeper.homepage.domain.election.ElectionTestHelper;
import com.keeper.homepage.domain.election.ElectionVoterTestHelper;
import com.keeper.homepage.domain.election.dao.ElectionCandidateRepository;
import com.keeper.homepage.domain.election.dao.ElectionChartLogRepository;
import com.keeper.homepage.domain.election.dao.ElectionRepository;
import com.keeper.homepage.domain.election.dao.ElectionVoterRepository;
import com.keeper.homepage.domain.ctf.CtfContestTestHelper;
import com.keeper.homepage.domain.ctf.CtfTeamTestHelper;
import com.keeper.homepage.domain.ctf.application.CtfTeamService;
Expand Down Expand Up @@ -223,6 +229,18 @@ public class IntegrationTest {
@SpyBean
protected SurveyReplyExcuseRepository surveyReplyExcuseRepository;

@SpyBean
protected ElectionCandidateRepository electionCandidateRepository;

@SpyBean
protected ElectionChartLogRepository electionChartLogRepository;

@SpyBean
protected ElectionRepository electionRepository;

@SpyBean
protected ElectionVoterRepository electionVoterRepository;

@Autowired
protected FileRepository fileRepository;

Expand Down Expand Up @@ -345,6 +363,12 @@ public class IntegrationTest {
@Autowired
protected SurveyMemberReplyTestHelper surveyMemberReplyTestHelper;

@Autowired
protected ElectionTestHelper electionTestHelper;

@Autowired
protected ElectionVoterTestHelper electionVoterTestHelper;

@Autowired
protected CtfTeamTestHelper ctfTeamTestHelper;

Expand Down
Loading

0 comments on commit a2ffdfa

Please sign in to comment.