Skip to content

Commit

Permalink
Merge pull request #479 from NDLANO/4003-swagger-sort-enum
Browse files Browse the repository at this point in the history
#4003: Sort parameter doc
  • Loading branch information
jnatten authored May 16, 2024
2 parents b8dd90a + c9a130f commit 2c4df79
Show file tree
Hide file tree
Showing 32 changed files with 249 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ trait ArticleControllerV2 {

scrollSearchOr(searchParams.scrollId, language) {
val query = searchParams.query
val sort = Sort.valueOf(searchParams.sort.getOrElse(""))
val sort = searchParams.sort
val license = searchParams.license
val pageSize = searchParams.pageSize.getOrElse(DefaultPageSize)
val page = searchParams.page.getOrElse(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package no.ndla.articleapi.model.api

import no.ndla.articleapi.model.domain.Sort
import sttp.tapir.Schema.annotations.description

// format: off
Expand All @@ -19,7 +20,7 @@ case class ArticleSearchParams(
@description("The number of search hits to display for each page.") pageSize: Option[Int],
@description("Return only articles that have one of the provided ids") ids: Option[List[Long]],
@description("Return only articles of specific type(s)") articleTypes: Option[List[String]],
@description("The sorting used on results. Default is by -relevance.") sort: Option[String],
@description("The sorting used on results. Default is by -relevance.") sort: Option[Sort],
@description("Return all matched articles whether they exist on selected language or not.") fallback: Option[Boolean],
@description("A search context retrieved from the response header of a previous search.") scrollId: Option[String],
@description("A comma separated list of codes from GREP API to filter by.") grepCodes: Option[Seq[String]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

package no.ndla.articleapi.model.domain

import enumeratum._
import com.scalatsi.TypescriptType.TSEnum
import com.scalatsi.{TSNamedType, TSType}
import enumeratum.*
import sttp.tapir.Codec.PlainCodec
import sttp.tapir.Schema
import sttp.tapir.codec.enumeratum.*

sealed abstract class Sort(override val entryName: String) extends EnumEntry

object Sort extends Enum[Sort] {
object Sort extends Enum[Sort] with CirceEnum[Sort] {

val values: IndexedSeq[Sort] = findValues
val all: IndexedSeq[String] = values.map(_.entryName)
Expand All @@ -28,4 +33,11 @@ object Sort extends Enum[Sort] {

def valueOf(s: String): Option[Sort] = Sort.values.find(_.entryName == s)

implicit val schema: Schema[Sort] = schemaForEnumEntry[Sort]
implicit val codec: PlainCodec[Sort] = plainCodecEnumEntry[Sort]
private val tsEnumValues: Seq[(String, String)] = values.map(e => e.toString -> e.entryName)
implicit val enumTsType: TSNamedType[Sort] = TSType.alias[Sort](
"Sort",
TSEnum.string("ArticleSortEnum", tsEnumValues: _*)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ trait AudioController {
query.underlying,
language,
license,
sort,
sort.flatMap(Sort.valueOf),
pageSize,
pageNo,
shouldScroll,
Expand Down Expand Up @@ -329,7 +329,7 @@ trait AudioController {
query: Option[String],
language: Option[String],
license: Option[String],
sort: Option[String],
sort: Option[Sort],
pageSize: Option[Int],
page: Option[Int],
shouldScroll: Boolean,
Expand All @@ -345,7 +345,7 @@ trait AudioController {
license = license,
page = page,
pageSize = pageSize,
sort = Sort.valueOf(sort).getOrElse(Sort.ByRelevanceDesc),
sort = sort.getOrElse(Sort.ByRelevanceDesc),
shouldScroll = shouldScroll,
audioType = audioType.flatMap(AudioType.valueOf),
seriesFilter = seriesFilter,
Expand All @@ -359,7 +359,7 @@ trait AudioController {
license = license,
page = page,
pageSize = pageSize,
sort = Sort.valueOf(sort).getOrElse(Sort.ByTitleAsc),
sort = sort.getOrElse(Sort.ByTitleAsc),
shouldScroll = shouldScroll,
audioType = audioType.flatMap(AudioType.valueOf),
seriesFilter = seriesFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ trait SeriesController {
.serverLogicPure { case (query, language, sort, page, pageSize, scrollId, fallback) =>
scrollSearchOr(scrollId, language.getOrElse(Language.AllLanguages)) {
val shouldScroll = scrollId.exists(InitialScrollContextKeywords.contains)
search(query, language, sort, pageSize, page, shouldScroll, fallback.getOrElse(false))
search(query, language, Sort.valueOf(sort), pageSize, page, shouldScroll, fallback.getOrElse(false))
}.handleErrorsOrOk
}

Expand Down Expand Up @@ -192,7 +192,7 @@ trait SeriesController {
private def search(
query: Option[String],
language: Option[String],
sort: Option[String],
sort: Option[Sort],
pageSize: Option[Int],
page: Option[Int],
shouldScroll: Boolean,
Expand All @@ -205,7 +205,7 @@ trait SeriesController {
language = language,
page = page,
pageSize = pageSize,
sort = Sort.valueOf(sort).getOrElse(Sort.ByRelevanceDesc),
sort = sort.getOrElse(Sort.ByRelevanceDesc),
shouldScroll = shouldScroll,
fallback = fallback
)
Expand All @@ -216,7 +216,7 @@ trait SeriesController {
language = language,
page = page,
pageSize = pageSize,
sort = Sort.valueOf(sort).getOrElse(Sort.ByTitleAsc),
sort = sort.getOrElse(Sort.ByTitleAsc),
shouldScroll = shouldScroll,
fallback = fallback
)
Expand Down
15 changes: 14 additions & 1 deletion audio-api/src/main/scala/no/ndla/audioapi/model/Sort.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

package no.ndla.audioapi.model

import enumeratum._
import com.scalatsi.TypescriptType.TSEnum
import com.scalatsi.{TSNamedType, TSType}
import enumeratum.*
import sttp.tapir.Codec.PlainCodec
import sttp.tapir.Schema
import sttp.tapir.codec.enumeratum.*

sealed abstract class Sort(override val entryName: String) extends EnumEntry

Expand Down Expand Up @@ -36,4 +41,12 @@ object Sort extends Enum[Sort] {
}
}

implicit val schema: Schema[Sort] = schemaForEnumEntry[Sort]
implicit val codec: PlainCodec[Sort] = plainCodecEnumEntry[Sort]
private val tsEnumValues: Seq[(String, String)] = values.map(e => e.toString -> e.entryName)
implicit val enumTsType: TSNamedType[Sort] = TSType.alias[Sort](
"Sort",
TSEnum.string("AudioSortEnum", tsEnumValues: _*)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package no.ndla.audioapi.model.api

import no.ndla.audioapi.model.Sort
import sttp.tapir.Schema.annotations.description

// format: off
Expand All @@ -17,7 +18,7 @@ case class SearchParams(
@description("The ISO 639-1 language code describing language used in query-params") language: Option[String],
@description("The page number of the search hits to display.") page: Option[Int],
@description("The number of search hits to display for each page.") pageSize: Option[Int],
@description("The sorting used on results. Default is by -relevance.") sort: Option[String],
@description("The sorting used on results. Default is by -relevance.") sort: Option[Sort],
@description("A search context retrieved from the response header of a previous search.") scrollId: Option[String],
@description("Type of audio to filter by.") audioType: Option[String],
@description("Filter result by whether they are a part of a series or not.\n'true' will return only audios that are a part of a series.\n'false' will return only audios that are NOT a part of a series.\nNot specifying will return both audios that are a part of a series and not.") filterBySeries: Option[Boolean],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package no.ndla.audioapi.model.api

import no.ndla.audioapi.model.Sort
import sttp.tapir.Schema.annotations.description

// format: off
Expand All @@ -16,7 +17,7 @@ case class SeriesSearchParams(
@description("The ISO 639-1 language code describing language used in query-params") language: Option[String],
@description("The page number of the search hits to display.") page: Option[Int],
@description("The number of search hits to display for each page.") pageSize: Option[Int],
@description("The sorting used on results. Default is by -relevance.") sort: Option[String],
@description("The sorting used on results. Default is by -relevance.") sort: Option[Sort],
@description("A search context retrieved from the response header of a previous search.") scrollId: Option[String],
@description("Return all matched series whether they exist on selected language or not.") fallback: Option[Boolean]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ trait DraftConceptController {

scrollSearchOr(scrollId, lang.getOrElse(DefaultLanguage)) {
val query = searchParams.query
val sort = searchParams.sort.flatMap(Sort.valueOf)
val sort = searchParams.sort
val language = searchParams.language.getOrElse(AllLanguages)
val pageSize = searchParams.pageSize.getOrElse(DefaultPageSize)
val page = searchParams.page.getOrElse(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ trait PublishedConceptController {
.serverLogicPure { searchParams =>
scrollSearchOr(searchParams.scrollId, searchParams.language.getOrElse(DefaultLanguage)) {
val query = searchParams.query
val sort = searchParams.sort.flatMap(Sort.valueOf)
val sort = searchParams.sort
val language = searchParams.language.getOrElse(Language.AllLanguages)
val pageSize = searchParams.pageSize.getOrElse(DefaultPageSize)
val page = searchParams.page.getOrElse(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package no.ndla.conceptapi.model.api

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.conceptapi.model.domain.Sort
import sttp.tapir.Schema.annotations.description

// format: off
Expand All @@ -19,7 +20,7 @@ case class ConceptSearchParams(
@description("The page number of the search hits to display.") page: Option[Int],
@description("The number of search hits to display for each page.") pageSize: Option[Int],
@description("Return only articles that have one of the provided ids.") ids: Option[List[Long]],
@description("The sorting used on results. Default is by -relevance.") sort: Option[String],
@description("The sorting used on results. Default is by -relevance.") sort: Option[Sort],
@description("Whether to fallback to existing language if not found in selected language.") fallback: Option[Boolean],
@description("A search context retrieved from the response header of a previous search.") scrollId: Option[String],
@description("A comma-separated list of subjects that should appear in the search.") subjects: Option[Set[String]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package no.ndla.conceptapi.model.api

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.conceptapi.model.domain.Sort
import sttp.tapir.Schema.annotations.description

// format: off
Expand All @@ -19,7 +20,7 @@ case class DraftConceptSearchParams(
@description("The page number of the search hits to display.") page: Option[Int],
@description("The number of search hits to display for each page.") pageSize: Option[Int],
@description("Return only articles that have one of the provided ids.") ids: Option[List[Long]],
@description("The sorting used on results. Default is by -relevance.") sort: Option[String],
@description("The sorting used on results. Default is by -relevance.") sort: Option[Sort],
@description("Whether to fallback to existing language if not found in selected language.") fallback: Option[Boolean],
@description("A search context retrieved from the response header of a previous search.") scrollId: Option[String],
@description("A comma-separated list of subjects that should appear in the search.") subjects: Option[Set[String]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

package no.ndla.conceptapi.model.domain

import enumeratum._
import com.scalatsi.TypescriptType.TSEnum
import com.scalatsi.{TSNamedType, TSType}
import enumeratum.*
import sttp.tapir.Codec.PlainCodec
import sttp.tapir.Schema
import sttp.tapir.codec.enumeratum.*

sealed abstract class Sort(override val entryName: String) extends EnumEntry

object Sort extends Enum[Sort] {
object Sort extends Enum[Sort] with CirceEnum[Sort] {

val values: IndexedSeq[Sort] = findValues

Expand Down Expand Up @@ -43,4 +48,11 @@ object Sort extends Enum[Sort] {
}
}

implicit val schema: Schema[Sort] = schemaForEnumEntry[Sort]
implicit val codec: PlainCodec[Sort] = plainCodecEnumEntry[Sort]
private val tsEnumValues: Seq[(String, String)] = values.map(e => e.toString -> e.entryName)
implicit val enumTsType: TSNamedType[Sort] = TSType.alias[Sort](
"Sort",
TSEnum.string("ConceptSortEnum", tsEnumValues: _*)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ trait DraftController {
val language = searchParams.language.getOrElse(Language.AllLanguages)
scrollSearchOr(searchParams.scrollId, language) {
val query = searchParams.query
val sort = Sort.valueOf(searchParams.sort.getOrElse(""))
val sort = searchParams.sort
val license = searchParams.license
val pageSize = searchParams.pageSize.getOrElse(DefaultPageSize)
val page = searchParams.page.getOrElse(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package no.ndla.draftapi.model.api

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.draftapi.model.domain.Sort
import sttp.tapir.Schema.annotations.description

// format: off
Expand All @@ -21,7 +22,7 @@ case class ArticleSearchParams(
@description("The number of search hits to display for each page.") pageSize: Option[Int],
@description("Return only articles that have one of the provided ids") ids: Option[List[Long]],
@description("Return only articles of specific type(s)") articleTypes: Option[List[String]],
@description("The sorting used on results. Default is by -relevance.") sort: Option[String],
@description("The sorting used on results. Default is by -relevance.") sort: Option[Sort],
@description("A search context retrieved from the response header of a previous search.") scrollId: Option[String],
@description("Fallback to some existing language if language is specified.") fallback: Option[Boolean],
@description("Return only articles containing codes from GREP API") grepCodes: Option[List[String]]
Expand Down
17 changes: 15 additions & 2 deletions draft-api/src/main/scala/no/ndla/draftapi/model/domain/Sort.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

package no.ndla.draftapi.model.domain

import enumeratum._
import com.scalatsi.TypescriptType.TSEnum
import com.scalatsi.{TSNamedType, TSType}
import enumeratum.*
import sttp.tapir.Codec.PlainCodec
import sttp.tapir.Schema
import sttp.tapir.codec.enumeratum.*

sealed abstract class Sort(override val entryName: String) extends EnumEntry

object Sort extends Enum[Sort] {
object Sort extends Enum[Sort] with CirceEnum[Sort] {

val values: IndexedSeq[Sort] = findValues

Expand All @@ -33,4 +38,12 @@ object Sort extends Enum[Sort] {
}
}

implicit val schema: Schema[Sort] = schemaForEnumEntry[Sort]
implicit val codec: PlainCodec[Sort] = plainCodecEnumEntry[Sort]
private val tsEnumValues: Seq[(String, String)] = values.map(e => e.toString -> e.entryName)
implicit val enumTsType: TSNamedType[Sort] = TSType.alias[Sort](
"Sort",
TSEnum.string("DraftSortEnum", tsEnumValues: _*)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ trait ImageControllerV2 {
val pageSize = searchParams.pageSize
val page = searchParams.page
val podcastFriendly = searchParams.podcastFriendly
val sort = Sort.valueOf(searchParams.sort)
val sort = searchParams.sort
val includeCopyrighted = searchParams.includeCopyrighted.getOrElse(false)
val shouldScroll = searchParams.scrollId.exists(InitialScrollContextKeywords.contains)
val modelReleasedStatus =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ trait ImageControllerV3 {
val pageSize = searchParams.pageSize
val page = searchParams.page
val podcastFriendly = searchParams.podcastFriendly
val sort = Sort.valueOf(searchParams.sort)
val sort = searchParams.sort
val includeCopyrighted = searchParams.includeCopyrighted.getOrElse(false)
val shouldScroll = searchParams.scrollId.exists(InitialScrollContextKeywords.contains)
val modelReleasedStatus =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package no.ndla.imageapi.model.api

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.imageapi.model.domain.Sort
import sttp.tapir.Schema.annotations.description

// format: off
Expand All @@ -20,7 +21,7 @@ case class SearchParams(
@description("Fallback to existing language if language is specified.") fallback: Option[Boolean],
@description("Return only images with full size larger than submitted value in bytes.") minimumSize: Option[Int],
@description("Return copyrighted images. May be omitted.") includeCopyrighted: Option[Boolean],
@description("""The sorting used on results. The following are supported: relevance, -relevance, title, -title, lastUpdated, -lastUpdated, id, -id. Default is by -relevance (desc) when query is set, and title (asc) when query is empty.""") sort: Option[String],
@description("""The sorting used on results. The following are supported: relevance, -relevance, title, -title, lastUpdated, -lastUpdated, id, -id. Default is by -relevance (desc) when query is set, and title (asc) when query is empty.""") sort: Option[Sort],
@description("The page number of the search hits to display.") page: Option[Int],
@description("The number of search hits to display for each page.") pageSize: Option[Int],
@description("Only show podcast friendly images.") podcastFriendly: Option[Boolean],
Expand Down
17 changes: 15 additions & 2 deletions image-api/src/main/scala/no/ndla/imageapi/model/domain/Sort.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

package no.ndla.imageapi.model.domain

import enumeratum._
import com.scalatsi.TypescriptType.TSEnum
import com.scalatsi.{TSNamedType, TSType}
import enumeratum.*
import sttp.tapir.Codec.PlainCodec
import sttp.tapir.Schema
import sttp.tapir.codec.enumeratum.*

sealed abstract class Sort(override val entryName: String) extends EnumEntry

object Sort extends Enum[Sort] {
object Sort extends Enum[Sort] with CirceEnum[Sort] {

val values: IndexedSeq[Sort] = findValues

Expand All @@ -36,4 +41,12 @@ object Sort extends Enum[Sort] {
}
}

implicit val schema: Schema[Sort] = schemaForEnumEntry[Sort]
implicit val codec: PlainCodec[Sort] = plainCodecEnumEntry[Sort]
private val tsEnumValues: Seq[(String, String)] = values.map(e => e.toString -> e.entryName)
implicit val enumTsType: TSNamedType[Sort] = TSType.alias[Sort](
"Sort",
TSEnum.string("ImageSortEnum", tsEnumValues: _*)
)

}
Loading

0 comments on commit 2c4df79

Please sign in to comment.