Skip to content

Commit

Permalink
deploy: 3차 배포 (#42)
Browse files Browse the repository at this point in the history
* feat: 개표 결과 저장 기능 구현

* feat: 개표 결과 조회 기능 구현
  • Loading branch information
LEEJaeHyeok97 authored Dec 19, 2024
1 parent 2f9c6bf commit d324223
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.studentvote.domain.user.domain.repository;

import com.studentvote.domain.user.domain.Governance;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface GovernanceRepository extends JpaRepository<Governance, Long> {
Optional<Governance> findByVoteHeadquaterUserId(Long voteHeadquaterUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import com.studentvote.domain.user.domain.repository.UserRepository;
import com.studentvote.domain.vote.domain.Vote;
import com.studentvote.domain.vote.domain.VoteInformation;
import com.studentvote.domain.vote.domain.VoteResult;
import com.studentvote.domain.vote.domain.repository.VoteInformationRepository;
import com.studentvote.domain.vote.domain.repository.VoteRepository;
import com.studentvote.domain.vote.domain.repository.VoteResultRepository;
import com.studentvote.domain.vote.dto.request.CreateVoteRequest;
import com.studentvote.domain.vote.dto.request.RegisterVoteRateRequest;
import com.studentvote.domain.vote.dto.response.GetRateResponse;
import com.studentvote.domain.vote.dto.response.GetResultResponse;
import com.studentvote.domain.vote.exception.AlreadyExistVoteInformationException;
import com.studentvote.domain.vote.exception.AlreadyExistVoteResultException;
import com.studentvote.global.config.s3.S3Service;
import com.studentvote.global.payload.Message;
import java.util.Optional;
Expand All @@ -22,6 +26,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@RequiredArgsConstructor
@Service
Expand All @@ -36,6 +41,7 @@ public class VoteService {
private final VoteRepository voteRepository;
private final GovernanceRepository governanceRepository;
private final UserRepository userRepository;
private final VoteResultRepository voteResultRepository;

@Transactional
public Message createVote(CustomUserDetails userDetails, CreateVoteRequest createVoteRequest) {
Expand Down Expand Up @@ -93,8 +99,6 @@ public Message reset(CustomUserDetails userDetails) {
public Message registerVoteRate(CustomUserDetails userDetails, Long departmentId,
RegisterVoteRateRequest registerVoteRateRequest) {

System.out.println("userDetails.user().getEmail() = " + userDetails.user().getEmail());

User user = userRepository.findByEmail(userDetails.user().getEmail())
.orElseThrow(() -> new IllegalArgumentException("등록되지 않은 유저입니다."));

Expand All @@ -121,4 +125,60 @@ public Page<GetRateResponse> getRate(Long departmentId, Pageable pageable) {

return getRateResponses;
}

@Transactional
public Message postResult(CustomUserDetails userDetails, MultipartFile file) {

User user = userRepository.findByEmail(userDetails.user().getEmail())
.orElseThrow(() -> new IllegalArgumentException("등록되지 않은 유저입니다."));

String image = s3Service.uploadImageToS3(file);

Governance governance = governanceRepository.findByVoteHeadquaterUserId(user.getId())
.orElseThrow(() -> new NullPointerException("해당하는 자치기구가 없습니다."));

Boolean existsedByGovernance = voteResultRepository.ExistsByGovernance(governance);

if (existsedByGovernance) {
throw new AlreadyExistVoteResultException();
}

VoteResult voteResult = VoteResult.of(image, governance);

voteResultRepository.save(voteResult);

return Message
.builder()
.message("개표 결과 저장이 완료되었습니다.")
.build();
}

public GetResultResponse getResult(Long governanceId) {
Governance governance = governanceRepository.findById(governanceId)
.orElseThrow(() -> new IllegalArgumentException("해당하는 자치기구 id가 유효하지 않습니다."));

Optional<VoteResult> optionalVoteResult = voteResultRepository.findByGovernance(governance);

String deptName = governance.getGovernanceName().isEmpty() ? governance.getGovernanceType() : governance.getGovernanceName();

if (optionalVoteResult.isPresent()) {
VoteResult voteResult = optionalVoteResult.get();


GetResultResponse getResultResponse = GetResultResponse
.builder()
.departmentName(deptName)
.resultImageUrl(voteResult.getResultImage())
.build();

return getResultResponse;
}

GetResultResponse
.builder()
.departmentName(deptName)
.resultImageUrl(null)
.build();
return null;
}
}
30 changes: 15 additions & 15 deletions src/main/java/com/studentvote/domain/vote/domain/VoteResult.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.studentvote.domain.vote.domain;

import com.studentvote.domain.common.BaseEntity;
import com.studentvote.domain.user.domain.Governance;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -19,23 +23,19 @@ public class VoteResult extends BaseEntity {
@Column(name = "id", updatable = false)
private Long id;

private String mainCandidateName;
private String resultImage;

private String mainCandidateDepartment;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "governance_id")
private Governance governance;

private String mainCandidateStudentId;

private String viceCandidateName;
public VoteResult(String resultImage, Governance governance) {
this.resultImage = resultImage;
this.governance = governance;
}

private String viceCandidateDepartment;

private String viceCandidateStudentId;

private Integer totalVoters;

private Integer totalVotes;

private Integer approvalVotes;

private Integer oppositionVotes;
public static VoteResult of(String resultImage, Governance governance) {
return new VoteResult(resultImage, governance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.studentvote.domain.vote.domain.repository;

import com.studentvote.domain.user.domain.Governance;
import com.studentvote.domain.vote.domain.VoteResult;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface VoteResultRepository extends JpaRepository<VoteResult, Long> {
Optional<VoteResult> findByGovernance(Governance governance);

@Query("select count(v) > 0 from VoteResult v where v.governance = :governance")
Boolean ExistsByGovernance(Governance governance);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.studentvote.domain.vote.dto.response;

import lombok.Builder;

public record GetResultResponse(
String departmentName,
String resultImageUrl
) {

@Builder
public GetResultResponse(String departmentName, String resultImageUrl) {
this.departmentName = departmentName;
this.resultImageUrl = resultImageUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.studentvote.domain.vote.exception;

public class AlreadyExistVoteResultException extends RuntimeException {
public AlreadyExistVoteResultException() {
super("이미 등록된 개표 결과 이미지가 존재합니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.studentvote.domain.vote.dto.request.CreateVoteRequest;
import com.studentvote.domain.vote.dto.request.RegisterVoteRateRequest;
import com.studentvote.domain.vote.dto.response.GetRateResponse;
import com.studentvote.domain.vote.dto.response.GetResultResponse;
import com.studentvote.global.payload.Message;
import com.studentvote.global.payload.ResponseCustom;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -20,7 +21,9 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "Vote", description = "Vote API")
@RestController
Expand Down Expand Up @@ -63,4 +66,19 @@ public ResponseCustom<Page<GetRateResponse>> getRate(
) {
return ResponseCustom.OK(voteService.getRate(departmentId, pageable));
}

@Operation(summary = "개표 결과 등록", description = "각 단과대/학과 별로 개표 결과를 등록합니다.")
@PostMapping("/result")
public ResponseCustom<Message> postResult(
@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestPart("file") MultipartFile file
) {
return ResponseCustom.OK(voteService.postResult(userDetails, file));
}

@Operation(summary = "개표 결과 조회", description = "각 단과대/학과 별 개표 결과를 조회합니다.")
@GetMapping("/result/{governanceId}")
public ResponseCustom<GetResultResponse> getResult(@PathVariable Long governanceId) {
return ResponseCustom.OK(voteService.getResult(governanceId));
}
}

0 comments on commit d324223

Please sign in to comment.