Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: 알림 목록 페이지 구현 (Notification Page) #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
parksinhae wants to merge 14 commits into callicoder:master
base: master
Choose a base branch
Loading
from eunhye-ahn:frontend/notification-page
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
74c13ef
db 연결
eunhye-ahn May 15, 2025
fb6753a
그룹 생성
eunhye-ahn May 15, 2025
591b045
내 그룹 목록 조회 api
eunhye-ahn May 19, 2025
44be651
입장코드로 그룹 참여 api
eunhye-ahn May 19, 2025
1d9dfc6
그룹 상세 조회 api
eunhye-ahn May 20, 2025
4841b43
그룹 내 투표 목록 조회 api
eunhye-ahn May 21, 2025
f51663d
그룹투표생성 + 목록 조회 api
eunhye-ahn May 21, 2025
fc15eff
그룹 투표 상세조회 api
eunhye-ahn May 21, 2025
264d1c4
Merge conflict resolved
eunhye-ahn May 21, 2025
b59c69c
Resolve merge conflicts in controller and service
eunhye-ahn May 21, 2025
ea0b292
투표참여 api
eunhye-ahn May 21, 2025
25835ef
댓글 생성+조회+삭제 api
eunhye-ahn May 21, 2025
a01b8b5
알림 전체 목록 페이지 추가
parksinhae May 24, 2025
9999d20
불필요한 캐시 파일 Git 추적 제거 및 .gitignore 추가
parksinhae May 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
그룹 투표 상세조회 api
  • Loading branch information
eunhye-ahn committed May 21, 2025
commit fc15eff29fd7818a72e9ed53b517f1e5495e807f
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ public ResponseEntity<PagedResponse<PollResponse>> getAllPollInGroup(@PathVariab
return ResponseEntity.ok(response);
}

@GetMapping("/{pollId}")
public ResponseEntity<PollResponse> getPollByIdInGroup(@PathVariable Long groupId,
@PathVariable Long pollId,
@CurrentUser UserPrincipal userPrincipal){
PollResponse response = pollService.getPollByIdInGroup(pollId, groupId, userPrincipal);
return ResponseEntity.ok(response);
}

}
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,38 @@ public class PollService {
@Autowired
private GroupMemberRepository groupMemberRepository;


public PagedResponse<PollResponse> getAllPolls(UserPrincipal currentUser, int page, int size) {
validatePageNumberAndSize(page, size);

// Retrieve Polls
Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createdAt");
Page<Poll> polls = pollRepository.findAll(pageable);

private PagedResponse<PollResponse> mapPollPagetoPageResponse(UserPrincipal currentUser, Page<Poll> polls) {
if(polls.getNumberOfElements() == 0) {
return new PagedResponse<>(Collections.emptyList(), polls.getNumber(),
polls.getSize(), polls.getTotalElements(), polls.getTotalPages(), polls.isLast());
}

// Map Polls to PollResponses containing vote counts and poll creator details
List<Long> pollIds = polls.map(Poll::getId).getContent();
Map<Long, Long> choiceVoteCountMap = getChoiceVoteCountMap(pollIds);
Map<Long, Long> pollUserVoteMap = getPollUserVoteMap(currentUser, pollIds);
Map<Long, User> creatorMap = getPollCreatorMap(polls.getContent());

List<PollResponse> pollResponses = polls.map(poll -> {
return ModelMapper.mapPollToPollResponse(poll,
choiceVoteCountMap,
creatorMap.get(poll.getCreatedBy()),
pollUserVoteMap == null ? null : pollUserVoteMap.getOrDefault(poll.getId(), null));
}).getContent();
List<PollResponse> pollResponses = polls.map(poll -> ModelMapper.mapPollToPollResponse(
poll,
choiceVoteCountMap,
creatorMap.get(poll.getCreatedBy()),
pollUserVoteMap == null ? null : pollUserVoteMap.getOrDefault(poll.getId(), null)
)).getContent();

return new PagedResponse<>(pollResponses, polls.getNumber(),
polls.getSize(), polls.getTotalElements(), polls.getTotalPages(), polls.isLast());
}

public PagedResponse<PollResponse> getAllPolls(UserPrincipal currentUser, int page, int size) {
validatePageNumberAndSize(page, size);

// Retrieve Polls
Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createdAt");
Page<Poll> polls = pollRepository.findAll(pageable);

return mapPollPagetoPageResponse(currentUser, polls);
}

public PagedResponse<PollResponse> getAllPollsInGroup(Long groupId, UserPrincipal userPrincipal, int page, int size) {
//그룹 유효성검사
Group group = groupRepository.findById(groupId)
Expand All @@ -95,28 +97,7 @@ public PagedResponse<PollResponse> getAllPollsInGroup(Long groupId, UserPrincipa
//결과 데이터 가져오기
Page<Poll> polls = pollRepository.findByGroupId(groupId, pageable);

if(polls.getNumberOfElements() == 0) {
return new PagedResponse<>(Collections.emptyList(), polls.getNumber(),
polls.getSize(), polls.getTotalElements(), polls.getTotalPages(), polls.isLast());
}

// Map Polls to PollResponses containing vote counts and poll creator details
List<Long> pollIds = polls.map(Poll::getId).getContent();
Map<Long, Long> choiceVoteCountMap = getChoiceVoteCountMap(pollIds);
Map<Long, Long> pollUserVoteMap = getPollUserVoteMap(userPrincipal, pollIds);
Map<Long, User> creatorMap = getPollCreatorMap(polls.getContent());


List<PollResponse> pollResponses = polls.map(poll -> {
return ModelMapper.mapPollToPollResponse(poll,
choiceVoteCountMap,
creatorMap.get(poll.getCreatedBy()),
pollUserVoteMap == null ? null : pollUserVoteMap.getOrDefault(poll.getId(), null));
}).getContent();

return new PagedResponse<>(pollResponses, polls.getNumber(),
polls.getSize(), polls.getTotalElements(), polls.getTotalPages(), polls.isLast());

return mapPollPagetoPageResponse(userPrincipal, polls);
}

public PagedResponse<PollResponse> getPollsCreatedBy(String username, UserPrincipal currentUser, int page, int size) {
Expand Down Expand Up @@ -188,57 +169,50 @@ public PagedResponse<PollResponse> getPollsVotedBy(String username, UserPrincipa
}


public Poll createPoll(PollRequest pollRequest) {
private Poll createPollInternal(PollRequest request, Long createdBy, Group group) {
Poll poll = new Poll();
poll.setQuestion(pollRequest.getQuestion());

pollRequest.getChoices().forEach(choiceRequest -> {
poll.addChoice(new Choice(choiceRequest.getText()));
});

Instant now = Instant.now();
Instant expirationDateTime = now.plus(Duration.ofDays(pollRequest.getPollLength().getDays()))
.plus(Duration.ofHours(pollRequest.getPollLength().getHours()));
poll.setQuestion(request.getQuestion());

poll.setExpirationDateTime(expirationDateTime);

return pollRepository.save(poll);
}
if (createdBy != null) {
poll.setCreatedBy(createdBy);
}

//그룹 투표 생성
public Poll createPollInGroup(Long groupId, PollRequest request, UserPrincipal userPrincipal) {
//그룹 엔티티 조회
Group group = groupRepository.findById(groupId)
.orElseThrow(() -> new ResourceNotFoundException("Group", "id", groupId));
//투표 객체 생성
Poll poll = new Poll();
poll.setQuestion(request.getQuestion());
if (group != null) {
poll.setGroup(group);
poll.setCreatedBy(userPrincipal.getId());
}

//선택지 추가
request.getChoices().forEach(choiceRequest -> {
poll.addChoice(new Choice(choiceRequest.getText()));
});

//종료시간 계산
Instant now = Instant.now();
Instant expirationDateTime = now.plus(Duration.ofDays(request.getPollLength().getDays()))
.plus(Duration.ofHours(request.getPollLength().getHours()));
poll.setExpirationDateTime(expirationDateTime);


//저장
return pollRepository.save(poll);
}


public PollResponse getPollById(Long pollId, UserPrincipal currentUser) {
Poll poll = pollRepository.findById(pollId).orElseThrow(
() -> new ResourceNotFoundException("Poll", "id", pollId));
public Poll createPoll(PollRequest pollRequest) {
return createPollInternal(pollRequest, null, null);
}

// Retrieve Vote Counts of every choice belonging to the current poll
List<ChoiceVoteCount> votes = voteRepository.countByPollIdGroupByChoiceId(pollId);
//그룹 투표 생성
public Poll createPollInGroup(Long groupId, PollRequest request, UserPrincipal userPrincipal) {
//그룹 엔티티 조회
Group group = groupRepository.findById(groupId)
.orElseThrow(() -> new ResourceNotFoundException("Group", "id", groupId));
boolean isMember = groupMemberRepository.existsByUserIdAndGroupId(userPrincipal.getId(), groupId);
if(!isMember) {
throw new BadRequestException("해당 그룹에 가입된 자만 투표할 수 있습니다.");
}
//저장
return createPollInternal(request, userPrincipal.getId(), group);
}

public PollResponse buildPollResponse(Poll poll, UserPrincipal currentUser) {
List<ChoiceVoteCount> votes = voteRepository.countByPollIdGroupByChoiceId(poll.getId());

Map<Long, Long> choiceVotesMap = votes.stream()
.collect(Collectors.toMap(ChoiceVoteCount::getChoiceId, ChoiceVoteCount::getVoteCount));
Expand All @@ -250,13 +224,38 @@ public PollResponse getPollById(Long pollId, UserPrincipal currentUser) {
// Retrieve vote done by logged in user
Vote userVote = null;
if(currentUser != null) {
userVote = voteRepository.findByUserIdAndPollId(currentUser.getId(), pollId);
userVote = voteRepository.findByUserIdAndPollId(currentUser.getId(), poll.getId());
}

return ModelMapper.mapPollToPollResponse(poll, choiceVotesMap,
creator, userVote != null ? userVote.getChoice().getId(): null);
}

public PollResponse getPollById(Long pollId, UserPrincipal currentUser) {
Poll poll = pollRepository.findById(pollId).orElseThrow(
() -> new ResourceNotFoundException("Poll", "id", pollId));

return buildPollResponse(poll, currentUser);
}

public PollResponse getPollByIdInGroup(Long pollId, Long groupId, UserPrincipal currentUser) {
Group group = groupRepository.findById(groupId)
.orElseThrow(() -> new ResourceNotFoundException("Group", "id", groupId));
boolean isMember = groupMemberRepository.existsByUserIdAndGroupId(currentUser.getId(), groupId);
if(!isMember) {
throw new BadRequestException("해당 그룹에 가입된 사용자만 투표를 조회할 수 있습니다.");
}

Poll poll = pollRepository.findById(pollId).orElseThrow(
() -> new ResourceNotFoundException("Poll", "id", pollId)
);
if(poll.getGroup() != null && !poll.getGroup().getId().equals(groupId)) {
throw new BadRequestException("해당 투표는 요청한 그룹에 속하지 않습니다.");
}

return buildPollResponse(poll, currentUser);
}

public PollResponse castVoteAndGetUpdatedPoll(Long pollId, VoteRequest voteRequest, UserPrincipal currentUser) {
Poll poll = pollRepository.findById(pollId)
.orElseThrow(() -> new ResourceNotFoundException("Poll", "id", pollId));
Expand Down

AltStyle によって変換されたページ (->オリジナル) /