Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yj-leez committed Dec 21, 2023
2 parents ed4f6cd + 29e5a1e commit b714494
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.sniff.springvote18th.domain;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.*;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
public class CandidateVote {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum ErrorCode {
INVALID_PASSWORD(HttpStatus.UNAUTHORIZED, ""),
INVALID_VOTE(HttpStatus.UNAUTHORIZED, ""),
TEAM_NOT_FOUND(HttpStatus.NOT_FOUND, ""),
CANDIDATE_NOT_FOUND(HttpStatus.NOT_FOUND, ""),
ALREADY_VOTED(HttpStatus.CONFLICT, "");

private HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.sniff.springvote18th.partleader;

import com.sniff.springvote18th.domain.User;
import com.sniff.springvote18th.partleader.dto.CandidateResponseDto;
import com.sniff.springvote18th.partleader.dto.PartLeaderRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/part-leader")
@RequiredArgsConstructor
public class PartLeaderController {

private final PartLeaderService partLeaderService;

@GetMapping("/vote")
public CandidateResponseDto getCandidateList(@RequestParam String part){
return partLeaderService.getCandidateResult(part);
}

@PostMapping("/vote")
public ResponseEntity votePartLeader(@RequestParam String part, @RequestBody PartLeaderRequestDto dto, @AuthenticationPrincipal User user){
return partLeaderService.votePartLeader(part, dto, user);
}


@GetMapping("/results")
public CandidateResponseDto getCandidateListDesc(@RequestParam String part){
return partLeaderService.getCandidateResultDesc(part);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.sniff.springvote18th.partleader;

import com.sniff.springvote18th.domain.Candidate;
import com.sniff.springvote18th.domain.CandidateVote;
import com.sniff.springvote18th.domain.Part;
import com.sniff.springvote18th.domain.User;
import com.sniff.springvote18th.exception.CustomException;
import com.sniff.springvote18th.exception.ErrorCode;
import com.sniff.springvote18th.partleader.dto.CandidateDto;
import com.sniff.springvote18th.partleader.dto.CandidateResponseDto;
import com.sniff.springvote18th.partleader.dto.PartLeaderRequestDto;
import com.sniff.springvote18th.repository.CandidateRepository;
import com.sniff.springvote18th.repository.CandidateVoteRepository;
import com.sniff.springvote18th.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class PartLeaderService {
private final CandidateRepository candidateRepository;
private final CandidateVoteRepository candidateVoteRepository;
private final UserRepository userRepository;

public CandidateResponseDto getCandidateResult(String part){
List<Candidate> candidates = candidateRepository.getCandidateList(Part.valueOf(part));
List<CandidateDto> candidateDtos = candidates.stream().map(CandidateDto::new).toList();
return new CandidateResponseDto(candidateDtos);
}

public CandidateResponseDto getCandidateResultDesc(String part){
List<Candidate> candidates = candidateRepository.getCandidateListDesc(Part.valueOf(part));
List<CandidateDto> candidateDtos = candidates.stream().map(CandidateDto::new).toList();
return new CandidateResponseDto(candidateDtos);
}

@Transactional
public ResponseEntity votePartLeader(String part, PartLeaderRequestDto dto, User user){
if(!user.isCandidateVoted()){

if(!user.getPart().name().equals(part)) throw new CustomException(ErrorCode.INVALID_VOTE, "해당 파트에는 투표 불가");

if (part.equals("FE")) {
if (!(10 < dto.getId() && dto.getId() <= 20)) {
throw new CustomException(ErrorCode.INVALID_VOTE, "올바르지 않은 id");
}
} else if (part.equals("BE")) {
if (!(0 < dto.getId() && dto.getId() <= 10)) {
throw new CustomException(ErrorCode.INVALID_VOTE, "올바르지 않은 id");
}
}


candidateRepository.updateVoteCount((long)dto.getId());
userRepository.updateIsCandidateVoted(user.getId());

Candidate candidate = candidateRepository.findById((long) dto.getId())
.orElseThrow(() -> new CustomException(ErrorCode.CANDIDATE_NOT_FOUND, "후보자 정보 없음"));
candidateVoteRepository.save(CandidateVote.builder()
.user(user)
.candidate(candidate)
.build());

} else {
throw new CustomException(ErrorCode.ALREADY_VOTED, "이미 투표 완료");
}
return new ResponseEntity(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sniff.springvote18th.partleader.dto;

import com.sniff.springvote18th.domain.Candidate;
import com.sniff.springvote18th.domain.Part;
import lombok.*;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class CandidateDto {
private Long id;
private String name;
private Part part;
private int count;

@Builder
public CandidateDto(Candidate candidate){
this.id = candidate.getId();
this.name = candidate.getName();
this.part = candidate.getPart();
this.count = candidate.getCount();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.sniff.springvote18th.partleader.dto;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class CandidateResponseDto {
private List<CandidateDto> candidateList;

@Builder
public CandidateResponseDto(List<CandidateDto> candidateDtos){ this.candidateList = candidateDtos; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sniff.springvote18th.partleader.dto;

import lombok.Getter;

@Getter
public class PartLeaderRequestDto {
private int id;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package com.sniff.springvote18th.repository;

import com.sniff.springvote18th.domain.Candidate;
import com.sniff.springvote18th.domain.Part;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CandidateRepository extends JpaRepository<Candidate, Long> {
@Query("SELECT c FROM Candidate c WHERE c.part = :part")
List<Candidate> getCandidateList(@Param("part") Part part);

@Query("SELECT c FROM Candidate c WHERE c.part = :part ORDER BY c.count DESC")
List<Candidate> getCandidateListDesc(@Param("part") Part part);

@Modifying
@Query("UPDATE Candidate c SET c.count = c.count + 1 WHERE c.id = :userId")
void updateVoteCount(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public interface UserRepository extends JpaRepository<User, Long> {
@Modifying
@Query("UPDATE User u SET u.isTeamVoted = true WHERE u.id = :userId")
void updateIsTeamVoted(@Param("userId") Long userId);

@Modifying
@Query("UPDATE User u SET u.isCandidateVoted = true WHERE u.id = :userId")
void updateIsCandidateVoted(@Param("userId") Long userId);
}

0 comments on commit b714494

Please sign in to comment.