Revert "feat: track poll votes"

This reverts commit 23582934fa.
This commit is contained in:
tim
2025-08-30 12:05:35 +08:00
parent a57f3e6406
commit c3ae97f8ba
5 changed files with 9 additions and 52 deletions

View File

@@ -12,5 +12,5 @@ public class PollDto {
private List<String> options;
private Map<Integer, Integer> votes;
private LocalDateTime endTime;
private Map<Integer, List<AuthorDto>> participants;
private List<AuthorDto> participants;
}

View File

@@ -6,12 +6,10 @@ import com.openisle.dto.PostSummaryDto;
import com.openisle.dto.ReactionDto;
import com.openisle.dto.LotteryDto;
import com.openisle.dto.PollDto;
import com.openisle.dto.AuthorDto;
import com.openisle.model.CommentSort;
import com.openisle.model.Post;
import com.openisle.model.LotteryPost;
import com.openisle.model.PollPost;
import com.openisle.model.PollParticipant;
import com.openisle.model.User;
import com.openisle.service.CommentService;
import com.openisle.service.ReactionService;
@@ -21,7 +19,6 @@ import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** Mapper responsible for converting posts into DTOs. */
@@ -105,10 +102,7 @@ public class PostMapper {
p.setOptions(pp.getOptions());
p.setVotes(pp.getVotes());
p.setEndTime(pp.getEndTime());
Map<Integer, List<AuthorDto>> participants = pp.getParticipants().stream()
.collect(Collectors.groupingBy(PollParticipant::getOptionIndex,
Collectors.mapping(ppart -> userMapper.toAuthorDto(ppart.getUser()), Collectors.toList())));
p.setParticipants(participants);
p.setParticipants(pp.getParticipants().stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
dto.setPoll(p);
}
}

View File

@@ -1,33 +0,0 @@
package com.openisle.model;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* Represents a single vote in a poll, capturing which user selected which option.
*/
@Entity
@Table(name = "poll_participants")
@Getter
@Setter
@NoArgsConstructor
public class PollParticipant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = false)
private PollPost post;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
@Column(name = "option_index", nullable = false)
private int optionIndex;
}

View File

@@ -30,8 +30,11 @@ public class PollPost extends Post {
@Column(name = "vote_count")
private Map<Integer, Integer> votes = new HashMap<>();
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<PollParticipant> participants = new HashSet<>();
@ManyToMany
@JoinTable(name = "poll_participants",
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "user_id"))
private Set<User> participants = new HashSet<>();
@Column
private LocalDateTime endTime;

View File

@@ -10,7 +10,6 @@ import com.openisle.model.Comment;
import com.openisle.model.NotificationType;
import com.openisle.model.LotteryPost;
import com.openisle.model.PollPost;
import com.openisle.model.PollParticipant;
import com.openisle.repository.PostRepository;
import com.openisle.repository.LotteryPostRepository;
import com.openisle.repository.PollPostRepository;
@@ -294,19 +293,13 @@ public class PostService {
}
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
boolean alreadyVoted = post.getParticipants().stream()
.anyMatch(p -> p.getUser().equals(user));
if (alreadyVoted) {
if (post.getParticipants().contains(user)) {
throw new IllegalArgumentException("User already voted");
}
if (optionIndex < 0 || optionIndex >= post.getOptions().size()) {
throw new IllegalArgumentException("Invalid option");
}
PollParticipant participant = new PollParticipant();
participant.setPost(post);
participant.setUser(user);
participant.setOptionIndex(optionIndex);
post.getParticipants().add(participant);
post.getParticipants().add(user);
post.getVotes().merge(optionIndex, 1, Integer::sum);
return pollPostRepository.save(post);
}