Skip to content

Commit

Permalink
Merge branch 'develop' into feat/#66/exception-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
drbug2000 authored Aug 11, 2024
2 parents a893d61 + e204417 commit 987fb0f
Show file tree
Hide file tree
Showing 38 changed files with 848 additions and 116 deletions.
14 changes: 13 additions & 1 deletion src/main/java/space/space_spring/config/WebSocketConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package space.space_spring.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import space.space_spring.interceptor.jwtSocket.JwtChannelInterceptor;

@Configuration
@EnableWebSocketMessageBroker
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

private final JwtChannelInterceptor jwtChannelInterceptor;

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
// stomp ์ ‘์† ์ฃผ์†Œ url = ws://localhost:8080/ws
Expand All @@ -23,6 +30,11 @@ public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");

// ๋ฉ”์„ธ์ง€ ๋ฐœํ–‰(์†ก์‹ ) ์š”์ฒญ์˜ ์—”๋“œ ํฌ์ธํŠธ
registry.setApplicationDestinationPrefixes("/app");
registry.setApplicationDestinationPrefixes("/app", "/topic");
}

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(jwtChannelInterceptor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package space.space_spring.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.handler.annotation.*;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
import space.space_spring.dto.chat.request.ChatMessageRequest;
import space.space_spring.dto.chat.response.ChatMessageLogResponse;
import space.space_spring.dto.chat.response.ChatMessageResponse;
import space.space_spring.service.ChattingService;
import space.space_spring.service.UserChatRoomService;

import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
public class ChattingController {

private final ChattingService chattingService;

private final UserChatRoomService userChatRoomService;

@MessageMapping("/chat/{chatRoomId}") // {chatRoomId} ์ฑ„ํŒ…๋ฐฉ์œผ๋กœ ๋ณด๋‚ธ ๋ฉ”์„ธ์ง€ ๋งคํ•‘
@SendTo("/topic/chat/{chatRoomId}") // {chatRoomId} ์ฑ„ํŒ…๋ฐฉ์„ ๊ตฌ๋…ํ•œ ๊ณณ๋“ค๋กœ ๋ฉ”์„ธ์ง€ ์ „์†ก
public ChatMessageResponse sendChatMessage (@Payload ChatMessageRequest chatMessageRequest, @DestinationVariable Long chatRoomId,
@Header("simpSessionAttributes") Map<String, Object> sessionAttributes) {
Long senderId = (Long) sessionAttributes.get("userId");
// log.info(senderId + " ๋‹˜์ด " + chatRoomId + " ์ฑ„ํŒ…๋ฐฉ์œผ๋กœ " + chatMessageRequest.getContent() + " ์ „์†ก");

return chattingService.sendChatMessage(senderId, chatMessageRequest, chatRoomId);
}

@SubscribeMapping("/chat/{chatRoomId}") // {chatRoomId} ์ฑ„ํŒ…๋ฐฉ์„ ๊ตฌ๋…
public ChatMessageLogResponse subscribeChatRoom (@DestinationVariable Long chatRoomId, @Header("simpSessionAttributes") Map<String, Object> sessionAttributes) {
// log.info(chatRoomId + " ์ฑ„ํŒ…๋ฐฉ ๊ตฌ๋…");
sessionAttributes.put("chatRoomId", chatRoomId);
return chattingService.readChatMessageLog(chatRoomId);
}

// socket disconnect ์‹œ ํ˜ธ์ถœ
@EventListener
public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
Map<String, Object> sessionAttributes = headerAccessor.getSessionAttributes();

Long userId = (Long) sessionAttributes.get("userId");
Long chatRoomId = (Long) sessionAttributes.get("chatRoomId");

userChatRoomService.saveLastReadTime(userId, chatRoomId);
}
}
52 changes: 49 additions & 3 deletions src/main/java/space/space_spring/controller/PayController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.pay.dto.PayReceiveInfoDto;
import space.space_spring.dto.pay.dto.PayRequestInfoDto;
import space.space_spring.dto.pay.dto.PayTargetInfoDto;
import space.space_spring.dto.pay.dto.TotalPayInfoDto;
import space.space_spring.dto.pay.request.PostPayCompleteRequest;
import space.space_spring.dto.pay.request.PostPayCreateRequest;
import space.space_spring.dto.pay.response.GetPayViewResponse;
import space.space_spring.dto.pay.response.GetRecentPayRequestBankInfoResponse;
import space.space_spring.dto.pay.response.GetRequestPayViewResponse;
import space.space_spring.dto.pay.response.*;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.PayService;
import space.space_spring.util.userSpace.UserSpaceUtils;
Expand Down Expand Up @@ -66,6 +67,24 @@ public BaseResponse<GetRequestPayViewResponse> showRequestPayListForUser(@JwtLog
return new BaseResponse<>(new GetRequestPayViewResponse(payRequestInfoDtoListInComplete, payRequestInfoDtoListComplete));
}

/**
* ๋‚ด๊ฐ€ ์š”์ฒญ๋ฐ›์€ ์ •์‚ฐ ์กฐํšŒ
*/
@GetMapping("/space/{spaceId}/pay/receive")
public BaseResponse<GetReceivePayViewResponse> showReceivePayListForUser(@JwtLoginAuth Long userId, @PathVariable Long spaceId) {
// TODO 1. ์œ ์ €๊ฐ€ ์ŠคํŽ˜์ด์Šค์— ์†ํ•˜๋Š” ์ง€ ๊ฒ€์ฆ -> ์ถ”ํ›„์— ์ธํ„ฐ์…‰ํ„ฐ์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ๋” ๋ฆฌํŽ™ํ† ๋ง ํ•„์š”
validateIsUserInSpace(userId, spaceId);

// TODO 2. ์œ ์ €๊ฐ€ ์š”์ฒญ๋ฐ›์€ ์ •์‚ฐ ์ค‘ ํ˜„์žฌ ์ง„ํ–‰์ค‘์ธ ์ •์‚ฐ ๋ฆฌ์ŠคํŠธ get -> ์ •์‚ฐ ํƒ€๊ฒŸ ์œ ์ €๊ฐ€ ์ •์‚ฐ ์•ˆํ–ˆ์„ ๊ฒฝ์šฐ : isComplete = false
List<PayReceiveInfoDto> payReceiveInfoDtoListInComplete = payService.getPayReceiveInfoForUser(userId, spaceId, false);

// TODO 3. ์œ ์ €๊ฐ€ ์š”์ฒญ๋ฐ›์€ ์ •์‚ฐ ์ค‘ ์™„๋ฃŒํ•œ ์ •์‚ฐ ๋ฆฌ์ŠคํŠธ get -> ์ •์‚ฐ ํƒ€๊ฒŸ ์œ ์ €๊ฐ€ ์ •์‚ฐ ํ–ˆ์„ ๊ฒฝ์šฐ : isComplete = true
List<PayReceiveInfoDto> payReceiveInfoDtoListComplete = payService.getPayReceiveInfoForUser(userId, spaceId, true);

return new BaseResponse<>(new GetReceivePayViewResponse(payReceiveInfoDtoListInComplete, payReceiveInfoDtoListComplete));
}


/**
* ์œ ์ €๊ฐ€ ์ตœ๊ทผ ์ •์‚ฐ๋ฐ›์€ ์€ํ–‰ ๊ณ„์ขŒ ์ •๋ณด ์กฐํšŒ
* ํ•ด๋‹น api๋Š” ์œ ์ €๊ฐ€ ์†ํ•œ ์ŠคํŽ˜์ด์Šค์˜ ์ •๋ณด๊ฐ€ ํ•„์š”์—†๋‹ค๊ณ  ํŒ๋‹จํ•ด์„œ spaceId ๋ฅผ request๋กœ ๋ฐ›์ง€ ์•Š์Œ
Expand Down Expand Up @@ -102,4 +121,31 @@ public BaseResponse<String> createPay(@JwtLoginAuth Long userId, @PathVariable L

return new BaseResponse<>("์ •์‚ฐ ์ƒ์„ฑ ์„ฑ๊ณต");
}

/**
* ํ•˜๋‚˜์˜ ์ •์‚ฐ์— ๋Œ€ํ•œ ์ƒ์„ธ์ •๋ณด ์กฐํšŒ
*/
@GetMapping("/space/{spaceId}/pay/{payRequestId}")
public BaseResponse<TotalPayInfoDto> showTotalPayInfo(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @PathVariable Long payRequestId) {

// TODO 1. ์œ ์ €๊ฐ€ ์ŠคํŽ˜์ด์Šค์— ์†ํ•˜๋Š” ์ง€ ๊ฒ€์ฆ
validateIsUserInSpace(userId, spaceId);

// TODO 2. ์ •์‚ฐ ์ƒ์„ธ ์ •๋ณด ์กฐํšŒ
return new BaseResponse<>(payService.getTotalPayInfo(spaceId, payRequestId));
}

/**
* ์ •์‚ฐ ํƒ€๊ฒŸ ์œ ์ €์˜ ์ •์‚ฐ ์™„๋ฃŒ ์ฒ˜๋ฆฌ
*/
@PostMapping("/space/{spaceId}/pay/complete")
public BaseResponse<PostPayCompleteResponse> setPayComplete(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @RequestBody PostPayCompleteRequest postPayCompleteRequest) {

// TODO 1. ์œ ์ €๊ฐ€ ์ŠคํŽ˜์ด์Šค์— ์†ํ•˜๋Š” ์ง€ ๊ฒ€์ฆ
validateIsUserInSpace(userId, spaceId);

// TODO 2. ์ •์‚ฐ ํƒ€๊ฒŸ ์œ ์ €์˜ ์ •์‚ฐ ์™„๋ฃŒ ์ฒ˜๋ฆฌ
return new BaseResponse<>(payService.setPayRequestTargetToComplete(postPayCompleteRequest.getPayRequestTargetId()));
}

}
22 changes: 22 additions & 0 deletions src/main/java/space/space_spring/controller/SpaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.space.GetSpaceJoinDto;
import space.space_spring.dto.space.response.GetUserInfoBySpaceResponse;
import space.space_spring.dto.space.request.PostSpaceCreateRequest;

Expand All @@ -15,6 +16,7 @@
import space.space_spring.response.BaseResponse;
import space.space_spring.service.S3Uploader;
import space.space_spring.service.SpaceService;
import space.space_spring.util.userSpace.UserSpaceUtils;

import java.io.IOException;

Expand All @@ -31,6 +33,7 @@ public class SpaceController {
private final SpaceService spaceService;
private final S3Uploader s3Uploader;
private final String spaceImgDirName = "spaceImg";
private final UserSpaceUtils userSpaceUtils;

@PostMapping("")
public BaseResponse<String> createSpace(@JwtLoginAuth Long userId, @Validated @ModelAttribute PostSpaceCreateRequest postSpaceCreateRequest, BindingResult bindingResult) throws IOException {
Expand Down Expand Up @@ -71,4 +74,23 @@ public BaseResponse<GetUserInfoBySpaceResponse> getAllUserInfoBySpace(@PathVaria
return new BaseResponse<>(spaceService.findUserInfoBySpace(spaceId));
}

/**
* ์ŠคํŽ˜์ด์Šค ๊ฐ€์ž… view๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์กฐํšŒ
*/
@GetMapping("/{spaceId}/join")
public BaseResponse<GetSpaceJoinDto.Response> getSpaceJoin(@JwtLoginAuth Long userId, @PathVariable Long spaceId) {

// TODO 1. ์œ ์ €๊ฐ€ ์ด๋ฏธ ์ŠคํŽ˜์ด์Šค์— ๊ฐ€์ž…๋˜์–ด ์žˆ๋Š” ์œ ์ €์ธ์ง€๋ฅผ ๊ฒ€์ฆ
validateIsUserAlreadySpaceMember(userId, spaceId);

// TODO 2. ์ดˆ๋Œ€ํ•  ์ŠคํŽ˜์ด์Šค์˜ ์ •๋ณด๋ฅผ return
return new BaseResponse<>(spaceService.findSpaceJoin(spaceId));
}

private void validateIsUserAlreadySpaceMember(Long userId, Long spaceId) {
// ์œ ์ €๊ฐ€ ์ด๋ฏธ ์ŠคํŽ˜์ด์Šค์˜ ๋ฉค๋ฒ„์ผ ๊ฒฝ์šฐ exception ๋ฐœ์ƒ
// exception ์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ response์— ์œ ์ €๊ฐ€ ์ŠคํŽ˜์ด์Šค์— ๊ฐ€์ž…๋œ ์œ ์ €์ธ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋Š”๊ฒŒ ๋” ์ข‹์„๊นŒ??
userSpaceUtils.isUserAlreadySpaceMember(userId, spaceId);
}

}
20 changes: 0 additions & 20 deletions src/main/java/space/space_spring/controller/TestController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package space.space_spring.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;

import space.space_spring.dao.PayDao;
import space.space_spring.dto.chat.request.ChatTestRequest;
import space.space_spring.dto.chat.response.ChatTestResponse;
import space.space_spring.response.BaseResponse;

@RestController
Expand All @@ -30,16 +22,4 @@ public BaseResponse<String> jwtLoginTest(@JwtLoginAuth Long userId) {
return new BaseResponse<>("jwt login test ์„ฑ๊ณต");
}

@MessageMapping("/chat/{spaceChatId}") // {spaceChatId} ์ฑ„ํŒ…๋ฐฉ์œผ๋กœ ๋ณด๋‚ธ ๋ฉ”์„ธ์ง€ ๋งคํ•‘
@SendTo("/topic/chat/{spaceChatId}") // {spaceChatId} ์ฑ„ํŒ…๋ฐฉ์„ ๊ตฌ๋…ํ•œ ๊ณณ๋“ค๋กœ ๋ฉ”์„ธ์ง€ ์ „์†ก
public ChatTestResponse sendMsgTest(@Payload ChatTestRequest chat, @DestinationVariable String spaceChatId) {
log.info(spaceChatId + " ์ฑ„ํŒ…๋ฐฉ์œผ๋กœ " + chat.getMsg() + " ์ „์†ก");
return ChatTestResponse.of(chat.getMsg());
}

@SubscribeMapping("/topic/chat/{spaceChatId}") // {spaceChatId} ์ฑ„ํŒ…๋ฐฉ์„ ๊ตฌ๋…
public void subscribeTest(@DestinationVariable String spaceChatId) {
log.info(spaceChatId + " ์ฑ„ํŒ…๋ฐฉ ๊ตฌ๋…");
}

}
12 changes: 12 additions & 0 deletions src/main/java/space/space_spring/dao/PayDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import space.space_spring.dto.pay.dto.PayRequestInfoDto;
import space.space_spring.dto.pay.dto.RecentPayRequestBankInfoDto;
import space.space_spring.dto.pay.dto.TotalPayInfoDto;
import space.space_spring.entity.PayRequest;
import space.space_spring.entity.PayRequestTarget;
import space.space_spring.entity.Space;
Expand Down Expand Up @@ -87,4 +89,14 @@ public PayRequestTarget createPayRequestTarget(PayRequest payRequest, Long targe
em.persist(payRequestTarget);
return payRequestTarget;
}

public PayRequest findPayRequestById(Long payRequestId) {

return em.find(PayRequest.class, payRequestId);
}

public PayRequestTarget findPayRequestTargetById(Long payRequestTargetId) {
return em.find(PayRequestTarget.class, payRequestTargetId);
}

}
2 changes: 1 addition & 1 deletion src/main/java/space/space_spring/dao/SpaceDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import space.space_spring.dto.space.GetSpaceJoinDto;
import space.space_spring.entity.Space;

@Repository
Expand All @@ -22,5 +23,4 @@ public Space saveSpace(String spaceName, String spaceImgUrl) {
public Space findSpaceBySpaceId(Long spaceId) {
return em.find(Space.class, spaceId);
}

}
9 changes: 8 additions & 1 deletion src/main/java/space/space_spring/dao/UserSpaceDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public UserSpace createUserSpace(User manager, Space saveSpace) {

public Optional<UserSpace> findUserSpaceByUserAndSpace(User user, Space space) {
TypedQuery<UserSpace> query = em.createQuery(
"SELECT us FROM UserSpace us WHERE us.user = :user AND us.space = :space", UserSpace.class);
"SELECT us FROM UserSpace us WHERE us.user = :user AND us.space = :space AND us.status = 'ACTIVE'", UserSpace.class);
query.setParameter("user", user);
query.setParameter("space", space);

Expand Down Expand Up @@ -108,4 +108,11 @@ private List<UserInfoInSpace> mapToUserInfoInSpace(List<Object[]> results) {
return userInfoInSpaceList;
}

public int calculateSpaceMemberNum(Space space) {
String jpql = "SELECT COUNT(us) FROM UserSpace us WHERE us.space = :space AND us.status = 'ACTIVE'";
TypedQuery<Long> query = em.createQuery(jpql, Long.class);
query.setParameter("space", space);

return query.getSingleResult().intValue();
}
}
17 changes: 17 additions & 0 deletions src/main/java/space/space_spring/dao/chat/ChattingDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package space.space_spring.dao.chat;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import space.space_spring.entity.document.ChatMessage;

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

@Repository
public interface ChattingDao extends MongoRepository<ChatMessage, String> {
List<ChatMessage> findByChatRoomId(Long chatRoomId);

ChatMessage findTopByChatRoomIdOrderByCreatedAtDesc(Long chatRoomId);

int countByChatRoomIdAndCreatedAtBetween(Long chatRoomId, LocalDateTime lastReadTime, LocalDateTime lastUpdateTime);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package space.space_spring.dao.chat;

import org.springframework.data.jpa.repository.JpaRepository;
import space.space_spring.entity.ChatRoom;
import space.space_spring.entity.User;
import space.space_spring.entity.UserChatRoom;


public interface UserChatRoomDao extends JpaRepository<UserChatRoom, Long> {
UserChatRoom findByUserAndChatRoom(User userByUserId, ChatRoom chatRoomByChatRoomId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package space.space_spring.dto.chat.request;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;
import space.space_spring.entity.enumStatus.ChatMessageType;

import java.util.HashMap;

@Getter
@NoArgsConstructor
public class ChatMessageRequest {

@NotBlank(message = "๋ฉ”์‹œ์ง€ ๋‚ด์šฉ์€ ๊ณต๋ฐฑ์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
private HashMap<String, String> content;

@NotBlank(message = "์ŠคํŽ˜์ด์Šค ์•„์ด๋””๋Š” ๊ณต๋ฐฑ์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
private Long spaceId;

@NotBlank(message = "๋ฉ”์‹œ์ง€ ํƒ€์ž…์€ ๊ณต๋ฐฑ์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
private ChatMessageType messageType;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ public class CreateChatRoomRequest {

private MultipartFile img;

// TODO: member ์กฐํšŒ API ๊ฐœ๋ฐœ ์‹œ ์ˆ˜์ • ์˜ˆ์ •
private List<String> memberList;
private List<Long> memberList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package space.space_spring.dto.chat.response;

import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Getter
@Builder
public class ChatMessageLogResponse {
private List<ChatMessageResponse> chatMessageLog;

public static ChatMessageLogResponse of(List<ChatMessageResponse> chatMessageList) {
return ChatMessageLogResponse.builder()
.chatMessageLog(chatMessageList)
.build();
}
}
Loading

0 comments on commit 987fb0f

Please sign in to comment.