diff --git a/rsc.jar b/rsc.jar new file mode 100644 index 0000000..4423d51 Binary files /dev/null and b/rsc.jar differ diff --git a/src/main/kotlin/com/example/demo/controller/HomeController.kt b/src/main/kotlin/com/example/demo/controller/HomeController.kt index e66834d..34a9ae5 100644 --- a/src/main/kotlin/com/example/demo/controller/HomeController.kt +++ b/src/main/kotlin/com/example/demo/controller/HomeController.kt @@ -12,6 +12,8 @@ import org.springframework.web.bind.annotation.RestController import reactor.core.publisher.Flux import reactor.core.publisher.Mono import org.springframework.context.annotation.Lazy +import org.springframework.messaging.handler.annotation.DestinationVariable +import org.springframework.messaging.handler.annotation.MessageMapping @RestController @@ -56,6 +58,16 @@ class HomeController(@Autowired @Lazy private val userService: UserService) { suspend fun messages(@PathVariable userName: String) = coroutineScope { userService.getUserMessages(userName) } + + @MessageMapping("messages.findAll") + suspend fun all() = coroutineScope { + userService.getAllMessages() + } + + @MessageMapping("users.{name}") + suspend fun getUser(@DestinationVariable name: String) = coroutineScope { + userService.getUserByName(name) + } } data class MyData(val id: Int, val name2: String) \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/service/UserService.kt b/src/main/kotlin/com/example/demo/service/UserService.kt index fd624df..223be84 100644 --- a/src/main/kotlin/com/example/demo/service/UserService.kt +++ b/src/main/kotlin/com/example/demo/service/UserService.kt @@ -8,37 +8,49 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.asFlow import org.jooq.Condition import org.jooq.DSLContext +import org.jooq.Record import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Lazy import org.springframework.stereotype.Service import reactor.core.publisher.Flux +import java.util.* @Service @Lazy class UserService(@Autowired @Lazy private val dsl: DSLContext) { - fun users(): Flux { - return Flux - .from(dsl - .select() - .from(Tables.USERS)) - .map { - User(it.get(USERS.ID), it.get(USERS.NAME)) - } - } + fun users(): Flux = Flux + .from(dsl + .select() + .from(Tables.USERS)) + .map(Query::toUser) + suspend fun users2(): Flow { return dsl .select() .from(USERS) .fetch() - .map { userRecord -> - User(userRecord.get(USERS.ID), userRecord.get(USERS.NAME)) - } + .map(Query::toUser) .asFlow() } + suspend fun getUserByName(userName: String): Optional = dsl + .select(USERS.ID, USERS.NAME) + .from(USERS) + .where(USERS.NAME.eq(userName)) + .limit(1) + .fetchOptional() + .map(Query::toUser) + + + suspend fun getAllMessages(): Flow = dsl + .select(MESSAGES.MESSAGE).from(MESSAGES) + .fetch() + .map { it.get(MESSAGES.MESSAGE) } + .asFlow() + suspend fun getUserMessages(userName: String): Flow = dsl .select(MESSAGES.MESSAGE).from(MESSAGES, USERS) .where(MESSAGES.USER_ID.eq(USERS.ID), USERS.NAME.eq(userName)) @@ -56,5 +68,6 @@ class UserService(@Autowired @Lazy private val dsl: DSLContext) { private object Query { val messages = listOf(MESSAGES.MESSAGE) val complexClause: Condition = MESSAGES.MESSAGE.eq("") + fun toUser(r: Record) = User(r.get(USERS.ID), r.get(USERS.NAME)) } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3ef26f9..7eefe5f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,3 +5,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/test_db spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.username=test_user spring.datasource.password=password + +spring.rsocket.server.port=7000 +spring.rsocket.server.transport=tcp \ No newline at end of file