diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Telegram.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Telegram.kt index 0447e70..3769db6 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Telegram.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Telegram.kt @@ -41,48 +41,36 @@ class Telegram { @PostMapping("webhook") fun test(@RequestBody str : String) { println("path >>> $str") -// val client = WebClient.create() -// client.get() -// .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/getUpdates") -// .retrieve() -// .bodyToMono(String::class.java).block() ?: "FAIL" } -// @ResponseBody -// @GetMapping("webhook") -// fun test(@PathVariable path : String) { -// println("path >>> $path") -// val client = WebClient.create() -// client.get() -// .uri("https://api.telegram.org/bot7934509464:AAE_xUbICxMdywLGnxo7BkeIqA1nVza4P9w/getUpdates") -// .retrieve() -// .bodyToMono(String::class.java).block() ?: "FAIL" -// -// } @Bean @Scheduled(cron = "0 0 0/1 * * *") // fun runJob() { try { - println("telegramBotKey >>>> ${globalEvv.telegramBotKey}") - println("telegramMyId >>>> ${globalEvv.telegramMyId}") - println("weatherApiKey >>>> ${globalEvv.weatherApiKey}") - if (globalEvv.weatherApiKey?.length ?: 0 > 3 && globalEvv.telegramBotKey?.length ?: 0 > 3 && globalEvv.telegramMyId?.length ?: 0 > 3) { + logService.log("telegramBotKey >>>> ${globalEvv.telegramBotKey}") + logService.log("telegramMyId >>>> ${globalEvv.telegramMyId}") + logService.log("weatherApiKey >>>> ${globalEvv.weatherApiKey}") + if ( + ((globalEvv.weatherApiKey?.length ?: 0) > 3) && + ((globalEvv.telegramBotKey?.length ?: 0) > 3) && + ((globalEvv.telegramMyId?.length ?: 0) > 3) + ) { val client0 = WebClient.create() val result = client0.get() .uri("http://api.weatherapi.com/v1/current.json?key=${globalEvv.weatherApiKey}&q=seoul&aqi=no") .retrieve() .bodyToMono(String::class.java) .block() ?: "FAIL" - var sss = Gson().fromJson(result, CurrentWeather::class.java) - - val client = WebClient.create() - client.get() - .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=온도${sss.current?.temp_c}") - .retrieve() - .bodyToMono(String::class.java).block() ?: "FAIL" + Gson().fromJson(result, CurrentWeather::class.java)?.let { sss -> + val client = WebClient.create() + client.get() + .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=온도${sss.current?.temp_c}") + .retrieve() + .bodyToMono(String::class.java).block() ?: "FAIL" + } } }catch (e : Exception) { e.printStackTrace() @@ -95,27 +83,31 @@ class Telegram { @Scheduled(cron = "0 0/2 * * * *") // fun pollingTelegramUpdate() { try { - println("pollingTelegramUpdate telegramBotKey >>>> ${globalEvv.telegramBotKey}") - println("pollingTelegramUpdate telegramMyId >>>> ${globalEvv.telegramMyId}") - println("pollingTelegramUpdate weatherApiKey >>>> ${globalEvv.weatherApiKey}") - if (globalEvv.weatherApiKey?.length ?: 0 > 3 && globalEvv.telegramBotKey?.length ?: 0 > 3 && globalEvv.telegramMyId?.length ?: 0 > 3) { + logService.log("pollingTelegramUpdate telegramBotKey >>>> ${globalEvv.telegramBotKey}") + logService.log("pollingTelegramUpdate telegramMyId >>>> ${globalEvv.telegramMyId}") + logService.log("pollingTelegramUpdate weatherApiKey >>>> ${globalEvv.weatherApiKey}") + if ( + ((globalEvv.weatherApiKey?.length ?: 0) > 3 )&& + ((globalEvv.telegramBotKey?.length ?: 0) > 3 )&& + ((globalEvv.telegramMyId?.length ?: 0) > 3) + ) { val client0 = WebClient.create() val result = client0.get() .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/getUpdates") .retrieve() .bodyToMono(String::class.java).block() ?: "FAIL" - println("pollingTelegramUpdate result >>>> $result") - var sss = Gson().fromJson(result, TelegramUpdate::class.java) - println("pollingTelegramUpdate sss >>>> $sss") - if (sss.isSucces()) { - sss.result?.filter { -// println("${(it.message?.date ?: 0L).times(1000L) } > ${before5Min()} ${(it.message?.date ?: 0L).times(1000L) - before5Min()} ") - ((it.message?.date ?: 0L) * 1000L) > before5Min() - }?.forEach { - println("pollingTelegramUpdate before Query doOnSuccess m >>>> ${it}") - it.message?.let { msg -> - println("pollingTelegramUpdate before Query doOnSuccess m >>>> ${msg.message_id}") - qns(msg.message_id,msg) + logService.log("pollingTelegramUpdate result >>>> $result") + Gson().fromJson(result, TelegramUpdate::class.java)?.let { sss -> + logService.log("pollingTelegramUpdate sss >>>> $sss") + if (sss.isSucces()) { + sss.result?.filter { + ((it.message?.date ?: 0L) * 1000L) > before5Min() + }?.forEach { + logService.log("pollingTelegramUpdate before Query doOnSuccess m >>>> ${it}") + it.message?.let { msg -> + logService.log("pollingTelegramUpdate before Query doOnSuccess m >>>> ${msg.message_id}") + qns(msg.message_id,msg) + } } } } @@ -126,28 +118,31 @@ class Telegram { } fun qns(it : String, msg : Message) { - var doSave = true - telegramService.findById(it)?.subscribe( { m -> - println("pollingTelegramUpdate doOnSuccess m >>>> $m") - if (m != null) { - if (msg.text?.contains("어디") == true) { + var doSave = true + telegramService.findById(it)?.subscribe( { m -> + logService.log("pollingTelegramUpdate doOnSuccess m >>>> $m") + if (m != null) { + if (msg.text?.contains("어디") == true) { - } else { - logService.log(msg.text ?: "NONE") - } - } else { - doSave = false - } - },{ e -> - e.printStackTrace() - },{ - if (doSave) { - telegramService.save(msg) - sendMsg() - } - println("pollingTelegramUpdate doOnSuccess comp ") - }) + } else { + logService.log(msg.text ?: "NONE") + } + } else { + doSave = false + } + },{ e -> + e.printStackTrace() + },{ + if (doSave) { + telegramService.save(msg) + if (msg.text?.contains("어디") == true || msg.text?.startsWith("\"") == true) { + sendMsg() + } + } + logService.log("pollingTelegramUpdate doOnSuccess comp") + }) } + fun sendMsg() { val client = WebClient.create() client.get() diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/User.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/User.kt deleted file mode 100644 index 1618a3f..0000000 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/User.kt +++ /dev/null @@ -1,45 +0,0 @@ -package kr.lunaticbum.back.lun.controllers - -import jakarta.servlet.http.HttpServletRequest -import org.springframework.web.bind.annotation.* -import org.springframework.web.reactive.function.client.WebClient -import org.springframework.web.servlet.ModelAndView -import org.springframework.web.servlet.view.ContentNegotiatingViewResolver - - -@RestController -@RequestMapping("user") -class User { - - @GetMapping("/join") - fun hello(httpServletRequest: HttpServletRequest): ModelAndView { - println("onJoin") - val vm = ModelAndView() - vm.modelMap.put("ddd","asdas") - println("${vm.toString()}") - return vm - } - - - - - @ResponseBody - @PostMapping("joinUser.api") - fun joinUser(httpServletRequest: HttpServletRequest, @RequestBody jsonString: String) : String { - println("${httpServletRequest.requestURI}") - val reqString = jsonString - println(reqString) - return "1234" - } - - @ResponseBody - @GetMapping("test/{path}") - fun test(@PathVariable path : String): String { - println("path >>> $path") - val client = WebClient.create() - return client.get() - .uri("https://api.telegram.org/bot7934509464:AAE_xUbICxMdywLGnxo7BkeIqA1nVza4P9w/sendMessage?chat_id=71476436&text=${path}") - .retrieve() - .bodyToMono(String::class.java).block() ?: "FAIL" - } -} \ No newline at end of file diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt new file mode 100644 index 0000000..945fabb --- /dev/null +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt @@ -0,0 +1,96 @@ +package kr.lunaticbum.back.lun.controllers + +import com.google.gson.Gson +import jakarta.servlet.http.HttpServletRequest +import kr.lunaticbum.back.lun.configs.GlobalEnvironment +import kr.lunaticbum.back.lun.model.ResponceResult +import kr.lunaticbum.back.lun.model.User +import kr.lunaticbum.back.lun.model.UserManager +import kr.lunaticbum.back.lun.utils.LogService +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.data.annotation.AccessType +import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.* +import org.springframework.web.reactive.function.client.WebClient +import org.springframework.web.servlet.ModelAndView +import org.springframework.web.servlet.function.ServerResponse +import reactor.core.publisher.Mono + + +@RestController +@RequestMapping("user") +class UserController { + + @Autowired + lateinit var globalEvv : GlobalEnvironment + + @Autowired + lateinit var logService: LogService + + @Autowired + lateinit var userManager: UserManager + + @GetMapping("/join") + fun hello(httpServletRequest: HttpServletRequest): ModelAndView { + logService.log("onJoin") + val vm = ModelAndView() + vm.modelMap.put("ddd","asdas") + logService.log("${vm.toString()}") + return vm + } + + + + + @ResponseBody + @PostMapping("joinUser.api") + fun joinUser(httpServletRequest: HttpServletRequest, @RequestBody jsonString: String) : ResponseEntity { + logService.log("${httpServletRequest.requestURI}") + logService.log(jsonString) + val reqString = jsonString.split("_||L_") + var nb = arrayListOf() + (reqString[0].replace("_||L_","").split("")).toList().let { + nb.addAll(it) + } + logService.log(nb.toString()) + var na = arrayListOf() + reqString[1].replace("_||L_","").split("").toList().let { + na.addAll(it) + } + + logService.log(na.toString()) + var max = nb.size + na.size + var fullData = arrayListOf() + for (idx in 0..max) { + if (idx % 2 == 0) { + if (nb.size > 0) { + fullData.add(nb.removeLast()) + } + } else { + if (na.size > 0) { + fullData.add(na.removeLast()) + } + } + } + logService.log(fullData.joinToString("")) + var user = Gson().fromJson(fullData.joinToString(""), User::class.java) + var u = userManager.save(user).block() + val responce = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(ResponceResult().apply { + resultCode = if (u != null) 0 else 8245 + resultMsg = if (u != null) "OK" else "User Insert Fail" + }) + return responce + } + + @ResponseBody + @GetMapping("test/{path}") + fun test(@PathVariable path : String): String { + logService.log("path >>> $path") + val client = WebClient.create() + return client.get() + .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${path}") + .retrieve() + .bodyToMono(String::class.java).block() ?: "FAIL" + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/model/ResponceResult.kt b/src/main/kotlin/kr/lunaticbum/back/lun/model/ResponceResult.kt new file mode 100644 index 0000000..8d8d866 --- /dev/null +++ b/src/main/kotlin/kr/lunaticbum/back/lun/model/ResponceResult.kt @@ -0,0 +1,10 @@ +package kr.lunaticbum.back.lun.model + +import lombok.Getter + + +@Getter +class ResponceResult { + var resultCode: Int = 0 + var resultMsg: String? = null +} diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/model/User.kt b/src/main/kotlin/kr/lunaticbum/back/lun/model/User.kt new file mode 100644 index 0000000..78362e0 --- /dev/null +++ b/src/main/kotlin/kr/lunaticbum/back/lun/model/User.kt @@ -0,0 +1,73 @@ +package kr.lunaticbum.back.lun.model + +import kr.lunaticbum.back.lun.utils.LogService +import lombok.AllArgsConstructor +import lombok.Data +import lombok.NoArgsConstructor +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.annotation.Primary +import org.springframework.data.annotation.CreatedDate +import org.springframework.data.annotation.Id +import org.springframework.data.mongodb.core.mapping.Document +import org.springframework.data.mongodb.repository.Query +import org.springframework.data.mongodb.repository.ReactiveMongoRepository +import org.springframework.stereotype.Repository +import org.springframework.stereotype.Service +import reactor.core.publisher.Mono + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Document(collection = "User") +class User { + @Id + var user_id: String? = null + var user_pw: String? = null + var user_pw_check: String? = null + + var user_email: String? = null + @CreatedDate + var user_join: Long = 0L + + var user_name: String? = null + var isAccept : String? = null + var isAdmin : String? = null +} + + +@Repository +interface UserRepository : ReactiveMongoRepository { + @Query("{id :?0}") + override fun findById(id: String): Mono + + + fun save(user: User): Mono +} +interface UserService { + fun findById(id: String): Mono? +} + +@Service +class UserManager : UserService { + @Autowired + private lateinit var logService: LogService + + @Autowired + private lateinit var userRepository: UserRepository + + + + override fun findById(id: String): Mono? { + return userRepository.findById(id) + } + + + + fun save(user: User): Mono { + println("saved user before ${user}") + return userRepository.save(user) +// .subscribe( { println("saved user after ${it}") },{e -> e.printStackTrace()},{ +// println("saved user comp") +// }) + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/utils/Logger.kt b/src/main/kotlin/kr/lunaticbum/back/lun/utils/Logger.kt index 06a9e13..6c3a4bc 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/utils/Logger.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/utils/Logger.kt @@ -44,12 +44,13 @@ class Logger { @Service @RequiredArgsConstructor class LogService { - private val myLogger: Logger? = null + + fun logic(id: String) { - myLogger?.log("service id = $id") + println("service id = $id") } fun log(id: String) { - myLogger?.log("log = $id") + println("log = $id") } } diff --git a/src/main/resources/templates/user/join.html b/src/main/resources/templates/user/join.html index 4ef1c80..5996c26 100644 --- a/src/main/resources/templates/user/join.html +++ b/src/main/resources/templates/user/join.html @@ -64,50 +64,64 @@ } -
+ @@ -135,6 +149,6 @@

회원가입

-
+ \ No newline at end of file