Skip to content

Commit

Permalink
Session notified to clients with session (#39)
Browse files Browse the repository at this point in the history
* feature: GatheringSessionMember as Embeddable object, participateSession endpoint

* refactor: rename GatheringSessionNotificationDto participant ids
  • Loading branch information
bviz6542 authored Jul 9, 2024
1 parent 5254f08 commit 04d444b
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.swiftgathering_server.controller;

import com.example.swiftgathering_server.dto.CreateSessionRequestDto;
import com.example.swiftgathering_server.dto.EndSessionRequestDto;
import com.example.swiftgathering_server.dto.ParticipateSessionRequestDto;
import com.example.swiftgathering_server.service.GatheringSessionService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,9 +20,9 @@ public ResponseEntity<Void> createSession(@RequestBody CreateSessionRequestDto r
return ResponseEntity.ok().build();
}

@PatchMapping("/{id}")
public ResponseEntity<Void> endSession(@PathVariable Long id, @RequestBody EndSessionRequestDto requestDto) {
gatheringSessionService.endSession(id, requestDto);
@PatchMapping
public ResponseEntity<Void> participateSession(@RequestBody ParticipateSessionRequestDto participateSessionRequestDto) {
gatheringSessionService.participateSession(participateSessionRequestDto);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

Expand All @@ -21,10 +22,9 @@ public class GatheringSession {

private UUID sessionId = UUID.randomUUID();
private LocalDateTime createdTime;
private LocalDateTime endedTime;

@OneToMany(mappedBy = "gatheringSession", cascade = CascadeType.ALL)
private List<GatheringSessionMember> gatheringSessionMembers;
@ElementCollection
private List<GatheringSessionMember> gatheringSessionMembers = new ArrayList<>();

@OneToMany(mappedBy = "gatheringSession", cascade = CascadeType.ALL)
private List<FlagLocation> locations;
Expand All @@ -34,9 +34,4 @@ public GatheringSession(List<GatheringSessionMember> gatheringSessionMembers, Lo
this.gatheringSessionMembers = gatheringSessionMembers;
this.createdTime = createdTime;
}

public void endSession(LocalDateTime endedTime, List<FlagLocation> locations) {
this.endedTime = endedTime;
this.locations = locations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Embeddable
public class GatheringSessionMember {
private Long memberId;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "gathering_session_id")
private GatheringSession gatheringSession;

@ManyToOne
@JoinColumn(name = "member_id")
private Member member;
@Enumerated(EnumType.STRING)
private GatheringSessionMemberStatus status;

@Builder
public GatheringSessionMember(GatheringSession gatheringSession, Member member) {
this.gatheringSession = gatheringSession;
this.member = member;
public GatheringSessionMember(Long memberId, GatheringSessionMemberStatus status) {
this.memberId = memberId;
this.status = status;
}

public void setStatus(GatheringSessionMemberStatus status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.swiftgathering_server.domain;

public enum GatheringSessionMemberStatus {
INVITED, PARTICIPATED
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.example.swiftgathering_server.dto;

import com.example.swiftgathering_server.domain.GatheringSessionMember;
import lombok.Getter;

import java.util.List;

@Getter
public class CreateSessionRequestDto {
private List<GatheringSessionMember> members;
private Long senderId;
private List<Long> guestIds;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

@Getter
public class GatheringSessionNotificationDto {
private Long sessionId;
private List<Long> memberIds;
final private Long sessionId;
final private List<Long> participantIds;

public GatheringSessionNotificationDto(Long sessionId, List<Long> memberIds) {
public GatheringSessionNotificationDto(Long sessionId, List<Long> participantIds) {
this.sessionId = sessionId;
this.memberIds = memberIds;
this.participantIds = participantIds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.swiftgathering_server.dto;

import lombok.Getter;

@Getter
public class ParticipateSessionRequestDto {
private Long sessionId;
private Long memberId;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.swiftgathering_server.repository;

import com.example.swiftgathering_server.domain.FriendRequest;
import com.example.swiftgathering_server.domain.Friendship;
import com.example.swiftgathering_server.domain.Member;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,6 +21,10 @@ public Long save(FriendRequest friendRequest) {
return friendRequest.getId();
}

public void update(FriendRequest friendRequest) {
em.merge(friendRequest);
}

public Optional<FriendRequest> findById(Long requestId) {
FriendRequest friendRequest = em.find(FriendRequest.class, requestId);
return Optional.of(friendRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public GatheringSession save(GatheringSession gatheringSession) {
return gatheringSession;
}

public GatheringSession update(GatheringSession gatheringSession) {
return em.merge(gatheringSession);
}

public Optional<GatheringSession> findById(Long id) {
GatheringSession gatheringSession = em.find(GatheringSession.class, id);
return Optional.of(gatheringSession);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
Expand All @@ -31,6 +32,12 @@ public Optional<Member> findByLoginUsername(String loginUsername) {
.findAny();
}

public List<Member> findAllByIds(List<Long> ids) {
return em.createQuery("select m from Member m where m.id in :ids", Member.class)
.setParameter("ids", ids)
.getResultList();
}

public void remove(Member member) {
em.remove(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public void updateFriendRequestStatus(Long memberId, FriendRequestUpdateDto frie
} else {
friendRequest.setRequestStatus(false);
}
friendRequestRepository.save(friendRequest);
friendRequestRepository.update(friendRequest);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.example.swiftgathering_server.service;

import com.example.swiftgathering_server.domain.FlagLocation;
import com.example.swiftgathering_server.domain.GatheringSession;
import com.example.swiftgathering_server.domain.GatheringSessionMember;
import com.example.swiftgathering_server.domain.Member;
import com.example.swiftgathering_server.domain.*;
import com.example.swiftgathering_server.dto.CreateSessionRequestDto;
import com.example.swiftgathering_server.dto.EndSessionRequestDto;
import com.example.swiftgathering_server.dto.GatheringSessionNotificationDto;
import com.example.swiftgathering_server.repository.FlagLocationRepository;
import com.example.swiftgathering_server.dto.ParticipateSessionRequestDto;
import com.example.swiftgathering_server.exception.ResourceNotFoundException;
import com.example.swiftgathering_server.repository.GatheringSessionRepository;
import com.example.swiftgathering_server.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
Expand All @@ -20,71 +18,63 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
public class GatheringSessionService {

private final MemberRepository memberRepository;
private final GatheringSessionRepository gatheringSessionRepository;
private final FlagLocationRepository flagLocationRepository;
// private final FlagLocationRepository flagLocationRepository;
private final AmqpTemplate amqpTemplate;

public void createSession(CreateSessionRequestDto requestDto) {
List<GatheringSessionMember> members = requestDto.getMembers();
List<GatheringSessionMember> sessionMembers = memberRepository
.findAllByIds(requestDto.getGuestIds()).stream()
.map(member -> GatheringSessionMember.builder()
.memberId(member.getId())
.status(GatheringSessionMemberStatus.INVITED)
.build())
.collect(Collectors.toList());

LocalDateTime createdTime = LocalDateTime.now();
GatheringSession session = GatheringSession.builder()
.gatheringSessionMembers(members)
.gatheringSessionMembers(sessionMembers)
.createdTime(createdTime)
.build();
GatheringSession sessionId = gatheringSessionRepository.save(session);
notifyClients(sessionId);
return;
}

public void endSession(Long sessionId, EndSessionRequestDto requestDto) {
GatheringSession session = gatheringSessionRepository.findById(sessionId)
.orElseThrow(() -> new NoSuchElementException("Session not found"));

LocalDateTime endedTime = LocalDateTime.now();

List<FlagLocation> flagLocations = requestDto.getFlagLocations().stream()
.map(dto -> FlagLocation.builder()
.latitude(dto.getLatitude())
.longitude(dto.getLongitude())
.name(dto.getName())
.createdTime(dto.getCreatedTime() != null ? dto.getCreatedTime() : LocalDateTime.now())
.gatheringSession(session)
.build())
.collect(Collectors.toList());
GatheringSession savedSession = gatheringSessionRepository.save(session);
notifyClients(savedSession);
}

session.endSession(endedTime, flagLocations);
public void participateSession(ParticipateSessionRequestDto requestDto) {
GatheringSession session = gatheringSessionRepository.findById(requestDto.getSessionId())
.orElseThrow(() -> new ResourceNotFoundException("Session not found"));

flagLocationRepository.saveAll(flagLocations);
session.getGatheringSessionMembers().stream()
.filter(member -> member.getMemberId().equals(requestDto.getMemberId()))
.findFirst()
.ifPresentOrElse(
member -> member.setStatus(GatheringSessionMemberStatus.PARTICIPATED),
() -> {
throw new IllegalStateException("Member not invited to the session");
});

gatheringSessionRepository.save(session);
return;
gatheringSessionRepository.update(session);
}

private void notifyClients(GatheringSession session) {
List<Long> memberIds = session.getGatheringSessionMembers().stream()
.map(GatheringSessionMember::getMember)
.map(Member::getId)
.map(GatheringSessionMember::getMemberId)
.collect(Collectors.toList());

for (Long memberId : memberIds) {
Queue queue = new Queue("swift-gathering.queue." + memberId, false);
DirectExchange exchange = new DirectExchange("swift-gathering.exchange." + memberId);
Binding binding = BindingBuilder.bind(queue).to(exchange).with("swift-gathering.routing." + memberId);

// rabbitAdmin.declareQueue(queue);
// rabbitAdmin.declareExchange(exchange);
// rabbitAdmin.declareBinding(binding);

GatheringSessionNotificationDto notification = new GatheringSessionNotificationDto(session.getId(), memberIds);

amqpTemplate.convertAndSend(exchange.getName(), binding.getRoutingKey(), notification);
}
}
Expand Down

0 comments on commit 04d444b

Please sign in to comment.