From 12dd49279bf0af50183c7603a44a5be188689f30 Mon Sep 17 00:00:00 2001 From: moab01567 Date: Tue, 28 Oct 2025 14:26:35 +0100 Subject: [PATCH 1/5] added new endpoint --- .../meeting/client/voting/VotingClient.java | 33 +++++++++++++++---- .../src/main/resources/application-dev.yml | 1 + .../src/main/resources/application-prod.yml | 1 + .../voting/VotingInternalController.java | 10 ++++-- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/meeting/src/main/java/com/exam_microservices/meeting/client/voting/VotingClient.java b/meeting/src/main/java/com/exam_microservices/meeting/client/voting/VotingClient.java index 4b3c84d9..670107e5 100644 --- a/meeting/src/main/java/com/exam_microservices/meeting/client/voting/VotingClient.java +++ b/meeting/src/main/java/com/exam_microservices/meeting/client/voting/VotingClient.java @@ -1,5 +1,6 @@ package com.exam_microservices.meeting.client.voting; +import com.exam_microservices.meeting.meeting.MeetingStatus; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.*; @@ -12,15 +13,15 @@ public class VotingClient { private final RestTemplate restTemplate; - private final String microServiceInternalSecret; - private final String postVotingUrl; + @Value("${rest.internal.secret}") + private String microServiceInternalSecret; + @Value("${rest.votingClient.postVoting}") + private String postVotingUrl; + @Value("${rest.votingClient.putMeetingStatusToVoting}") + private String putMeetingStatusInVotingUrl; - public VotingClient(RestTemplateBuilder builder, - @Value("${rest.internal.secret}") String microServiceInternalSecret, - @Value("${rest.votingClient.postVoting}") String postVotingUrl) { + public VotingClient(RestTemplateBuilder builder) { this.restTemplate = builder.build(); - this.microServiceInternalSecret = microServiceInternalSecret; - this.postVotingUrl = postVotingUrl; } public void startVotingRound(Long meetingId, String question, List participantsIds){ @@ -41,4 +42,22 @@ public void startVotingRound(Long meetingId, String question, List partici request, String.class); } + + public void sendMeetingStatusToVoting(Long meetingId, MeetingStatus meetingStatus){ + // Creating Request Body and Getting Url From application file + PutMeetingStatusDTO putMeetingStatus = PutMeetingStatusDTO.builder() + .meetingId(meetingId) + .meetingStatus(meetingStatus) + .build(); + HttpHeaders headers = new HttpHeaders(); + headers.set(HttpHeaders.AUTHORIZATION, microServiceInternalSecret); + headers.setContentType(MediaType.APPLICATION_JSON); + // making a request to voting service :) + HttpEntity request = new HttpEntity<>(putMeetingStatus,headers); + ResponseEntity response = restTemplate.exchange( + putMeetingStatusInVotingUrl, // the url can be found in application-prod.yml file + HttpMethod.PUT, + request, + String.class); + } } diff --git a/meeting/src/main/resources/application-dev.yml b/meeting/src/main/resources/application-dev.yml index 85a91e4d..87de2661 100644 --- a/meeting/src/main/resources/application-dev.yml +++ b/meeting/src/main/resources/application-dev.yml @@ -46,6 +46,7 @@ rest: secret: ${INTERNAL_SECRET} votingClient: postVoting: ${GATEWAY_URL}/service/voting + putMeetingStatusToVoting: ${GATEWAY_URL}/service/voting/meetingStatus housingClient: postHousingInvitation: ${GATEWAY_URL}/service/housing/invitation diff --git a/meeting/src/main/resources/application-prod.yml b/meeting/src/main/resources/application-prod.yml index 7b17046d..b8456c01 100644 --- a/meeting/src/main/resources/application-prod.yml +++ b/meeting/src/main/resources/application-prod.yml @@ -39,6 +39,7 @@ rest: secret: ${INTERNAL_SECRET} votingClient: postVoting: ${GATEWAY_URL}/service/voting + putMeetingStatusToVoting: ${GATEWAY_URL}/service/voting/meetingStatus housingClient: postHousingInvitation: ${GATEWAY_URL}/service/housing/invitation diff --git a/voting/src/main/java/com/exam_microservices/voting/voting/VotingInternalController.java b/voting/src/main/java/com/exam_microservices/voting/voting/VotingInternalController.java index b798f0d8..f84454ad 100644 --- a/voting/src/main/java/com/exam_microservices/voting/voting/VotingInternalController.java +++ b/voting/src/main/java/com/exam_microservices/voting/voting/VotingInternalController.java @@ -2,9 +2,9 @@ import com.exam_microservices.voting.voting.DTO.*; import com.exam_microservices.voting.voting.DTO.GetVotingDTO; -import com.exam_microservices.voting.voting.DTO.GetVotingStateDTO; import com.exam_microservices.voting.voting.DTO.PostVotingDTO; import com.exam_microservices.voting.voting.DtoInternal.GetVotingReportDTO; +import com.exam_microservices.voting.voting.DtoInternal.PutMeetingStatusDTO; import com.exam_microservices.voting.voting.interfaces.VotingService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -52,5 +52,11 @@ public ResponseEntity> getParticipantInfoVoting( @PathVariable Long participantId) { List getVotingReportDTOs = votingService.createVotingReportDTO(meetingId, participantId); return ResponseEntity.ok(getVotingReportDTOs); - } + } + + @PutMapping("/meetingStatus") + public ResponseEntity putMeetingStatusToParticipantCode(@RequestBody PutMeetingStatusDTO putMeetingStatusDTO){ + votingService.updateMeetingStatusInParticipantCodes(putMeetingStatusDTO.getMeetingId(), putMeetingStatusDTO.getMeetingStatus()); + return ResponseEntity.noContent().build(); + } } From 33c3ca7b41a3fd7d867994a270d7dede23e77140 Mon Sep 17 00:00:00 2001 From: moab01567 Date: Tue, 28 Oct 2025 14:27:14 +0100 Subject: [PATCH 2/5] MeetingExternalService send new meeting state votingClient --- .../meeting/meeting/external/MeetingExternalService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/meeting/src/main/java/com/exam_microservices/meeting/meeting/external/MeetingExternalService.java b/meeting/src/main/java/com/exam_microservices/meeting/meeting/external/MeetingExternalService.java index ebe2f5e9..450f2384 100644 --- a/meeting/src/main/java/com/exam_microservices/meeting/meeting/external/MeetingExternalService.java +++ b/meeting/src/main/java/com/exam_microservices/meeting/meeting/external/MeetingExternalService.java @@ -71,6 +71,7 @@ public Meeting startMeeting(Long housingId, Long meetingId) { meeting.setMeetingStatus(MeetingStatus.ONGOING_WAITING_ON_QUESTIONS); meeting.setStartTime(LocalDateTime.now()); + votingClient.sendMeetingStatusToVoting(meeting.getId(), meeting.getMeetingStatus()); return meetingRepo.save(meeting); } @@ -84,7 +85,7 @@ public Meeting endMeeting(Long housingId,Long meetingId) { participants.forEach(participant -> { meetingPublisher.publishMeetingEnded(meeting, participant); }); - + votingClient.sendMeetingStatusToVoting(meeting.getId(), meeting.getMeetingStatus()); return meetingRepo.save(meeting); } From 7285edea46f2df36318b5c62e589095662762160 Mon Sep 17 00:00:00 2001 From: moab01567 Date: Tue, 28 Oct 2025 14:27:36 +0100 Subject: [PATCH 3/5] created new files :) --- .../client/voting/PutMeetingStatusDTO.java | 11 +++++++++++ .../voting/ParticipantCode/MeetingStatus.java | 18 ++++++++++++++++++ .../DtoInternal/PutMeetingStatusDTO.java | 11 +++++++++++ 3 files changed, 40 insertions(+) create mode 100644 meeting/src/main/java/com/exam_microservices/meeting/client/voting/PutMeetingStatusDTO.java create mode 100644 voting/src/main/java/com/exam_microservices/voting/ParticipantCode/MeetingStatus.java create mode 100644 voting/src/main/java/com/exam_microservices/voting/voting/DtoInternal/PutMeetingStatusDTO.java diff --git a/meeting/src/main/java/com/exam_microservices/meeting/client/voting/PutMeetingStatusDTO.java b/meeting/src/main/java/com/exam_microservices/meeting/client/voting/PutMeetingStatusDTO.java new file mode 100644 index 00000000..8ab05581 --- /dev/null +++ b/meeting/src/main/java/com/exam_microservices/meeting/client/voting/PutMeetingStatusDTO.java @@ -0,0 +1,11 @@ +package com.exam_microservices.meeting.client.voting; + +import com.exam_microservices.meeting.meeting.MeetingStatus; +import lombok.Builder; +import lombok.Data; + +@Builder @Data +public class PutMeetingStatusDTO { + private Long meetingId; + private MeetingStatus meetingStatus; +} diff --git a/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/MeetingStatus.java b/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/MeetingStatus.java new file mode 100644 index 00000000..9eb2f2ed --- /dev/null +++ b/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/MeetingStatus.java @@ -0,0 +1,18 @@ +package com.exam_microservices.voting.ParticipantCode; + +public enum MeetingStatus { + NOT_STARTED, + ONGOING_WAITING_ON_QUESTIONS, + ONGOING_VOTING, + DONE; + public String getPublicMeetingStatusMessage(){ + return switch (this){ + case ONGOING_VOTING, ONGOING_WAITING_ON_QUESTIONS -> + "This meeting is already ongoing"; + case DONE -> + "This meeting is done"; + case NOT_STARTED -> + "This meeting has not stated yet"; + }; + } +} diff --git a/voting/src/main/java/com/exam_microservices/voting/voting/DtoInternal/PutMeetingStatusDTO.java b/voting/src/main/java/com/exam_microservices/voting/voting/DtoInternal/PutMeetingStatusDTO.java new file mode 100644 index 00000000..8c1860a5 --- /dev/null +++ b/voting/src/main/java/com/exam_microservices/voting/voting/DtoInternal/PutMeetingStatusDTO.java @@ -0,0 +1,11 @@ +package com.exam_microservices.voting.voting.DtoInternal; + +import com.exam_microservices.voting.ParticipantCode.MeetingStatus; +import lombok.Builder; +import lombok.Data; + +@Builder @Data +public class PutMeetingStatusDTO { + private Long meetingId; + private MeetingStatus meetingStatus; +} From 5950289c3b23433ff5aae0c5367b0d5b832b31db Mon Sep 17 00:00:00 2001 From: moab01567 Date: Tue, 28 Oct 2025 14:28:07 +0100 Subject: [PATCH 4/5] added new colum in participant code --- .../voting/ParticipantCode/ParticipantsCode.java | 13 +++++++++---- .../ParticipantCode/ParticipantsCodeRepo.java | 6 ++++-- .../ParticipantCode/ParticipantsCodeService.java | 8 ++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCode.java b/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCode.java index abd6d8d5..811408c4 100644 --- a/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCode.java +++ b/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCode.java @@ -1,9 +1,6 @@ package com.exam_microservices.voting.ParticipantCode; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -20,4 +17,12 @@ public class ParticipantsCode { private Long meetingId; private Long residentId; private Long participantId; + private MeetingStatus meetingStatus; + + @PrePersist + void prePersist() { + if (meetingStatus == null) { + meetingStatus = MeetingStatus.NOT_STARTED; + } + } } diff --git a/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCodeRepo.java b/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCodeRepo.java index 4d8c7764..75e21933 100644 --- a/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCodeRepo.java +++ b/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCodeRepo.java @@ -1,8 +1,10 @@ package com.exam_microservices.voting.ParticipantCode; import org.springframework.data.jpa.repository.JpaRepository; -import java.util.Optional; + +import java.util.List; public interface ParticipantsCodeRepo extends JpaRepository { - Optional findByCodeAndMeetingIdAndResidentId(String code, Long meetingId, Long residentId); + + List findAllByMeetingId(Long meetingId); } \ No newline at end of file diff --git a/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCodeService.java b/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCodeService.java index 9f88b7fb..ec1a8a3b 100644 --- a/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCodeService.java +++ b/voting/src/main/java/com/exam_microservices/voting/ParticipantCode/ParticipantsCodeService.java @@ -10,6 +10,7 @@ import java.time.ZoneId; import java.time.ZonedDateTime; +import java.util.List; import java.util.UUID; @Service @@ -34,6 +35,7 @@ private ParticipantsCode generateCode(Long meetingId, Long residentId, Long part .meetingId(meetingId) .residentId(residentId) .participantId(participantId) + .meetingStatus(MeetingStatus.NOT_STARTED) .build(); return repository.save(participantCode); @@ -57,4 +59,10 @@ public void addParticipantToVoting(Long meetingId, Long residentId, Long partici public ParticipantsCode getParticipantCode(String code) { return repository.findById(code).orElseThrow(()-> new ParticipantCodeNotFound("participant not found, code="+code)); } + + public void changeParticipantCodeMeetingStatus(Long meetingId, MeetingStatus meetingStatus){ + List participantsCodes = repository.findAllByMeetingId(meetingId); + participantsCodes.forEach(participantsCode -> participantsCode.setMeetingStatus(meetingStatus)); + repository.saveAll(participantsCodes); + } } From e24dca509112c540a8e76ba5ef1c490df77bf7f4 Mon Sep 17 00:00:00 2001 From: moab01567 Date: Tue, 28 Oct 2025 14:28:36 +0100 Subject: [PATCH 5/5] change VotingState --- .../voting/voting/VotingServiceImpl.java | 11 +++++++++-- .../exam_microservices/voting/voting/VotingState.java | 4 +++- .../voting/voting/interfaces/VotingService.java | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/voting/src/main/java/com/exam_microservices/voting/voting/VotingServiceImpl.java b/voting/src/main/java/com/exam_microservices/voting/voting/VotingServiceImpl.java index d035e767..349bbcbc 100644 --- a/voting/src/main/java/com/exam_microservices/voting/voting/VotingServiceImpl.java +++ b/voting/src/main/java/com/exam_microservices/voting/voting/VotingServiceImpl.java @@ -1,5 +1,6 @@ package com.exam_microservices.voting.voting; +import com.exam_microservices.voting.ParticipantCode.MeetingStatus; import com.exam_microservices.voting.ParticipantCode.ParticipantsCode; import com.exam_microservices.voting.ParticipantCode.ParticipantsCodeService; import com.exam_microservices.voting.exception.*; @@ -148,8 +149,9 @@ public Voting getVotingQuestion(String code) { @Override public VotingState getVotingStateForParticipant(String code) { ParticipantsCode participantsCode = participantsCodeService.getParticipantCode(code); - //not sure yet maybe add a check that meeting ended here. - // but then will need to fix som endpoints between meeting and voting + if (participantsCode.getMeetingStatus() == MeetingStatus.NOT_STARTED) return VotingState.MEETING_NOT_STARTED; + if (participantsCode.getMeetingStatus() == MeetingStatus.DONE) return VotingState.MEETING_ENDED; + Optional votingOptional = votingRepo.findFirstByMeetingIdAndVotingStatus(participantsCode.getMeetingId(), VotingStatus.OPEN); if (votingOptional.isEmpty()) return VotingState.NOT_ONGOING; @@ -188,4 +190,9 @@ public List createVotingReportDTO(Long meetingId, Long parti .build();}) .toList(); } + + @Override + public void updateMeetingStatusInParticipantCodes(Long meetingId, MeetingStatus meetingStatus) { + participantsCodeService.changeParticipantCodeMeetingStatus(meetingId, meetingStatus); + } } diff --git a/voting/src/main/java/com/exam_microservices/voting/voting/VotingState.java b/voting/src/main/java/com/exam_microservices/voting/voting/VotingState.java index 53f8a96c..a25ed163 100644 --- a/voting/src/main/java/com/exam_microservices/voting/voting/VotingState.java +++ b/voting/src/main/java/com/exam_microservices/voting/voting/VotingState.java @@ -5,7 +5,9 @@ * Used to indicate whether the voting is ongoing, finished, or if the participant has voted. */ public enum VotingState { + MEETING_NOT_STARTED, NOT_ONGOING, ONGOING_BUT_YOU_HAVE_VOTED, - ONGOING_BUT_YOU_HAVE_NOT_VOTED + ONGOING_BUT_YOU_HAVE_NOT_VOTED, + MEETING_ENDED } diff --git a/voting/src/main/java/com/exam_microservices/voting/voting/interfaces/VotingService.java b/voting/src/main/java/com/exam_microservices/voting/voting/interfaces/VotingService.java index c6428fb2..c6d17f70 100644 --- a/voting/src/main/java/com/exam_microservices/voting/voting/interfaces/VotingService.java +++ b/voting/src/main/java/com/exam_microservices/voting/voting/interfaces/VotingService.java @@ -1,5 +1,6 @@ package com.exam_microservices.voting.voting.interfaces; +import com.exam_microservices.voting.ParticipantCode.MeetingStatus; import com.exam_microservices.voting.voting.DTO.*; import com.exam_microservices.voting.voting.DtoInternal.GetVotingReportDTO; import com.exam_microservices.voting.voting.Voting; @@ -14,4 +15,5 @@ public interface VotingService { Voting getVotingQuestion(String Code); VotingState getVotingStateForParticipant(String code); List createVotingReportDTO(Long meetingId, Long participantId); + void updateMeetingStatusInParticipantCodes(Long meetingId, MeetingStatus meetingStatus); }