diff --git a/src/main/java/com/example/swiftgathering_server/controller/GatheringSessionController.java b/src/main/java/com/example/swiftgathering_server/controller/GatheringSessionController.java index be54afd..1f335de 100644 --- a/src/main/java/com/example/swiftgathering_server/controller/GatheringSessionController.java +++ b/src/main/java/com/example/swiftgathering_server/controller/GatheringSessionController.java @@ -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; @@ -20,9 +20,9 @@ public ResponseEntity createSession(@RequestBody CreateSessionRequestDto r return ResponseEntity.ok().build(); } - @PatchMapping("/{id}") - public ResponseEntity endSession(@PathVariable Long id, @RequestBody EndSessionRequestDto requestDto) { - gatheringSessionService.endSession(id, requestDto); + @PatchMapping + public ResponseEntity participateSession(@RequestBody ParticipateSessionRequestDto participateSessionRequestDto) { + gatheringSessionService.participateSession(participateSessionRequestDto); return ResponseEntity.ok().build(); } } \ No newline at end of file diff --git a/src/main/java/com/example/swiftgathering_server/domain/GatheringSession.java b/src/main/java/com/example/swiftgathering_server/domain/GatheringSession.java index 4ca8276..2b34880 100644 --- a/src/main/java/com/example/swiftgathering_server/domain/GatheringSession.java +++ b/src/main/java/com/example/swiftgathering_server/domain/GatheringSession.java @@ -7,6 +7,7 @@ import lombok.NoArgsConstructor; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -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 gatheringSessionMembers; + @ElementCollection + private List gatheringSessionMembers = new ArrayList<>(); @OneToMany(mappedBy = "gatheringSession", cascade = CascadeType.ALL) private List locations; @@ -34,9 +34,4 @@ public GatheringSession(List gatheringSessionMembers, Lo this.gatheringSessionMembers = gatheringSessionMembers; this.createdTime = createdTime; } - - public void endSession(LocalDateTime endedTime, List locations) { - this.endedTime = endedTime; - this.locations = locations; - } } diff --git a/src/main/java/com/example/swiftgathering_server/domain/GatheringSessionMember.java b/src/main/java/com/example/swiftgathering_server/domain/GatheringSessionMember.java index 9ff7804..ab97636 100644 --- a/src/main/java/com/example/swiftgathering_server/domain/GatheringSessionMember.java +++ b/src/main/java/com/example/swiftgathering_server/domain/GatheringSessionMember.java @@ -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; } } diff --git a/src/main/java/com/example/swiftgathering_server/domain/GatheringSessionMemberStatus.java b/src/main/java/com/example/swiftgathering_server/domain/GatheringSessionMemberStatus.java new file mode 100644 index 0000000..214f9d2 --- /dev/null +++ b/src/main/java/com/example/swiftgathering_server/domain/GatheringSessionMemberStatus.java @@ -0,0 +1,5 @@ +package com.example.swiftgathering_server.domain; + +public enum GatheringSessionMemberStatus { + INVITED, PARTICIPATED +} diff --git a/src/main/java/com/example/swiftgathering_server/dto/CreateSessionRequestDto.java b/src/main/java/com/example/swiftgathering_server/dto/CreateSessionRequestDto.java index c38a8be..dfbeeaf 100644 --- a/src/main/java/com/example/swiftgathering_server/dto/CreateSessionRequestDto.java +++ b/src/main/java/com/example/swiftgathering_server/dto/CreateSessionRequestDto.java @@ -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 members; + private Long senderId; + private List guestIds; } \ No newline at end of file diff --git a/src/main/java/com/example/swiftgathering_server/dto/EndSessionRequestDto.java b/src/main/java/com/example/swiftgathering_server/dto/EndSessionRequestDto.java deleted file mode 100644 index c94aa09..0000000 --- a/src/main/java/com/example/swiftgathering_server/dto/EndSessionRequestDto.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.example.swiftgathering_server.dto; - - -import com.example.swiftgathering_server.domain.FlagLocation; -import lombok.Getter; - -import java.util.List; - -@Getter -public class EndSessionRequestDto { - private List flagLocations; -} diff --git a/src/main/java/com/example/swiftgathering_server/dto/GatheringSessionNotificationDto.java b/src/main/java/com/example/swiftgathering_server/dto/GatheringSessionNotificationDto.java index aadbb91..fd749b2 100644 --- a/src/main/java/com/example/swiftgathering_server/dto/GatheringSessionNotificationDto.java +++ b/src/main/java/com/example/swiftgathering_server/dto/GatheringSessionNotificationDto.java @@ -6,11 +6,11 @@ @Getter public class GatheringSessionNotificationDto { - private Long sessionId; - private List memberIds; + final private Long sessionId; + final private List participantIds; - public GatheringSessionNotificationDto(Long sessionId, List memberIds) { + public GatheringSessionNotificationDto(Long sessionId, List participantIds) { this.sessionId = sessionId; - this.memberIds = memberIds; + this.participantIds = participantIds; } } \ No newline at end of file diff --git a/src/main/java/com/example/swiftgathering_server/dto/ParticipateSessionRequestDto.java b/src/main/java/com/example/swiftgathering_server/dto/ParticipateSessionRequestDto.java new file mode 100644 index 0000000..ac215b5 --- /dev/null +++ b/src/main/java/com/example/swiftgathering_server/dto/ParticipateSessionRequestDto.java @@ -0,0 +1,9 @@ +package com.example.swiftgathering_server.dto; + +import lombok.Getter; + +@Getter +public class ParticipateSessionRequestDto { + private Long sessionId; + private Long memberId; +} diff --git a/src/main/java/com/example/swiftgathering_server/repository/FriendRequestRepository.java b/src/main/java/com/example/swiftgathering_server/repository/FriendRequestRepository.java index 4e9fbcf..231ed17 100644 --- a/src/main/java/com/example/swiftgathering_server/repository/FriendRequestRepository.java +++ b/src/main/java/com/example/swiftgathering_server/repository/FriendRequestRepository.java @@ -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; @@ -20,6 +21,10 @@ public Long save(FriendRequest friendRequest) { return friendRequest.getId(); } + public void update(FriendRequest friendRequest) { + em.merge(friendRequest); + } + public Optional findById(Long requestId) { FriendRequest friendRequest = em.find(FriendRequest.class, requestId); return Optional.of(friendRequest); diff --git a/src/main/java/com/example/swiftgathering_server/repository/GatheringSessionRepository.java b/src/main/java/com/example/swiftgathering_server/repository/GatheringSessionRepository.java index 3b60e91..a4b5716 100644 --- a/src/main/java/com/example/swiftgathering_server/repository/GatheringSessionRepository.java +++ b/src/main/java/com/example/swiftgathering_server/repository/GatheringSessionRepository.java @@ -18,6 +18,10 @@ public GatheringSession save(GatheringSession gatheringSession) { return gatheringSession; } + public GatheringSession update(GatheringSession gatheringSession) { + return em.merge(gatheringSession); + } + public Optional findById(Long id) { GatheringSession gatheringSession = em.find(GatheringSession.class, id); return Optional.of(gatheringSession); diff --git a/src/main/java/com/example/swiftgathering_server/repository/MemberRepository.java b/src/main/java/com/example/swiftgathering_server/repository/MemberRepository.java index 82c2f02..4c35d15 100644 --- a/src/main/java/com/example/swiftgathering_server/repository/MemberRepository.java +++ b/src/main/java/com/example/swiftgathering_server/repository/MemberRepository.java @@ -5,6 +5,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @Repository @@ -31,6 +32,12 @@ public Optional findByLoginUsername(String loginUsername) { .findAny(); } + public List findAllByIds(List 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); } diff --git a/src/main/java/com/example/swiftgathering_server/service/FriendRequestService.java b/src/main/java/com/example/swiftgathering_server/service/FriendRequestService.java index 9b538d9..92d82f4 100644 --- a/src/main/java/com/example/swiftgathering_server/service/FriendRequestService.java +++ b/src/main/java/com/example/swiftgathering_server/service/FriendRequestService.java @@ -64,6 +64,6 @@ public void updateFriendRequestStatus(Long memberId, FriendRequestUpdateDto frie } else { friendRequest.setRequestStatus(false); } - friendRequestRepository.save(friendRequest); + friendRequestRepository.update(friendRequest); } } diff --git a/src/main/java/com/example/swiftgathering_server/service/GatheringSessionService.java b/src/main/java/com/example/swiftgathering_server/service/GatheringSessionService.java index a7400ba..5d26c31 100644 --- a/src/main/java/com/example/swiftgathering_server/service/GatheringSessionService.java +++ b/src/main/java/com/example/swiftgathering_server/service/GatheringSessionService.java @@ -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; @@ -20,7 +18,6 @@ import java.time.LocalDateTime; import java.util.List; -import java.util.NoSuchElementException; import java.util.stream.Collectors; @Service @@ -28,63 +25,56 @@ @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 members = requestDto.getMembers(); + List 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 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 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); } }