-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/team-sniff/spring-vote-18th
- Loading branch information
Showing
9 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
src/main/java/com/sniff/springvote18th/domain/CandidateVote.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/sniff/springvote18th/partleader/PartLeaderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/sniff/springvote18th/partleader/PartLeaderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/sniff/springvote18th/partleader/dto/CandidateDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/sniff/springvote18th/partleader/dto/CandidateResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/sniff/springvote18th/partleader/dto/PartLeaderRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/sniff/springvote18th/repository/CandidateRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters