Skip to content

Commit c684777

Browse files
committed
#65 UNSAFE try to fix GET /people
Signed-off-by: vityaman <vityaman.dev@yandex.ru>
1 parent 4fe988e commit c684777

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

backend/foundation/src/main/kotlin/ru/ifmo/se/dating/spring/storage/SpringJooqDatabase.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.jooq.SQLDialect
1010
import org.jooq.conf.Settings
1111
import org.jooq.exception.IntegrityConstraintViolationException
1212
import org.jooq.impl.DSL
13+
import org.jooq.tools.r2dbc.LoggingConnection
1314
import org.springframework.r2dbc.core.DatabaseClient
1415
import org.springframework.stereotype.Component
1516
import reactor.core.publisher.Flux
@@ -24,6 +25,7 @@ class SpringJooqDatabase(private val database: DatabaseClient) : JooqDatabase {
2425
private val settings = Settings()
2526
.withBindOffsetDateTimeType(true)
2627
.withBindOffsetTimeType(true)
28+
.withExecuteLogging(true)
2729

2830
override fun <T : Any> flow(block: DSLBlock<T>): Flow<T> =
2931
flux(block).asFlow()
@@ -35,7 +37,7 @@ class SpringJooqDatabase(private val database: DatabaseClient) : JooqDatabase {
3537
mono(block).awaitSingleOrNull()
3638

3739
private fun Connection.dsl() =
38-
DSL.using(this, SQLDialect.POSTGRES, settings)
40+
DSL.using(LoggingConnection(this), SQLDialect.POSTGRES, settings)
3941

4042
private fun <T : Any> flux(block: DSLBlock<T>) = database
4143
.inConnectionMany { block(it.dsl()).toFlux() }

backend/people/src/main/kotlin/ru/ifmo/se/dating/people/api/HttpPeopleApi.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class HttpPeopleApi(
3030
private val interestService: InterestService,
3131
private val pictureService: PictureService,
3232
) : PeopleApiDelegate {
33-
@Suppress("LongMethod", "CyclomaticComplexMethod", "MagicNumber")
33+
@Suppress("LongMethod", "CyclomaticComplexMethod", "MagicNumber", "CognitiveComplexMethod")
3434
override fun peopleGet(
3535
offset: Long,
3636
limit: Long,
@@ -89,7 +89,8 @@ class HttpPeopleApi(
8989
facultyId = facultyId?.let { Faculty.Id(it.toInt()) },
9090
updated = (updatedMin ?: OffsetDateTime.MIN)..(updatedMax ?: OffsetDateTime.MAX),
9191
area = area,
92-
picturesCount = (picturesCountMin ?: 0)..(picturesCountMax ?: Int.MAX_VALUE),
92+
picturesCount =
93+
(picturesCountMin ?: Int.MIN_VALUE)..(picturesCountMax ?: Int.MAX_VALUE),
9394
zodiac = zodiac?.toModel(),
9495
topicIds = topicId?.map { Topic.Id(it.toInt()) }?.toSet() ?: emptySet(),
9596
),

backend/people/src/main/kotlin/ru/ifmo/se/dating/people/storage/jooq/JooqPersonStorage.kt

+27-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.jooq.kotlin.ge
1414
import org.jooq.kotlin.le
1515
import org.springframework.stereotype.Repository
1616
import ru.ifmo.se.dating.exception.NotFoundException
17+
import ru.ifmo.se.dating.logging.Log.Companion.autoLog
1718
import ru.ifmo.se.dating.pagging.Page
1819
import ru.ifmo.se.dating.people.model.Location
1920
import ru.ifmo.se.dating.people.model.Person
@@ -30,6 +31,8 @@ import ru.ifmo.se.dating.storage.FetchPolicy
3031
import ru.ifmo.se.dating.storage.TxEnv
3132
import ru.ifmo.se.dating.storage.jooq.JooqDatabase
3233
import ru.ifmo.se.dating.storage.jooq.withPolicy
34+
import java.time.LocalDate
35+
import java.time.OffsetDateTime
3336

3437
@Suppress("TooManyFunctions")
3538
@Repository
@@ -40,6 +43,8 @@ class JooqPersonStorage(
4043
private val interests: InterestStorage,
4144
private val locations: LocationStorage,
4245
) : PersonStorage {
46+
private val log = autoLog()
47+
4348
@Suppress("CyclomaticComplexMethod")
4449
override suspend fun upsert(draft: Person.Draft): PersonVariant = txEnv.transactional {
4550
database.only {
@@ -108,7 +113,7 @@ class JooqPersonStorage(
108113
}
109114
}.let { }
110115

111-
@Suppress("LongMethod")
116+
@Suppress("LongMethod", "CyclomaticComplexMethod", "CognitiveComplexMethod")
112117
override fun selectFilteredReady(
113118
page: Page,
114119
filter: PersonService.Filter,
@@ -119,19 +124,31 @@ class JooqPersonStorage(
119124

120125
filter.lastName?.let { cond = cond.and(PERSON.LAST_NAME.likeRegex(it.pattern)) }
121126

122-
cond = cond.and(PERSON.HEIGHT.ge(filter.height.first))
123-
cond = cond.and(PERSON.HEIGHT.le(filter.height.last))
127+
if (filter.height.first != Int.MIN_VALUE) {
128+
cond = cond.and(PERSON.HEIGHT.ge(filter.height.first))
129+
}
130+
if (filter.height.last != Int.MAX_VALUE) {
131+
cond = cond.and(PERSON.HEIGHT.le(filter.height.last))
132+
}
124133

125-
cond = cond.and(PERSON.BIRTHDAY.ge(filter.birthday.start))
126-
cond = cond.and(PERSON.BIRTHDAY.le(filter.birthday.endInclusive))
134+
if (filter.birthday.start != LocalDate.MIN) {
135+
cond = cond.and(PERSON.BIRTHDAY.ge(filter.birthday.start))
136+
}
137+
if (filter.birthday.endInclusive != LocalDate.MAX) {
138+
cond = cond.and(PERSON.BIRTHDAY.le(filter.birthday.endInclusive))
139+
}
127140

128141
filter.facultyId?.let { cond = cond.and(PERSON.FACULTY_ID.eq(it.number)) }
129142

130-
cond = cond.and(PERSON.UPDATE_MOMENT.ge(filter.updated.start))
131-
cond = cond.and(PERSON.UPDATE_MOMENT.le(filter.updated.endInclusive))
143+
if (filter.updated.start != OffsetDateTime.MIN) {
144+
cond = cond.and(PERSON.UPDATE_MOMENT.ge(filter.updated.start))
145+
}
146+
if (filter.updated.endInclusive != OffsetDateTime.MAX) {
147+
cond = cond.and(PERSON.UPDATE_MOMENT.le(filter.updated.endInclusive))
148+
}
132149

133150
val picturesCountQuery =
134-
select(DSL.count())
151+
selectCount()
135152
.from(PICTURE)
136153
.where(
137154
PICTURE.IS_REFERENCED.eq(true)
@@ -158,6 +175,8 @@ class JooqPersonStorage(
158175
.where(cond)
159176
.offset(page.offset)
160177
.limit(page.limit)
178+
.also { log.warn("Executed SQL: ${it.sql}") }
179+
.also { log.warn("BindValues: ${it.bindValues}") }
161180
}
162181
.map { it.enrichToModel() as Person }
163182
.filter { filter.area == null || filter.area.contains(it.location.coordinates) }

0 commit comments

Comments
 (0)