Skip to content

Commit

Permalink
add query parameter for brands
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Jul 30, 2024
1 parent 4e296db commit a442ced
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.flipperdevices.ifrmvp.backend.route.brands.data
import com.flipperdevices.ifrmvp.backend.model.BrandModel

internal interface BrandsRepository {
suspend fun getBrands(categoryId: Long): List<BrandModel>
suspend fun getBrands(categoryId: Long, query: String): List<BrandModel>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import com.flipperdevices.ifrmvp.backend.db.signal.dao.TableDao
import com.flipperdevices.ifrmvp.backend.db.signal.table.BrandTable
import com.flipperdevices.ifrmvp.backend.model.BrandModel
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.andWhere
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.upperCase

internal class BrandsRepositoryImpl(
private val database: Database,
private val tableDao: TableDao
) : BrandsRepository {
override suspend fun getBrands(categoryId: Long): List<BrandModel> {
override suspend fun getBrands(categoryId: Long, query: String): List<BrandModel> {
check(tableDao.getCategoryById(categoryId).id == categoryId)
return transaction(database) {
BrandTable.selectAll()
.where { BrandTable.categoryId eq categoryId }
.apply {
if (query.isEmpty()) this
else andWhere { BrandTable.folderName.upperCase().like("%${query.uppercase()}%") }
}
.map {
BrandModel(
id = it[BrandTable.id].value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ internal class BrandsRouteRegistry(
.queryParameters["category_id"]
?.toLongOrNull()
?: -1
val query = context.request
.queryParameters["query"]
.orEmpty()
val brandsResponse = BrandsResponse(
brands = brandsRepository.getBrands(categoryId)
brands = brandsRepository.getBrands(categoryId, query)
.sortedBy { brandModel -> brandModel.folderName.lowercase() }
)
context.respond(brandsResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ internal object BrandsSwagger {
description = "Unique id of the category"
required = true
}
queryParameter<String>("query") {
description = "Entry string of brand name. " +
"For example, writing 'sam' - will return every brand that contains this string. " +
"Case insensitive."
required = true
}
}
response {
HttpStatusCode.OK to {
Expand Down

0 comments on commit a442ced

Please sign in to comment.