From f76b927c731bf97642c6777930be765bfb715098 Mon Sep 17 00:00:00 2001 From: emost22 Date: Thu, 29 Feb 2024 23:04:07 +0900 Subject: [PATCH 1/2] [#139] Feat: add socket users api --- .../user/controller/UserController.java | 1 + .../user/controller/UserSocketController.java | 22 +++++++++++++++++++ .../audy/domain/user/service/UserService.java | 11 ++++++---- .../user/service/UserServiceMapper.java | 4 ---- 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/pcb/audy/domain/user/controller/UserSocketController.java diff --git a/src/main/java/com/pcb/audy/domain/user/controller/UserController.java b/src/main/java/com/pcb/audy/domain/user/controller/UserController.java index ba52d45..4edd6b2 100644 --- a/src/main/java/com/pcb/audy/domain/user/controller/UserController.java +++ b/src/main/java/com/pcb/audy/domain/user/controller/UserController.java @@ -31,6 +31,7 @@ public BasicResponse getUser( @GetMapping("/course") public BasicResponse getCourseUser(@RequestParam Long courseId) { + // TODO delete return BasicResponse.success(userService.getUsers(courseId)); } } diff --git a/src/main/java/com/pcb/audy/domain/user/controller/UserSocketController.java b/src/main/java/com/pcb/audy/domain/user/controller/UserSocketController.java new file mode 100644 index 0000000..9383794 --- /dev/null +++ b/src/main/java/com/pcb/audy/domain/user/controller/UserSocketController.java @@ -0,0 +1,22 @@ +package com.pcb.audy.domain.user.controller; + +import com.pcb.audy.domain.user.dto.response.UserGetResList; +import com.pcb.audy.domain.user.service.UserService; +import com.pcb.audy.global.response.BasicResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.messaging.handler.annotation.DestinationVariable; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class UserSocketController { + private final UserService userService; + + @MessageMapping("/{courseId}/user") + @SendTo("/sub/{courseId}/user") + public BasicResponse getCourseUser(@DestinationVariable Long courseId) { + return BasicResponse.success(userService.getUsers(courseId)); + } +} diff --git a/src/main/java/com/pcb/audy/domain/user/service/UserService.java b/src/main/java/com/pcb/audy/domain/user/service/UserService.java index 327d827..3c4108b 100644 --- a/src/main/java/com/pcb/audy/domain/user/service/UserService.java +++ b/src/main/java/com/pcb/audy/domain/user/service/UserService.java @@ -31,10 +31,13 @@ public UserGetResList getUsers(Long courseId) { String key = COURSE_PREFIX + courseId; List users = - UserServiceMapper.INSTANCE.toUserGetResList( - redisProvider.getValues(key).stream() - .map(user -> objectMapper.convertValue(user, User.class)) - .toList()); + redisProvider.getValues(key).stream() + .map( + user -> + SocketUserGetRes.builder() + .userId(objectMapper.convertValue(user, Long.class)) + .build()) + .toList(); return UserGetResList.builder().users(users).total(users.size()).build(); } diff --git a/src/main/java/com/pcb/audy/domain/user/service/UserServiceMapper.java b/src/main/java/com/pcb/audy/domain/user/service/UserServiceMapper.java index 7fdf08a..e33bfec 100644 --- a/src/main/java/com/pcb/audy/domain/user/service/UserServiceMapper.java +++ b/src/main/java/com/pcb/audy/domain/user/service/UserServiceMapper.java @@ -1,9 +1,7 @@ package com.pcb.audy.domain.user.service; -import com.pcb.audy.domain.user.dto.response.SocketUserGetRes; import com.pcb.audy.domain.user.dto.response.UserGetRes; import com.pcb.audy.domain.user.entity.User; -import java.util.List; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @@ -12,6 +10,4 @@ public interface UserServiceMapper { UserServiceMapper INSTANCE = Mappers.getMapper(UserServiceMapper.class); UserGetRes toUserGetRes(User user); - - List toUserGetResList(List users); } From e2573771226628795e0ed75bbca5d9fbd2324b72 Mon Sep 17 00:00:00 2001 From: emost22 Date: Thu, 29 Feb 2024 23:13:41 +0900 Subject: [PATCH 2/2] [#139] Docs: add get socket users api for restdocs --- src/docs/asciidoc/index.adoc | 6 +++++- .../socket/user/get-socket-user-request.adoc | 5 +++++ .../socket/user/get-socket-user-response.adoc | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/docs/custom-snippets/socket/user/get-socket-user-request.adoc create mode 100644 src/docs/custom-snippets/socket/user/get-socket-user-response.adoc diff --git a/src/docs/asciidoc/index.adoc b/src/docs/asciidoc/index.adoc index 0c73bd0..dd2f99d 100644 --- a/src/docs/asciidoc/index.adoc +++ b/src/docs/asciidoc/index.adoc @@ -26,7 +26,11 @@ include::{snippets}/sample-controller-test/샘플_전체_조회/auto-section.ado == user API include::{snippets}/user-controller-test/유저_조회/auto-section.adoc[] -include::{snippets}/user-controller-test/코스_동시_접속_유저_조회/auto-section.adoc[] + +=== Get socket users API + +include::{snippets}/../../src/docs/custom-snippets/socket/user/get-socket-user-request.adoc[] +include::{snippets}/../../src/docs/custom-snippets/socket/user/get-socket-user-response.adoc[] == pin API diff --git a/src/docs/custom-snippets/socket/user/get-socket-user-request.adoc b/src/docs/custom-snippets/socket/user/get-socket-user-request.adoc new file mode 100644 index 0000000..2afdfe9 --- /dev/null +++ b/src/docs/custom-snippets/socket/user/get-socket-user-request.adoc @@ -0,0 +1,5 @@ +[source,http,options="nowrap"] +---- +SEND /pub/{courseId}/user +EX /pub/1/user +---- diff --git a/src/docs/custom-snippets/socket/user/get-socket-user-response.adoc b/src/docs/custom-snippets/socket/user/get-socket-user-response.adoc new file mode 100644 index 0000000..307474a --- /dev/null +++ b/src/docs/custom-snippets/socket/user/get-socket-user-response.adoc @@ -0,0 +1,18 @@ +[source,http,options="nowrap"] +---- +SUBSCRIBE /sub/{courseId}/user +EX /sub/1/user +Content-Type: application/json +{ + "code" : 0, + "message" : "실행에 성공했습니다.", + "data" : { + "total" : 1, + "users" : [ + { + "userId": 1 + } + ] + } +} +----