Skip to content

Commit

Permalink
Adding logic for getting results for each user
Browse files Browse the repository at this point in the history
  • Loading branch information
orchestr7 committed Oct 19, 2024
1 parent 66af2ad commit 2730c65
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import ru.posidata.backend.service.UserService
import ru.posidata.common.RoundResult
import ru.posidata.common.UserDataFromTelegram

@RestController
@RequestMapping("test")
class TestController(
private val userService: UserService
) {
@GetMapping("/test")
fun get(): ResponseEntity<Any> {
println("Hi!")
@GetMapping("/test1")
fun createUser(): ResponseEntity<Any> {
userService.findOrCreateUser(
UserDataFromTelegram(
1234567,
Expand All @@ -27,4 +27,20 @@ class TestController(
)
return ResponseEntity.status(HttpStatus.OK).body("HI!")
}

@GetMapping("/test2")
fun getResults(): ResponseEntity<Any> {
val a = userService.getResultsByUser(
UserDataFromTelegram(
1234567,
"a",
"a",
"",
123,
"",
"blabla",
)
)
return ResponseEntity.status(HttpStatus.OK).body(a?.map { RoundResult(it.roundNumber, it.result) })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RoundResultsEntity(
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long = 0,

var serialNumber: Int,
var roundNumber: Int,
var result: Int,

@ManyToOne
Expand All @@ -23,7 +23,7 @@ class RoundResultsEntity(
other as RoundResultsEntity

if (id != other.id) return false
if (serialNumber != other.serialNumber) return false
if (roundNumber != other.roundNumber) return false
if (result != other.result) return false
if (user != other.user) return false

Expand All @@ -32,7 +32,7 @@ class RoundResultsEntity(

override fun hashCode(): Int {
var result1 = id.hashCode()
result1 = 31 * result1 + serialNumber.hashCode()
result1 = 31 * result1 + roundNumber.hashCode()
result1 = 31 * result1 + result.hashCode()
result1 = 31 * result1 + user.hashCode()
return result1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package ru.posidata.backend.service
import org.springframework.stereotype.Service
import ru.posidata.backend.entity.RoundResultsEntity
import ru.posidata.backend.entity.UserEntityFromDb
import ru.posidata.backend.repository.RoundResultsRepository
import ru.posidata.backend.repository.UserRepository
import ru.posidata.common.UserDataFromTelegram

@Service
class UserService(
private val userRepository: UserRepository,
private val roundResultsRepository: RoundResultsRepository
) {
fun findOrCreateUser(user: UserDataFromTelegram): UserEntityFromDb {
return userRepository.findByUsername(user.username) ?: run {
Expand All @@ -20,7 +18,7 @@ class UserService(
lastName = user.lastName,
)
val roundResult = RoundResultsEntity(
serialNumber = 0,
roundNumber = 0,
result = 0,
user = newUser
)
Expand All @@ -30,6 +28,10 @@ class UserService(
}
}

fun getResultsByUser(userFromTg: UserDataFromTelegram): List<RoundResultsEntity>? {
return userRepository.findByUsername(userFromTg.username)?.roundResults ?: return null
}

fun getRandomUser(): UserEntityFromDb {
return userRepository.findAll().random()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ databaseChangeLog:
primaryKey: true
nullable: false
- column:
name: serial_number
name: round_number
type: INT
constraints:
nullable: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.posidata.common

data class RoundResult(
val roundNumber: Int,
val result: Int
)
8 changes: 4 additions & 4 deletions frontend/src/jsMain/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@

<head xmlns:og="http://ogp.me/ns#">
<!-- HTML Meta Tags -->
<title>Покемон или BigData</title>
<meta name="description" content="Покемон или БигДата?">
<title>Pokemon or Bigdata?</title>
<meta name="description" content="Pokemon or Bigdata?">

<!-- Facebook Meta Tags -->
<meta property="og:url" content="https://posidata.ru">
<meta property="og:type" content="website">
<meta property="og:title" content="posidata.ru">
<meta property="og:description" content="Покемон или БигДата? Кто ты сегодня?">
<meta property="og:description" content="Pokemon or Bigdata?">
<meta property="og:image" content="https://posidata.ru/img/preview.png">

<!-- Twitter Meta Tags -->
<meta name="twitter:card" content="summary_large_image">
<meta property="twitter:domain" content="posidata.ru">
<meta property="twitter:url" content="https://posidata.ru">
<meta name="twitter:title" content="posidata.ru">
<meta name="twitter:description" content="Покемон или БигДата? Кто ты сегодня?">
<meta name="twitter:description" content="Pokemon or Bigdata?">
<meta name="twitter:image" content="https://posidata.ru/img/preview.png">

<!-- Meta Tags Generated via https://www.opengraph.xyz -->
Expand Down

0 comments on commit 2730c65

Please sign in to comment.