Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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<Long> participantsIds){
Expand All @@ -41,4 +42,22 @@ public void startVotingRound(Long meetingId, String question, List<Long> 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<PutMeetingStatusDTO> request = new HttpEntity<>(putMeetingStatus,headers);
ResponseEntity<String> response = restTemplate.exchange(
putMeetingStatusInVotingUrl, // the url can be found in application-prod.yml file
HttpMethod.PUT,
request,
String.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions meeting/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions meeting/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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";
};
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ParticipantsCode, String> {
Optional<ParticipantsCode> findByCodeAndMeetingIdAndResidentId(String code, Long meetingId, Long residentId);

List<ParticipantsCode> findAllByMeetingId(Long meetingId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;

@Service
Expand All @@ -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);
Expand All @@ -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<ParticipantsCode> participantsCodes = repository.findAllByMeetingId(meetingId);
participantsCodes.forEach(participantsCode -> participantsCode.setMeetingStatus(meetingStatus));
repository.saveAll(participantsCodes);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,5 +52,11 @@ public ResponseEntity<List<GetVotingReportDTO>> getParticipantInfoVoting(
@PathVariable Long participantId) {
List<GetVotingReportDTO> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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<Voting> votingOptional = votingRepo.findFirstByMeetingIdAndVotingStatus(participantsCode.getMeetingId(), VotingStatus.OPEN);
if (votingOptional.isEmpty())
return VotingState.NOT_ONGOING;
Expand Down Expand Up @@ -188,4 +190,9 @@ public List<GetVotingReportDTO> createVotingReportDTO(Long meetingId, Long parti
.build();})
.toList();
}

@Override
public void updateMeetingStatusInParticipantCodes(Long meetingId, MeetingStatus meetingStatus) {
participantsCodeService.changeParticipantCodeMeetingStatus(meetingId, meetingStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,4 +15,5 @@ public interface VotingService {
Voting getVotingQuestion(String Code);
VotingState getVotingStateForParticipant(String code);
List<GetVotingReportDTO> createVotingReportDTO(Long meetingId, Long participantId);
void updateMeetingStatusInParticipantCodes(Long meetingId, MeetingStatus meetingStatus);
}